Buttonpress switch mode HELP

I am new to this whole arduino world and was wondering if i could get some help with a project im working on.

int ledPins[] = {2,3,4,5,6,7,8,9,10,11,12,13};
int Potpin = 0;
int Potread = 0;
int ButtonPin = 0; //all the different pins/variables
int ButtonState = LOW;
int oldButtonState = LOW;
int delayTime;
int mode;

void setup()
{
for (int i = 0; i < 9; i++){
pinMode(ledPins*, OUTPUT); //LEDs all Output*

  • {*
  • pinMode(Potpin, INPUT);*
  • pinMode(ButtonPin, INPUT); //Pot and button are input*
  • {*
    _ Serial.begin(9600); //serial for possible debugging_
  • }*
  • }*
    }
    }
    void loop()
    {
  • Potread = analogRead(Potpin);*
    _ delayTime = Potread * 5;_
  • ButtonState = digitalRead(ButtonPin); //new data and delaytime*
    [/quote]
    that is what i have so far. I'd like some insight on using a simple pushbutton to change modes.
    I am making an LED show where a press changes mode and a pot changes tempo/speed.
    any help would be appreciated.

Bad idea to use pins 0 or 1 for anything as they are serial coms to the board... Intererence on those just after a reset or when programming could / will cause erratic behaviour.

Thanks for the quick reply. Ya, i noticed that problem and fixed it. if you unplug, load, and replug it works fine. I looked at the debounce schetch but i dont get it. I want to put it in my code so each button press with change from 0-1 1-2 2-3 3-0 or such.

Step1 - listen to Greymelkin and don't use pins 0 and 1.

You should definately take a look at the Analogue in tutorial

(Especially the code)

Make sure that you have the LED's connected up properly (with resistors 220 ohm). You may want to look at the this code also- http://arduino.cc/en/Tutorial/BarGraph

I'd be inclined to use a switch case statement to go through the different states that the leds are in; it may also be good to look at some functions...
eg...

switch(state) {
  case 0: {
     led_chaser();
     break;
     }

  case 1: {
     fader();
     break;
     }

  default: {
    state = 0;
    break;
    }
}

see http://arduino.cc/en/Tutorial/SwitchCase
-- Code in particular...

and last but not least, put a closing "}" at the end of loop()

:slight_smile:

Ok, ill try to move my pins. One problem is pins 2-13 are taken, can you use analog pins as digital pins?
Plus, i want the pushbutton to change states. The switchcase is a good idea and i know how to use analog read. What i want to know is how to inplement pushbuttons to change the cases in the switchcase. Similar to the debounce tutorial and such.
Sorry for the specifics. :slight_smile:

Debouncing is a problem where the little bits of wire bounce around a bit before resting together in the push button; a lot like droping a ball on the ground.

For something that works as fast as a microcontroller, this is quite obvious, looking something like this:

1 1 0 0 0 0 1 1 0 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1...

So what you need to do is when you first read a HIGH, wait for a little while before doing anything else (otherwise it may register as several button pushes...)

Another problem which comes with any input is that it could just be a voltage spike from something as simple as a lightswitch being turned on (inpulse noise). To get around this, what you need to do is check to see if the button is still being pressed. To do this all you have to do is check if the input is still HIGH after a delay.

eg...
while(digitalRead(ButtonPin)) {
delay(40);
while(digitalRead(ButtonPin)) {
//button is on...
//set button flag to on
}
}

It is not just debouncing. How do i make the code change "modes" each time i push a button. debouncing is a separate issue.

And to answer your question about switching states...

first of all, for simplicity, let's assume that the device is turned off for a brief amount of time when the button is pushed, waiting for the button to be released...

The code in the loop then becomes

while(digitalRead(ButtonPin)) {
delay(40);
while(digitalRead(ButtonPin)) {
//button is on...
//set button flag to on
while(digitalRead(ButtonPin)) {/Do Nothing/}
}
}
switch(state) {
case 0: {
led_chaser();
if(!button_flag) break;
state++;

break; //you can take this out if you want

}

case 1: {
fader();
if(!button_flag) break;
button_flag = 0;
state++; //

break; //you can take this out if you want

}

default: {
state = 0;

break; //you can take this out if you want
}
}

and for the bouncing you can use the bounce library. Very helpfull and easy to use

http://www.arduino.cc/playground/Code/Bounce

i dont get it. How to the states change. i want it so if mode is 0, off, and i push the button i go to mode 1. same to 2, same 3, and so on.
i am new to all this and this is my first at all somewhat complex project.

You may also be interested in this...
http://www.arduino.cc/playground/Code/FiniteStateMachine

I haven't used it, but it will do what you want.

:slight_smile:

something like this is what i am going for. Without the led case though.
Plus, a speed control. (simple analog read delaytime)

btw, i like the idea of code writing without many libraries because then i have more control and will not always be dependant on them in the future.

So ok...

first of all, the button needs to be pushed and let go...

//See if pushed and held
while(digitalRead(ButtonPin)) {
  delay(40);
  while(digitalRead(ButtonPin)) {

    //button is on...
    //set button flag to on
    button_flag = 1; //this is just something I made up.
                            //If you want to use this flag go with 
                            //something like...
                            //uint8_t button_flag = 0

  //this next bit stops and waits until the button is released...
  while(digitalRead(ButtonPin)) {/*Do Nothing*/}
}

The "state machine" is what ever you want it to be.

switch(state) {
 case 0: {
    /*Do nothing, or do something: Your choice*/

    /*but when you see the flag go to 1
    increment the state so next time
    you are in state '1'*/

    //ie if button == low, exit switch statement
    if(!button_flag) break;

    // otherwise increment state
    state++;

    //this exits the switch statement, but the state
    //below is where it will end up, so you don't have 
   // to exit if you don't want to...
    break; //you can take this out if you want

    }

 case 1: {
    fader();
    if(!button_flag) break;
    button_flag = 0;
    state++; //

    break; //you can take this out if you want

    }

 //this is where it goes when it doesn't have a case for the value
 default: {
   state = 0; //I like to tell it where to go.

   break; //you can take this out if you want
   }
}

ok, thanks for all the help, if i need more help i may add to this post. I do wonder how the "digitalRead(buttonPin)
delay(40)
digitalRead(buttonPin)
works to see if button is pressed. :wink:

[edit]

switch(state) {
 case 0: {
    /*Do nothing, or do something: Your choice*/

    /*but when you see the flag go to 1
    increment the state so next time
    you are in state '1'*/

    //ie if button == low, exit switch statement
    if(!button_flag) break;
    
    [glow]//Clear the flag so that button is 
    //no longer "on"
    button_flag = 0;[/glow]
    // otherwise increment state
    state++;

    //this exits the switch statement, but the state
    //below is where it will end up, so you don't have
   // to exit if you don't want to...
    break; //you can take this out if you want

    }

 case 1: {
    fader();
    if(!button_flag) break;
    button_flag = 0;
    state++; //

    break; //you can take this out if you want

    }

 //this is where it goes when it doesn't have a case for the value
 default: {
   state = 0; //I like to tell it where to go.

   break; //you can take this out if you want
   }
}

[/edit]

and get rid of this, the while loop is already doing it.

[edit]

//See if pushed and held
while(digitalRead(ButtonPin)) {
  delay(40);
  while(digitalRead(ButtonPin)) {

    //button is on...
    //set button flag to on
    button_flag = 1; //this is just something I made up.
                            //If you want to use this flag go with
                            //something like...
                            //uint8_t button_flag = 0

  [highlight[s]]//this next bit stops and waits until the button is released...
  while(digitalRead(ButtonPin)) {/*Do Nothing*/}[/s][/glow]}

[/edit]

int ledPins[] = {2,3,4,5,6,7,8,9,10,11,12,13};
int Potpin = 0;
int Potread = 0;
int ButtonPin = 0; //all the different pins/variables
int Buttonread1 = LOW;
int Buttonread2 = LOW;
int ButtonState = 0;
int delayTime;
int mode;

void setup()
{
for (int i = 0; i < 9; i++){
pinMode(ledPins*, OUTPUT); //LEDs all Output*

  • {*

  • pinMode(Potpin, INPUT);*

  • pinMode(ButtonPin, INPUT); //Pot and button are input*

  • {*
    _ Serial.begin(9600); //serial for possible debugging_

  • }*

  • }*
    }
    }
    void loop()
    {

  • Buttonread1 = digitalRead(ButtonPin); //new data and delaytime*

  • delay(40);*

  • Buttonread2 = digitalRead(ButtonPin);*

  • while(Buttonread1 == HIGH && Buttonread1 == Buttonread2) {*

  • ButtonState = ButtonState + 1;*

  • }*
    }
    [/quote]
    will this work to figure out state?

try again...

[edit]

//See if pushed and held
while(digitalRead(ButtonPin)) {
  delay(40);
  while(digitalRead(ButtonPin)) {

    //button is on...
    //set button flag to on
    button_flag = 1; //this is just something I made up.
                            //If you want to use this flag go with
                            //something like...
                            //uint8_t button_flag = 0
[glow]
  //this next bit stops and waits until the button is released...
  while(digitalRead(ButtonPin)) {/*Do Nothing*/}[/glow]
}

[/edit]