Controlling two servos with push button


hello,

i new arduino , trying small project done. attempting control 2 servos single push button. on every press of button want servos rotate 90 degrees in opposite directions. seems working ok except left servo makes vibrating noise @ 1 of positions (180). appreciated.

here code using.

code: [select]
#include <servo.h>

servo leftservo;  // create left servo object
servo rightservo;  // create right servo object
// constant won't change:
const int  buttonpin = 2;    // pushbutton attached pin 2
// variables change:
int buttonstate = 0;         // current state of button
int lastbuttonstate = 0;     // previous state of button
int buttonpushcounter = 0;   // counter number of button presses
int centerpos = 90;
boolean currentbutton = low;

void setup() {
 
   leftservo.attach(9); // left servo attached pin 9
   rightservo.attach(10); //right ervo attached pin 10
  // tell both servos go center position
  leftservo.write(centerpos);
  rightservo.write(centerpos);
 
  delay(15);
 
  pinmode(buttonpin, input); // initialize button pin input:
}

boolean debounce(boolean last)
{
  boolean current = digitalread(buttonpin);
  if (last != current)
  {
    delay(5);
    current = digitalread(buttonpin);
  }
  return current;
}


void loop() {
 
  buttonstate = debounce(buttonpin); // read pushbutton input pin:
 
  // compare buttonstate previous state
  if (buttonstate != lastbuttonstate) {
    // if state has changed, increment counter
    if (buttonstate == high) {
      // if current state high button
      // went off on:
      buttonpushcounter++;
    }
    else {
    }

    // save current state last state,
    lastbuttonstate = buttonstate;
  }
 
  /* moves servo every other button push
  checking modulo of button push counter.
   the modulo function gives remainder of
   the division of 2 numbers: */
  if (buttonpushcounter % 2 == 0) {
  //move position 90 
   rightservo.write(centerpos);
   leftservo.write(centerpos);
   delay(15);
  } else {
  //or: move position 0 
   leftservo.write(0); //move left servo 90 degrees ccw center
   rightservo.write(180); // move right servo 90 cw center
   delay(15);
  }
}

and wrong making vibrating noise? if problem don't have problems!


Arduino Forum > Using Arduino > Project Guidance > Controlling two servos with push button


arduino

Comments

Popular posts from this blog

Convierte tu Raspberry en un NAS. Firmware fvdw-sl 15.3 - Raspberry Pi Forums

How to format a Get Request

avrdude: verification error, first mismatch at byte 0x0000 0x0c != 0x62