Created
August 30, 2016 17:59
-
-
Save bassdread/e57c9e5ebd5e1ffc943d69ec80d28099 to your computer and use it in GitHub Desktop.
Simple servo code to control a gong
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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