Push button servo sweep programme help

Hello Basically i wanted to control 5 servo with 5 push button. The program written is for 1 servo pls help me to write it for 5 servo with 5 pushbutton control…

Pls modify code for 5 servo with 5 push button… pls sir its urgent…

THANKS IN ADVANCE

/* Combo Sweep with PushButton
*/

#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

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int myservoPin=  9;      // the number of the servo pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object

  pinMode(buttonPin, INPUT);
  pinMode(buttonPin,INPUT_PULLUP);
}

void checkButton(){
  buttonState = digitalRead(buttonPin);
  
  while(buttonState == HIGH)
    buttonState = digitalRead(buttonPin);
  
}
void loop() {
  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(10);                       // waits 15ms for the servo to reach the position
    checkButton();
    
  }
  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(10);                       // waits 15ms for the servo to reach the position
    checkButton();
    
  }


/* ignore
   // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn servo off:
    digitalWrite(myservoPin, LOW);
  } else {
    // turn servo on:
    digitalWrite(myservoPin, HIGH);
  }
  */
  
}

pls sir its urgent..

When is our assignment due to be handed in ?

The simple but clumsy way would be to repeat the code 5 times. Using millis() for timing would be better than using delay(). A better way would be to put values in arrays so that you could iterate through them. An even better way would be to use an array of structs to hold the data to do with each servo. The best way would be to use a class to encapsulate the data and functions for each servo.

Assuming that someone wrote the code for you (unlikely) do you feel that you would be able to explain how and why it works ?

What have you tried so far ?

I have tried it sir but servo's not working except pre programmed code Can you do this for me :wink:

looks like your code just uses a button press to simply delay the movement of the servo. not clear why.

you could use an array to identify pins for as many servos as you would like and you could write(pos) to each

it would help to know how you want to control the servos

Can you do this for me

I could, but I won't

Post what you tried

Sure sir here’s my code but problem is the only buttonpin =2 (number) is working button pin 3 is not working…

/* Combo Sweep with PushButton
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
Servo myservo1;
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position
int pos1 = 0;

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int buttonPin1 =3;
const int myservoPin=  9;      // the number of the servo pin
const int myservoPin1= 11;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo.attach(11);

  pinMode(buttonPin, INPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin,INPUT_PULLUP);
  pinMode(buttonPin1,INPUT_PULLUP);
}

void checkButton(){
  buttonState = digitalRead(buttonPin);
  buttonState = digitalRead(buttonPin1);
  while(buttonState == HIGH)
    buttonState = digitalRead(buttonPin);
    buttonState = digitalRead(buttonPin1);
}
void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
  for (pos1=0; pos <=150; pos +=1)  
    // in steps of 1 degree
    myservo.write(pos); // tell servo to go to position in variable 'pos'
    myservo1.write(pos1);
    delay(10);                       // waits 15ms for the servo to reach the position
    checkButton();
    
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
  for (pos1=150; pos1>=0; pos1 -= 1)
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    myservo1.write(pos);
    delay(10);                       // waits 15ms for the servo to reach the position
    checkButton();
    
  }


/* ignore
   // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn servo off:
    digitalWrite(myservoPin, LOW);
  } else {
    // turn servo on:
    digitalWrite(myservoPin, HIGH);
  }
  */
  
}
void checkButton(){
  buttonState = digitalRead(buttonPin);
  buttonState = digitalRead(buttonPin1);
  while(buttonState == HIGH)
    buttonState = digitalRead(buttonPin);
    buttonState = digitalRead(buttonPin1);
}

I have not looked at most of your code but this function has a problem

Which statement or statements will be executed while buttonState is HIGH and why have you tried to put the states of 2 different buttons in the buttonState variable ?

  pinMode(buttonPin, INPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin,INPUT_PULLUP);
  pinMode(buttonPin1,INPUT_PULLUP);

Why INPUT and then INPUT_PULLUP? And since you ended up with INPUT_PULLUP then a pressed button will be LOW not HIGH if you've wired it correctly. How are you buttons wired?

Steve

can anyone correct the code for two servo sweep because i didn't find any solution???

You have not actually described what the program should do

Should each servo finish its sweep before another one starts ?
Should the program respond to user input to start a second servo sweeping whilst the first servo is sweeping ?
Should a user input for a servo that is sweeping restart the sweep ?
etc, etc

Ok so let me clear all the servo’s are individually controlled with button and sweeping depend on user input like i can stop servo wherever i want (pls look into url link for code) actually i’m making bionic hand and my aim is that for all 5 fingers, when i press button the servo should start moving and stops where i released button, and if i press and hold the button then it should turn from 0 to 180 (suppose) and return from 180 to 0 and so on due to holding of button…it is like moving a finger and stoping it wherever i want…

Here’s the link from where i got this code…This code is for 1 servo and i want it for total 5 servo’s…

https://codebender.cc/sketch:422635#Servo%20Sweep%20with%20Push%20Button.ino

So i want that my bionic finger should sweep(move) and stop wherever i want and sweep when button is pressed and hold.

The code that i modified for 2nd servo but not working

/* Combo Sweep with PushButton
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
Servo myservo1;
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position
int pos1 = 0;

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int buttonPin1 =3;
const int myservoPin=  9;      // the number of the servo pin
const int myservoPin1= 11;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int buttonState1 =0;

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo.attach(11);

  pinMode(buttonPin, INPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin,INPUT_PULLUP);
  pinMode(buttonPin1,INPUT_PULLUP);
}

void checkButton(){
    buttonState = digitalRead(buttonPin);
  while(buttonState == HIGH)
    buttonState = digitalRead(buttonPin);
}
void checkButton1(){
    buttonState1 = digitalRead(buttonPin1);
  while(buttonState1 == HIGH)
    buttonState1 = digitalRead(buttonPin1);
  
}
  
void loop() {
  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(10);                       // waits 15ms for the servo to reach the position
    checkButton();
  for (pos1=0; pos1 <=150; pos1 +=1)
    myservo1.write(pos1);
    delay(10);                       // waits 15ms for the servo to reach the position
    checkButton1();
    
  }
  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(10);                       // waits 15ms for the servo to reach the position
    checkButton();
  for (pos1=150; pos1 >=0; pos1 -= 1)             // tell servo to go to position in variable 'pos'
    myservo1.write(pos1);
    delay(5);                       // waits 15ms for the servo to reach the position
    checkButton1(); 
    
  }


/* ignore
   // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn servo off:
    digitalWrite(myservoPin, LOW);
  } else {
    // turn servo on:
    digitalWrite(myservoPin, HIGH);
  }
  */
  
}

You seem to have ignored most suggestions and all questions. There's just no helping some people.

Steve

Here is some code similar to yours. If this works for one servo, it should be easy to pretty much copy paste more if statements for more buttons/servos.

#include <Servo.h> 
 
Servo myservo; 
#define leftPin 2    //Active Low
#define rightPin 3   //Active Low
int pos = 90;
int delayPeriod = 50;  // increasing this slows down the servo movement

 
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  myservo.write(pos); // center the servo
  pinMode(leftPin, HIGH);   // turn on pullup resistors
  pinMode(rightPin, HIGH);
} 
 
 
void loop() 
{ 
  if(digitalRead(leftPin) == LOW)  
  {                              
   // in steps of 1 degree 
   if( pos > 0)
      --pos;
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(delayPeriod);                      
  } 
  if(digitalRead(rightPin) == LOW)  
  {                              
   if( pos < 180)
       ++pos;
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(delayPeriod);        
  }
}

Your original code would work better if you sorted out the {} braces. If you do an AutoFormat you will see that the Sweep for loops are not what you want. E.g.

  for (pos = 0; pos <= 180; pos += 1) { // *** where does this loop end?
    // in steps of 1 degree
    myservo.write(pos); // tell servo to go to position in variable 'pos'
    delay(10);                       // waits 15ms for the servo to reach the position
    checkButton();
  for (pos1=0; pos1 <=150; pos1 +=1) // *** where does this one start { or end } ?
    myservo1.write(pos1);
    delay(10);                       // waits 15ms for the servo to reach the position
    checkButton1();
   }

Steve

Thanks everyone for helping me
Yes after some days i actually found and edit code for working 5 servo control with push button system…
Like when you press and hold button the servo starts to move and stops where you releases the button with position range.

But there is little bit issue when i press button for 1st servo it works very well servo move very smoothly but when is press all 5 pushbutton at once then my all 5 servos not moving smoothly,they moves like pulses i dont know how to solve this??

Heres my code for 5 servo sweep with 5 pushbutton control

#include <Servo.h>

Servo myservo;  // pinky
Servo myservo1; // ring
Servo myservo2; // middle
Servo myservo3; // index
Servo myservo4; // thumb

//min value = finger is straight
//max value = finger is bend

int angle =170;
int angleStep =10; 

void setup() {
  // Servo buttons
  myservo.attach(3); 
  myservo1.attach(5);
  myservo2.attach(7);
  myservo3.attach(9);
  myservo4.attach(11);
  pinMode(2,INPUT_PULLUP);
  pinMode(4,INPUT_PULLUP);
  pinMode(6,INPUT_PULLUP);
  pinMode(8,INPUT_PULLUP);
  pinMode(10,INPUT_PULLUP);
}

void loop() {
  if(digitalRead(2) == LOW){
    angle = angle + angleStep;
    if (angle <= 0 || angle >= 180) {
      angleStep = -angleStep;
    }myservo.write(angle);
    delay(20); 
  }
 if(digitalRead(4) == LOW){
    angle = angle + angleStep;
    if (angle <= 0 || angle >= 180) {
      angleStep = -angleStep;
       }myservo1.write(angle);    
  delay(20);
    }
 if(digitalRead(6) == LOW){
    angle = angle + angleStep;
    if (angle <= 0 || angle >= 180) {
      angleStep = -angleStep;
       }myservo2.write(angle);    
  delay(20);
    }
  if(digitalRead(8) == LOW){
    angle = angle + angleStep;
    if (angle <= 0 || angle >= 180) {
      angleStep = -angleStep;
       }myservo3.write(angle);    
  delay(20);
    }
  if (digitalRead(10) == LOW){
    angle = angle + angleStep;
    if (angle <= 0 || angle >= 180) {
      angleStep = -angleStep;
       }myservo4.write(angle);    
  delay(20);
    }
   }

You seem to have only one angle variable in the program. How does the program know what angle to write to each servo ?

Yes sir for each servo there is one angle variation..as each servo will have to move in same angle for all 5 fingers, so is this code wrong or something needs to be changed??

when i press single button then the 1st servo moves very smoothly without any problem,
when is press 2 buttons at same time then both 1st & 2nd servo starts very jerky or pulsative kind movements..
when i press all buttons at same time then servos doesn't move smoothly they moves kinda jerky?? :confused:

for each servo there is one angle variation..as each servo will have to move in same angle for all 5 fingers,

If each servo is to be at the same angle then why do you have 5 buttons ?

I imagined that you would want to be able to control each finger individually

Thanks UKHeliBob sir, yes i did a mistake by initializing only single angle position now here’s my code
And again thanks to all the members for helping and teaching things to newbie like me :slight_smile:

Here’s my code now…

#include <Servo.h>

Servo myservo;  // pinky
Servo myservo1; // ring
Servo myservo2; // middle
Servo myservo3; // index
Servo myservo4; // thumb

//min value = finger is straight
//max value = finger is bend

int angle =170;
int angleStep =10;

int angle1 = 170;
int angleStep1= 10;

int angle2 = 170;
int angleStep2= 10;

int angle3 = 170;
int angleStep3= 10;

int angle4= 170;
int angleStep4= 10;

void setup() {
  // Servo buttons
  myservo.attach(3); 
  myservo1.attach(5);
  myservo2.attach(7);
  myservo3.attach(9);
  myservo4.attach(11);
  pinMode(2,INPUT_PULLUP);
  pinMode(4,INPUT_PULLUP);
  pinMode(6,INPUT_PULLUP);
  pinMode(8,INPUT_PULLUP);
  pinMode(10,INPUT_PULLUP);
}

void loop() {
  if(digitalRead(2) == LOW){
    angle = angle + angleStep;
    if (angle <= 0 || angle >= 180) {
      angleStep = -angleStep;
    }myservo.write(angle);
    delay(20); 
  }
 if(digitalRead(4) == LOW){
    angle1 = angle1 + angleStep1;
    if (angle1 <= 0 || angle1 >= 180) {
      angleStep1 = -angleStep1;
       }myservo1.write(angle1);    
  delay(20);
    }
 if(digitalRead(6) == LOW){
    angle2 = angle2 + angleStep2;
    if (angle2 <= 0 || angle2 >= 180) {
      angleStep2 = -angleStep2;
       }myservo2.write(angle2);    
  delay(20);
    }
  if(digitalRead(8) == LOW){
    angle3 = angle3 + angleStep3;
    if (angle3 <= 0 || angle3 >= 180) {
      angleStep3 = -angleStep3;
       }myservo3.write(angle3);    
  delay(20);
    }
  if (digitalRead(10) == LOW){
    angle4 = angle4 + angleStep4;
    if (angle4 <= 0 || angle4 >= 180) {
      angleStep4 = -angleStep4;
       }myservo4.write(angle4);    
  delay(20);
    }
   }

Now revise the code to put the data in arrays (or a struct) and iterate through it to avoid the duplicate code that reads the input, adjusts the angle and writes to the servo for each finger