Hello all.
I been learning Arduino programming for a couple months now. Just Youtube videos and piecing together references, and I've seen a lot of success .
But I'm getting hung up on this problem:
I'm writing a sketch to open and close a set of blinds, and I want the servo to run slower to avoid loud and jerky movement.
I know I can control servo speeds with a 'for' count, and I know I can control the servo position with 3 push buttons assigned to 0, 90 and 180 degrees with 'if' statements.
But when I try to combine them, so that I can choose a position with a button AND have it move at a reduced speed, it doesn't seem to work.
I want to select one of 3 positions with push buttons, but I want the servo to move at a slower more controlled speed.
Here is the code I'm trying to make work:
#include <Servo.h>
Servo servoMotor1;
int pos=0;
const int button0=2;
const int button1=3;
const int button2=4;
int button0State;
int button1State;
int button2State;
int servoDelay=50;
void setup() {
Serial.begin(9600);
servoMotor1.attach(9);
pos=0; //bring servo to 0 position at reset
servoMotor1.write(pos);
}
void loop() {
button0State=digitalRead(button0); // button calling for 0 degree position
button1State=digitalRead(button1); // button calling for 90 degree position
button2State=digitalRead(button2); // buttin calling for 180 degree position
int lastPos= servoMotor1.read(); //record the last postion written to servo
Serial.print("Button 1 ="); // serial printout for debugging purposes
Serial.println(button0State);
Serial.print("Button 2 =");
Serial.println(button1State);
Serial.print("Button 3 =");
Serial.println(button2State);
Serial.println(" ");
Serial.println(lastPos);
Serial.println(" ");
/////////////////////////////////////////////////
// If a button is pressed & the servo
if(button0State==HIGH && lastPos !=0){ // isn't already at the angle that button
if(lastPos <=95){ // would call for , it then checks to see if the
for (pos=90; pos>=0; pos=pos-1) { // current position (actually the last position
servoMotor1.write(pos); // written) is less than 95.
delay(servoDelay); // Since there are only 3 possible positions
} // 0, 90, and 180; then anything less than 95, but
} // NOT 0 has to be 90 and a request to move
// from 90 to 0 is made.
else { // Otherwise, it's 180 degrees and the 'else'
for (pos=180; pos>=0; pos=pos-1) {// statment request a move from 180 to 0
servoMotor1.write(pos); //
delay(servoDelay); // All the buttons are setup in this manner
} //
} //
} //
/////////////////////////////////////////////////
if(button1State==HIGH && lastPos !=90){
if(lastPos <=89){
for (pos=0; pos>=90; pos=pos+1) {
servoMotor1.write(pos);
delay(servoDelay);
}
}
else {
for (pos=180; pos>=0; pos=pos-1) {
servoMotor1.write(pos);
delay(servoDelay);
}
}
}
/////////////////////////////////////////////////
if(button2State==HIGH && lastPos !=180){
if(lastPos <=89){
for (pos=0; pos>=180; pos=pos+1) {
servoMotor1.write(pos);
delay(servoDelay);
}
}
else {
for (pos=90; pos>=180; pos=pos+1) {
servoMotor1.write(pos);
delay(servoDelay);
}
}
}
}
Unfortunately, this results in the servo moving to the assigned 0 degree position (as requested in setup) and remaining unresponsive from there.
I've confirmed my 'lastPos' and button assignments are accurate via the serial monitor and using them in different sketches that , for instance , don't include the 'for' count to slow the servo and simply designate a position and works as expected.
This doesn't return and errors when compiling and I can read though it step by step and I can't find the hole in it's logic.
Any assistance would be appreciated.
Thank you.