Go Down

Topic: Arduino Servo two buttons (Read 189 times) previous topic - next topic

dgs9619

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
  }
}
 

Robin2

#1
Apr 23, 2019, 06:28 pm Last Edit: Apr 23, 2019, 06:30 pm by Robin2
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 </>


Code: [Select]
so your code looks like this and is easy to copy to a text editor. See How to use the Forum
Two or three hours spent thinking and reading documentation solves most programming problems.

dgs9619

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.

Code: [Select]
#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
  }
}

Robin2

#3
Apr 23, 2019, 08:02 pm Last Edit: Apr 23, 2019, 08:02 pm by Robin2
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
Code: [Select]
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
Code: [Select]
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
Two or three hours spent thinking and reading documentation solves most programming problems.

dgs9619

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?

Code: [Select]
#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);
      }
      }
    }
  }

dgs9619

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

Code: [Select]
#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);
      }
      }
    }
  }

blh64

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

MorganS

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.
"The problem is in the code you didn't post."

Robin2

Quote
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
Code: [Select]
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

Code: [Select]
button1State = digitalRead(button1);

and then your tests become
Code: [Select]
if (button1State == LOW) {

and further down, for the second part

Code: [Select]
else if (button2State == LOW) {

You should also have
Code: [Select]
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
Two or three hours spent thinking and reading documentation solves most programming problems.

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

Coding Badly


Go Up