Servo controlled by buttons question HELP

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.

  1. 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.

  1. How would I be able to make the servo stop while running

I'm attaching my code. any help would be awesome :slight_smile: 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);                        
  } 
  }
}

You have working code. Print it out. Now, delete it. Rewrite from scratch, keeping in mind your old requirements AND your new requirements.

For one thing, if the servo is to keep moving back and forth, on each pass through loop, you must determine if it is time to move the servo, and, if so, to where.

The blink without delay example will show you the "is it time to do something" part.

There is no easy way to modify your existing code to incorporate your new requirements.

Thanks :). I was hoping it would be a easier fix....what command would I use to stop the servo?

..what command would I use to stop the servo?

Just stop changing the values you write to the servo.

AWOL:

..what command would I use to stop the servo?

Just stop changing the values you write to the servo.

Im sorry, What do you mean?

The servo will stop moving when you stop changing the values you write to it.