Skip to content

Instantly share code, notes, and snippets.

@bassdread
Created August 30, 2016 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bassdread/e57c9e5ebd5e1ffc943d69ec80d28099 to your computer and use it in GitHub Desktop.
Save bassdread/e57c9e5ebd5e1ffc943d69ec80d28099 to your computer and use it in GitHub Desktop.
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