Arduino Servo two buttons

Hi, I am working on a project for fun that i need to control one servo using two buttons. When i press one button once it is suppose to do one sweep from 0 to X deg and then back to 0. When i push the other button it is suppose to do that sweep twice. I cant figure out how to make the second button do two sweeps. I am very new to this and any advice would be greatly appreciated. Thanks

Code:

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position
int button1 = 14; // The button will be on Pin 14
int button2 = 15; // button 2 on pin 15

void setup()
{
myservo.attach(7); // attaches the servo on pin 7 to the servo object
pinMode(pos, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
digitalWrite (button1, LOW);
digitalWrite (button2, LOW);
}

void loop()
{

if (digitalRead(button1) == LOW)

for(pos = 0; pos < 90; pos += 90) // goes from 0 degrees to 90 degrees
{ // in steps of degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
// waits 1s for the servo to reach the position
}
if (digitalRead(button1) == HIGH)

for(pos = 90; pos>=90; pos-=90) // goes from 90 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 50ms for the servo to reach the position
}
if (digitalRead(button2) == LOW)
for(pos = 0; pos < 90; pos += 90) // goes from 0 degrees to 90 degrees
{ // in steps of degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
// waits 1s for the servo to reach the position
}
if (digitalRead(button2) == HIGH)

for(pos = 90; pos>=90; pos-=90) // goes from 90 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 50ms for the servo to reach the position
}
}

Read the two buttons as the first thing in loop() and save their values.

Then use a pair of IF statements to check buttonA first - if it is pressed do the single sweep. Else check buttonB and if it is pressed do the double sweep.

...R

PS ... To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Thanks for the response. I read the two buttons initially but now im having trouble coding the if and else statement. Any advice on the setup? Sorry im just super new to this.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position
int button1 = 14;  // The button will be on Pin 14
int button2 = 15; // button 2 on pin 15

void setup()
{
  myservo.attach(7);  // attaches the servo on pin 7 to the servo object
pinMode(pos, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
digitalWrite (button1, LOW);
digitalWrite (button2, LOW);
}

void loop()
{
 digitalRead(button1) == LOW;
 digitalRead(button2) == LOW;
    
 if (digitalRead(button1) == LOW){

  for(pos = 0; pos < 90; pos += 90)  // goes from 0 degrees to 90 degrees
  {                                  // in steps of  degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'                     // waits 1s for the servo to reach the position
  }
  for(pos = 90; pos>=90; pos-=90)     // goes from 90 degrees to 0 degrees
  {                               
     myservo.write(pos);              // tell servo to go to position in variable 'pos'
  delay(50);                             // waits 50ms for the servo to reach the position
  }
    }
 else
 for(pos = 0; pos < 90; pos += 90)  // goes from 0 degrees to 90 degrees
  {                                  // in steps of  degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
                          // waits 1s for the servo to reach the position
  }
  if (digitalRead(button2) == HIGH)
 
  for(pos = 90; pos>=90; pos-=90)     // goes from 90 degrees to 0 degrees
  {                               
     myservo.write(pos);              // tell servo to go to position in variable 'pos'
  delay(50);                             // waits 50ms for the servo to reach the position
  }
}

You don't have any { } for the block of code covered by the ELSE.

If you use the AutoFormat tool to indent your code consistently that sort of thing will be much easier to see.

This code

for(pos = 0; pos < 90; pos += 90)

will make the servo to 90° in one step. Is that what you want?

If you want it to go slowly then you need

for(pos = 0; pos < 90; pos += 5)  // goes from 0 degrees to 90 degrees
{                                                             // in steps of  5 degrees
   myservo.write(pos);                            // tell servo to go to position in variable 'pos' 
   delay(100);                                         // wait for the servo to reach the position
}

and you have similar problems with the other movements

...R

I tried a few things and it still wasnt working. I started a new approach but getting similar results. Any ideas on how to change this new code?

#include <Servo.h>

int button1 = 14;
int button2 = 15;
int pos = 0;

Servo servo;
boolean toggle = true;

void setup()
{
  pinMode(button1, INPUT); //arduino monitor pin state
  pinMode(button2, INPUT);
  servo.attach(7); //pin for servo control signal
  digitalWrite(5, HIGH);
}

void loop()
{
  if (digitalRead(button1) == LOW)
  {
    if(toggle)
    delay(1000);
    {for (pos = 0; pos <= 15; pos += 1){
    servo.write(pos);
    delay(20);}
    for (pos = 15; pos >= 0; pos -=1){
    servo.write(pos);
    delay(1);
    }
    }
  }
  if (digitalRead(button2) == LOW)
  {
    if(toggle)
    delay(1000);
    {for (pos = 0; pos<= 15; pos+=1){
      servo.write(pos);
      delay(20);}
      for (pos = 15; pos >= 0; pos-=1){
        servo.write(pos);
        delay(1);
      }
      }
    }
  }

I am very new to this, but i am coding an arduino uno to control a servo motor. I need it to do a sweep from 0 to X degrees and back to 0 by just one button press from the first button. Then, for the second button I need it to do that exact same thing, just two times. So the second button would tell the servo to go 0 to X degrees back to 0 then back to X degrees again and come back to 0. Sorry, kind of hard to explain in words. Right now i have this code thats just allowing each button to do the loop once. Any advice on how to change the code to do the 2 loops for the second button? Thanks

#include <Servo.h>

int button1 = 14;
int button2 = 15;
int pos = 0;

Servo servo;
boolean toggle = true;

void setup()
{
  pinMode(button1, INPUT); //arduino monitor pin state
  pinMode(button2, INPUT);
  servo.attach(7); //pin for servo control signal
  digitalWrite(5, HIGH);
}

void loop()
{
  if (digitalRead(button1) == LOW)
  {
    if(toggle)
    delay(1000);
    {for (pos = 0; pos <= 15; pos += 1){
    servo.write(pos);
    delay(20);}
    for (pos = 15; pos >= 0; pos -=1){
    servo.write(pos);
    delay(1);
    }
    }
  }
  if (digitalRead(button2) == LOW)
  {
    if(toggle)
    delay(1000);
    {for (pos = 0; pos<= 15; pos+=1){
      servo.write(pos);
      delay(20);}
      for (pos = 15; pos >= 0; pos-=1){
        servo.write(pos);
        delay(1);
      }
      }
    }
  }

How to you normally do the same repetitive task multiple times in any programming language? Use of for() loop. Or, better yet, put the code to move the servo from 0 -> x -> 0 in its own function and then call that function once for button1 or twice for button2

Both of those suggestions are poor options. They will make 'blocking code' which prevents the Arduino from doing anything else useful, such as reading the buttons.

Maybe make a counter: numSweepsToPerform. Then pushing button 1 will increment that counter by 1. The motor keeps sweeping and each finished sweep reduces the counter by 1. If you wanted 3 sweeps, push button 1 3 times or push button 1 once and button 2 once.

I tried a few things and it still wasnt working. I started a new approach but getting similar results. Any ideas on how to change this new code?

That is what I call the scatter-gun approach to debugging - change something, anything. It rarely works.

Go back to the program style in Reply #2. It has most of what is needed and it tries to implement the suggestions I made in Reply #1

I had not noticed these lines in the code in Reply #2

 digitalRead(button1) == LOW;
 digitalRead(button2) == LOW;
    
 if (digitalRead(button1) == LOW){

The first two lines do nothing useful because what they do is not saved. You need to put the values into variables like this

button1State = digitalRead(button1);

and then your tests become

if (button1State == LOW) {

and further down, for the second part

else if (button2State == LOW) {

You should also have

pinMode(button1, INPUT_PULLUP);

and connect the switch so that it pulls the pin to GND when it is pressed.

UNLESS you already have an external pull-down resistor on the switch pins. If so, pressing the switch will give a HIGH

...R

This is the same question as in your other Thread where I have been helping.

I am suggesting to the Moderator to merge the Threads.

Don't Double Post - it just wastes everyone's time.

...R

Threads merged.