How can I stop my following code using a key?
Basically the code should run the servo in a loop if "t" is pressed and stop the loop if "s" is pressed.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
int servodata;
void setup() {
myservo.attach(8); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
}
void loop() {
if (Serial.available()>0){
servodata = Serial.read();
while (servodata == 't'){
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(3); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(3); // waits 15ms for the servo to reach the position
}
}
}
while (servodata == 's'){
break;
}
}
thank you for help.
BlinkWithoutDelay
example, that comes with the Arduino IDE. It has been explained throughout the internet.flag
variable ... for example, name the flaggo
... ifgo
istrue
then run the motor (if (go) {
) ... read the value from serial and setgo
accordingly ... that way the servo code is not inside theif serial
loopfor
loop to position the servo?