Multiple functions one button

Hello im pretty new to this and have been playing round for a few weeks now. Ive got stuck though try to make a single button do multiple functions.

So on the first click it would do one function.

The next click another

the next another unique function

and say on the fourth click back to the first function.

Would be very greatful for any help.

Why can't you have arduino count the number of times your button does, say take a digital line low or high?

The usual thing to do when you get stuck is post your code.

Post your code, but ultimately what you want to code is a state machine that changes state every time you press the button (you'll need to debounce the button, too - either externally with hardware, or on the Arduino with software).

So this is what ive got so far. I goes though all the lights and stops when you press the button. Id like to get it to change light every time the button is pressed

#include <Button.h>

/*
create a Button object at pin 12
connect button between pin 12 and GND
*/
Button button = Button(12,PULLDOWN);

void setup(){
pinMode(8,OUTPUT); //debug to led 13
pinMode(9,OUTPUT);
pinMode(7,OUTPUT);
}

void loop(){

if (button.isPressed()){

digitalWrite(7,HIGH);
digitalWrite(9,LOW);

delay(1000);

}

if (button.isPressed() && button.wasPressed()){

digitalWrite(8,HIGH);
digitalWrite(7,LOW);
delay(1000);

}

if (button.isPressed() && button.wasPressed() && button.isPressed()){

digitalWrite(9,HIGH);
digitalWrite(8,LOW);

delay(1000);
}
}

if (button.isPressed() && button.wasPressed())

This tests whether the button is being held down.

if (button.isPressed() && button.wasPressed() && button.isPressed())

This is doing exactly the same thing.

Are you trying to change which LED is lit every time the switch is pressed, or are you trying to change which LED is lit only when the switch is held down?

change which LED is lit every time the switch gets pressed

You probably want to look at the uniquePress() method, then.

Ok this is abit more like it, only problem now is that it doesnt run in a smooth cycle and sometimes multiple lights come on at once

#include <Button.h>

/*
create a Button object at pin 12
connect button between pin 12 and GND
*/
Button button = Button(12,PULLDOWN);

void setup(){
pinMode(8,OUTPUT); //debug to led 13
pinMode(9,OUTPUT);
pinMode(7,OUTPUT);
}

void loop(){

if (button.uniquePress()){

digitalWrite(7,HIGH);
digitalWrite(9,LOW);
}{

if (button.uniquePress()){

digitalWrite(8,HIGH);
digitalWrite(7,LOW);
}{

if (button.uniquePress()){

digitalWrite(9,HIGH);
digitalWrite(8,LOW);

}

}
}
}

You want to call the uniquePress function once in each pass through loop. If the function returns true, increment a counter.

Make the lights light up, and reset the counter, based on the value of the counter.

byte pressCount = 0;

void loop()
{
   if(button.uniquePress())
      pressCount++;

   switch(pressCount)
   {
       case 1:
          // Turn off all LEDs. Turn on 7
          break;
      case 2:
          // Turn off all LEDs. Turn on 8
          break;
      case 3:
          // Turn off all LEDs. Turn on 9
          pressCount = 0;
          break;
    }
}

that works great,

Thanks a lot for the help

heres what i ended up with. I no its abit of a mess, but as long as it works right

#include <Button.h>

/*
create a Button object at pin 12
connect button between pin 12 and GND
*/
Button button = Button(12,PULLDOWN);

void setup(){
pinMode(8,OUTPUT); //debug to led 13
pinMode(9,OUTPUT);
pinMode(7,OUTPUT);

}
byte pressCount = 0;
void loop(){

if (button.uniquePress())
pressCount++;

switch(pressCount)

{case 1:

digitalWrite(7,HIGH);
digitalWrite(9,LOW);
digitalWrite(8,LOW);

break;

case 2:

digitalWrite(8,HIGH);
digitalWrite(7,LOW);
digitalWrite(6,LOW);

break;
case 3:

digitalWrite(9,HIGH);
digitalWrite(8,LOW);
digitalWrite(7,LOW);

pressCount = 0;
break;

}

}