Mode select button help

Hi all,

Im working on a project and have hit a wall and unfortunately dont have the experience with writing code to figure the next part out and at this point of frustration and confusion im ready to admit temporary defeat and ask for help. I need to add a mode select single push button to change through 3 maybe 4 programmes, adding LEDs to indicate the change in programme would be cool but not essential, im using arduino uno and a l298n motor shield to a servo all running of 7.4v lipo battery, any help would be gratefully appreciated.

The code so far

int pinpwm = 13;//motor pwm channel
int pindirC = 7;//motor direction clockwise
int pindirAC = 8;
int spead =10000;//motor speed

void setup()
{
pinMode(pinpwm,OUTPUT);
pinMode(pindirC,OUTPUT);
pinMode(pindirAC,OUTPUT);

}
void forward()
{
digitalWrite(pindirC, HIGH);
digitalWrite(pindirAC, LOW);
analogWrite(pinpwm,spead);

}
void backward()
{
digitalWrite(pindirC, LOW);
digitalWrite(pindirAC, HIGH);
analogWrite(pinpwm,spead);

}
void stop()//
{

digitalWrite(pindirC, LOW);
digitalWrite(pindirAC, LOW);
delay(1000);
}
void loop()
{
delay(0);

forward();
delay(200);
stop();

forward();
delay(170);
stop();

forward();
delay(130);
stop();

forward();
delay(100);
stop();

forward();
delay(80);
stop();
delay(3000);

backward();
delay(500);
stop();

delay(3000);
//loop delay(0);

}

I don't see where you tried to add the button or mode code.

Add a switch, debounce, increment a counter, use the counter to control the sketch mode/LED status.

Please use code tags. Use the </> icon in the posting menu. [code] Paste sketch here. [/code]

Is this homework?

.

Some ideas to maybe help get you started:

int BUTTON = 2;  //connect your button to pin 2
bool BUTTON_PRESSED;
int PROG_COUNT = 0;

pinMode(BUTTON,INPUT_PULLUP)  //button connected to Vcc when not pressed

if !BUTTON && !BUTTON_PRESSED  //BUTTON LOW when pressed.  if button is already pressed, ignore this if statement
{
    PROG_COUNT ++1;  // increment program selection
    BUTTON_PRESSED = TRUE;  //button is pressed
}

if BUTTON //button HIGH when released
{
    BUTTON_PRESSED = 0;  //button is no longer pressed
}

if PROG_COUNT == 1 
{
    prog1();
}
elsif PROG_COUNT ==2
{
    prog2();
}

etc...

P.S. I'm a few cups in, so I wouldn't doubt a few style/logical mistakes. Like I said, just some ideas to give you a nudge.