Ok, so I'm messing around with an Arduino Uno and a couple of radioshack servos, one standard one micro (amongst a few other things). I'm attempting to write a code where when I use a push button the micro servo rotates and then after a delay the standard servo does the same, plus a basic LED for indication.
I'm having issues writting the program, I've managed to get them to move, but I need to maintain the button pressed for a REALLY long time for them to go off, it was working perfectly with one servo (one quick tap and it all happened swiftly) but now the standard servo won't stop vibrating, they go off at the same time instead of with a delay in between and they misbehave after the loop ends..... any ideas?
#include <Servo.h>
Servo pinservo; // servo pin release object
Servo mtrservo; // servo motor disengage object
int ppos = 0; // variable to store the pinservo position
int pushbutton = 2; // button on pin 2
int LED = 3; //led on pin 3
int mpos = 0; // variable for motor servo position
void setup()
{
pinservo.attach(9); // servo on pin 9
pinMode(ppos, OUTPUT);
pinMode(pushbutton, INPUT);
pinMode(LED,OUTPUT);
digitalWrite (pushbutton, LOW);
mtrservo.attach(7);
pinMode(mpos,OUTPUT);
pinMode(pushbutton,INPUT);
digitalWrite (pushbutton,LOW);
}
void loop()
{
if (digitalRead(pushbutton) == LOW)
for(ppos = 0; ppos < 25; ppos += 25) // degrees from 0 to 45
for (mpos = 0; mpos < 90; mpos += 90)
{
pinservo.write(ppos); //servo to position in variable "pos"
mtrservo.write(mpos);
}
if (digitalRead(pushbutton) == HIGH)
for(ppos = 25; ppos>=25; ppos-=25) // goes from 45 degrees to 0 degrees
for(mpos =90 ; mpos>=90; mpos-=90)
{
mtrservo.write(mpos); //motor disengage, go to variable "mpos"
delay(250);
pinservo.write(ppos); // tell servo to go to position in variable 'ppos'
delay(50); // waits 750ms for the servo to reach the position (affects LED response)
}
if(digitalRead(pushbutton) == HIGH){//if button is pressed turn LED on, else off
digitalWrite(LED,LOW);
}else{
digitalWrite(LED,HIGH);
}
}