Hey I'm new to ardunio. I have two questions. I am trying to control a servo with buttons. I have it set up so when I press the one button it runs the servo once.
- How would make the servo run until there is another input?
also I want to be able to stop the servo while its running with another button, I've tried breaks and interrupts and neither of them seemed to work.
- How would I be able to make the servo stop while running
I'm attaching my code. any help would be awesome thanks!!!
#include <Servo.h>
Servo myservo;Â
int pos = 0;Â Â
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int ledPin1 =Â 9;
const int ledPin2 =Â 8;
int buttonState = 0;Â Â Â Â
void setup() {
 myservo.attach(10);
 pinMode(ledPin1, OUTPUT);
 pinMode(ledPin2, OUTPUT);
 pinMode(buttonPin1, INPUT);
 pinMode(buttonPin2, INPUT);
}
void loop()
{
 ButtonHigh();
 ButtonLow();
 ButtonServo();
 ButtonStop();
}
void ButtonHigh(){
 buttonState = digitalRead(buttonPin1);
Â
 if (buttonState == HIGH) {  Â
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, LOW);Â
 }
 else {
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, HIGH);
 }
Â
}
void ButtonLow(){
 buttonState = digitalRead(buttonPin1);
 if (buttonState == LOW) {   Â
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin1, LOW);
 Â
 }
 else {
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin1, HIGH);Â
 }
Â
}
void ButtonServo(){
 buttonState = digitalRead(buttonPin1);
 if (buttonState == LOW)
 {
 for(pos = 0; pos < 180; pos += 1)Â
 {                Â
  myservo.write(pos);      Â
  delay(15);           Â
 }
 for(pos = 180; pos>=50; pos-=1)  Â
 {               Â
  myservo.write(pos);      Â
  delay(15);           Â
 }
 }
}
void ButtonStop(){
 buttonState = digitalRead(buttonPin2);
 if (buttonState == LOW)
 {
  for(pos = 0; pos < 0; pos += 0)Â
 {                Â
  myservo.write(pos);      Â
  delay(15);           Â
 }
 for(pos = 0; pos>=0; pos-= 1)  Â
 {               Â
  myservo.write(pos);      Â
  delay(15);           Â
 }
 }
}