Need help sequencing using a momentary switch

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

I'd have to take a better, longer look to give real advice, but here's one thought about making your code prettier/better.

You have a lot of duplicated code in the if/else block.

I would do something like this:

int delay_list[] = {7000,9000,......}
int current_delay=0;
...
//after the bounce check
{
  myservo.attach(9);
  myservo.write(go);
  delay(delay_list[current_delay]);
  current_delay++;
  if (current_delay > WHATEVER_YOUR_MAX_IS)
  {
    current_delay = 0;
  }
}

Whoo hoo!! Thank you so much. It works!! Here's my working code. I threw in an LED for testing. I just added the servo stuff after I got the LED behaving like I wanted.
Thank you so much for the condensed code. Much easier to see the answer that way.


#include <Servo.h>

Servo myservo; // create servo object to control a servo

#define LED 13
#define BUTTON 2

int val = 0; // variable for reading the pin status
int bounceCheck = 0; // variable for debouncing
int cw = 0; // Servo cw value
int c = 0;
int delay_list[] = {1000,4000,1000};
int current_delay=0;

void setup() {
pinMode(BUTTON, INPUT); // declare pushbutton as input
pinMode(LED, OUTPUT); // declare LED as output
}

void loop(){

val = digitalRead(BUTTON); //read input value
delay(20); //wait 20ms
bounceCheck = digitalRead(BUTTON); //check again

if(val == bounceCheck){ //if val is the same then not a bounce
if (val == LOW) { //check if the input is LOW
} else {
digitalWrite(LED, HIGH);
myservo.attach(9);
myservo.write(cw);
delay(delay_list[current_delay]);
digitalWrite(LED, LOW);
myservo.detach();
current_delay++;
if (current_delay > 2)
{
current_delay = 0;
}
}

} //End if bounce check
} //end Loop


Thanks again!