Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple servo code to control a gong
#include <Servo.h>
Servo myservo;
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup()
{
myservo.attach(2); // attaches the servo on pin 2 to the servo object
myservo.write(90);
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
}
void loop()
{
if (stringComplete && inputString == "gong\n") {
Serial.println(inputString);
myservo.write(140);
delay(100);
myservo.write(40);
delay(5000);
myservo.write(90);
// clear the string ready for another go
inputString = "";
stringComplete = false;
}
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment