I need help with States and buttons in my code

I have a 3 x 3 led matrix with my led pins 2-10 and a button on 11, and variable resistor on Analog 0. I am trying to fit 5 programs into one, and it switches between them on a button press. this is my code so far:

#include <Bounce.h>

int ledPins[] = {2,3,4,5,6,7,8,9,10};
int buttonPin = 11;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

void setup()
{
for(int i = 0; i < 9; i++){
pinMode(ledPins*, OUTPUT); }*

  • pinMode(buttonPin, INPUT);*
    }

void loop()
{

  • buttonState = digitalRead(buttonPin);*
  • if (buttonState == lastButtonState) {*
  • if (buttonState = HIGH) {*
  • buttonPushCounter++;*
    //program 1
  • } }*
  • else {*
    //program 2
  • }*
    }
    [/quote]
    my program will only switch to the second program when the button is held down, and i don't know how to add my other 3. Please help me!
    button_prototype.ino (2.01 KB)

To start, you may want to debounce your switch.

    if (buttonState = HIGH) {

Look up the difference between = and == in C.

Why bother comparing anything against lastButtonState if you are not going to update it?

  buttonState = digitalRead(buttonPin);
  if (buttonState == lastButtonState) {

Equals? You want to do something only if the switch is the same state it was in last time?

Most people want to do something when the state changes.

And, of course, it helps to eventually update lastButtonState.

Ok, I redid a bunch of things based on what I was told by you guys, and this is the result:

#include <Bounce.h>

int ledPins[] = {2,3,4,5,6,7,8,9,10};
int buttonPin = 11;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

void setup()
{
for(int i = 0; i < 9; i++){
pinMode(ledPins*, OUTPUT); }*

  • pinMode(buttonPin, INPUT);*
    }

void loop()
{

  • buttonState = digitalRead(buttonPin);*
  • digitalRead(lastButtonState);*
  • if (buttonState == lastButtonState) {*
  • buttonPushCounter++;*
  • }*
  • else {}*
  • if (buttonPushCounter == 1) {*
    //program 2
  • }*
  • else {*
    //program 1
  • }*
    }
    [/quote]
    and it still doe'n't work.
    I honestly don't know how to make all this work as I am noobish at programming.
    And Larry, I thought including the bounce library fixed that?

I know the IDE has a "Copy for Forum" option but we are trying to get that removed.

Please use code tags, not quotes, by just copying and pasting your sketch yourself, select all of it, and hit the "#" button over the posting box.

hornedCapybara:
Ok, I redid a bunch of things based on what I was told by you guys, and this is the result:

Except you ignored Paul and my posts...

And Larry, I thought including the bounce library fixed that?

You just included it; you didn't use it. You have to actually use the code provided by the library.

  else {}

Whoa.

There are programming errors and logic errors in your code, it won't compile and the layout does not help.
First you need to fix the setup function. ledPins is an array of pin numbers so which pin will be set to OUTPUT by this statement ?

    pinMode(ledPins, OUTPUT)

The setup should be like this. Note that I have changed the layout to make it more readable.

void setup()
{
  for(int i = 0; i < 9; i++)
  {
    pinMode(ledPins[i], OUTPUT);   
  }
  pinMode(buttonPin, INPUT);
}

Looking at the code in loop(), which pin is this statement going to read and where will it put the answer ?

digitalRead(lastButtonState);

The logic of what you want to do goes like this after setup has run :

LOOP
Read the current button state and remember it
Read the button state again and remember it with a different name
If the state has not changed do nothing, keep looking, go round the loop again
If it has changed (button has been pressed or released)
If it has been pressed (state now HIGH) remember the new state as the old state, go round the loop again
Else (we now know that it has just been released)
Remember the new state as the old state
Increment the button press counter
If the button press counter has gone beyond the maximum reset it to zero
Test the button press counter
If zero call programzero()
If one call programone()
If two call programtwo()
etc, etc
KEEP GOING ROUND THE LOOP

NOTE - there is no debouncing unless you expressly do it when reading the button

Hi!
if i got the target you're trying to reach...
You want to switch among five choices, using a button.
I wrote down the following sketch, providing a second pushbutton (startButton), whose aim is to start the acquisition time for data from the other switch (commandButton).
In this way you tell arduino "pay attention i'm sending you the number of the program i want you to run, and i will tell you that number, by the number of clicks i will perform on the commandButton."

here is the sketch:

int startButton=12;
int commandButton=11;
int i=0;
int count;
int program;
int state=0;
int state2=0;
int prevState=0;
int prevState2=0;

long prevDebTime;
long prevDebTime2;
long debDelay=200;

void setup(){
  Serial.begin(9600);
  pinMode(startButton,INPUT);
  digitalWrite(startButton,HIGH);
  pinMode(commandButton, INPUT);
  digitalWrite(commandButton,HIGH);
}

void loop(){
  state=!digitalRead(startButton);
  if((millis()-prevDebTime)>debDelay){
   if (state!=prevState && state==HIGH) {
     if(i==0){
    i=1;
    prevDebTime=millis();
  }
  else if(i==1){
    i=0;
    Serial.println("Program number: ");
    Serial.println(count);
    program=count;
    switch(program){
       case 1:
       //insert program 1 commands
       break;
  
       case 2:
       //insert program 2 commands
       break;
       
       //and so on....
    } 
    count=0;
    prevDebTime=millis();
}
   }

}
if(i==1){
  state2=!digitalRead(commandButton);
  if((millis()-prevDebTime2)>debDelay){
    if(state2!=prevState2 && state2==HIGH){
      count++;
      prevDebTime2=millis();
    }
  }
}
}

you have to fill the switch function with the actions your led matrix is expected to show.
the pushbuttons are already debuonced.

hope this could help you!
And I apologize for my English!...

Well i tested it and for the stated purpose, it works...
can i have a feedback by you, guys?
what do you think about?