All,
I have an Arduino Diecimila. I have a momentary switch on pin 2.
I have a servo modified for continuous rotation. The program needs to turn the servo for as period of time, which translates to some number of rotations, each time the button is pressed. Each press results in a different set amount of turns.
Example: exact accuracy isn't that imporant. Don't need an encoder wheel
Press #1 - Rotate 5 times
Press #2 - Rotate 10 times
Press #3 - Rotate 15 times
Press #4 - Rotate 5 times
Press #5 - Rotate in reverse 35 times (reset)
Startover
I'm having a problem with the program logic or possibly approach to solving the problem.
Here is my code as it stands. It doesn't work predictably.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int inputPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
int bounceCheck = 0; // variable for debouncing
int go = 0; // Servo go value
int c = 0;
int i = 0;
void setup() {
pinMode(inputPin, INPUT); // declare pushbutton as input
// myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop(){
val = digitalRead(inputPin); //read input value
delay(20); //wait 10ms
bounceCheck = digitalRead(inputPin); //check again
if(val == bounceCheck){ //if val is the same then not a bounce
if (val == HIGH) { //check if the input is HIGH
myservo.detach();
} else {
if (c == 1)
{
myservo.attach(9);
myservo.write(go);
delay(7000);
myservo.detach();
c++;
}
else if (c == 2)
{
myservo.attach(9);
myservo.write(go);
delay(2000);
myservo.detach();
c++;
}
else if (c == 3)
{
myservo.attach(9);
myservo.write(go);
delay(9000);
myservo.detach();
c++;
}
{
myservo.detach();
}
myservo.detach();
} //End if not bounce
} //End if button HIGH
} //end loop
Any help would be greatly appreciated.
James