Pages: [1]   Go Down
Author Topic: I need help with States and buttons in my code  (Read 938 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Quote
#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
  }
}


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 - downloaded 15 times.)
Logged

Offline Offline
Faraday Member
**
Karma: 67
Posts: 2700
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

To start, you may want to debounce your switch.
Logged

The way you have it in your schematic isn't the same as how you have it wired up!

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18807
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    if (buttonState = HIGH) {

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


California
Online Online
Faraday Member
**
Karma: 88
Posts: 3382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, I redid a bunch of things based on what I was told by you guys, and this is the result:
Quote
#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
  }
}

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?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18807
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


California
Online Online
Faraday Member
**
Karma: 88
Posts: 3382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...

Quote
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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18807
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  else {}

Whoa.
Logged


East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4261
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?

Code:
    pinMode(ledPins, OUTPUT)

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

Code:
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 ?

Code:
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

Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Casorezzo, Milan, Italy
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
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!...
« Last Edit: January 10, 2013, 06:33:57 pm by gianlucalongoni » Logged

Casorezzo, Milan, Italy
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: