Pages: [1]   Go Down
Author Topic: Button state not sticky  (Read 432 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello programming gurus! smiley-mr-green

I am and extreme novice, and I have been struggling with a button press behavior. What I am trying to accomplish seems simple to me, but the logic just isn't working the way I expect. When I press my button I want a certain set of LEDs to come on, and stay on. When pressed again, to revert to the starting set of LEDs. The behavior that it exhibits is I have my starting LEDs on, when I press the button it changes to the other set, and changes back when I release the button. I tried a number of different methods for saving the state of the button press to no avail. This is a stepping stone to adding a fade-in effect, but I can't get the button to work the way I expect. I have changed where the lastButtonState variable sits in the brackets to no avail. I'm sure this is something very simple to an experienced programmer, but I'm just learning at this point. Below if the Code:

Code:
// Used to control the warp and impulse engines
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int warpredPin =  6;       // the number of the LED pin
const int warpbluePin = 5;
const int impulseredPin = 11;
const int impulsegreenPin = 10;


// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin


void setup() {
  // initialize the LED pin as an output:
  pinMode(warpredPin, OUTPUT);
  pinMode(warpbluePin, OUTPUT);
  pinMode(impulseredPin, OUTPUT);
  pinMode(impulsegreenPin, OUTPUT);
 
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
  //digitalWrite(warpbluePin, HIGH);
  //digitalWrite(warpredPin, LOW);
  //digitalWrite(impulseredPin, LOW);
  //digitalWrite(impulsegreenPin, HIGH);
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  //compare the buttonState to its previous state
  if (buttonState != lastButtonState){


  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:   
    digitalWrite(warpredPin, HIGH);
    digitalWrite(warpbluePin, LOW);
    digitalWrite(impulsegreenPin, LOW);
    digitalWrite(impulseredPin, HIGH); 
  }
  else {
    // turn LED off:
    digitalWrite(warpbluePin, HIGH);
    digitalWrite(warpredPin, LOW);
    digitalWrite(impulsegreenPin, HIGH);
    digitalWrite(impulseredPin, LOW);
 
  }
 
}
 // save the current state as the last state
  // for the next time through the loop
  lastButtonState = buttonState;
}
 

Thanks in advance for the help!
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 108
Posts: 3785
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How about a latch.

Code:
boolean latch = false;
byte button_pin = 2;
byte button;
byte LED_pin = 13;

void setup() {
.
.
.
}

void loop() {

button = digitalRead(button_pin);
//NOTE: you will need to debounce button
if(button == HIGH) {
   latch = !latch;
   if(latch == true) {
      digitalWrite(LED_pin, HIGH);
   }
   else {
      digitalWrite(LED_pin, LOW);
   }
}
« Last Edit: February 14, 2013, 09:15:09 am by HazardsMind » Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

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

One thing that I find helps is to put the { on a new line. Use Tools + Auto Format to properly indent the code, if it gets out of whack, as yours is.
Code:
void loop()
{
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  //compare the buttonState to its previous state
  if (buttonState != lastButtonState)
  {
     // check if the pushbutton is pressed.
     // if it is, the buttonState is HIGH:
     if (buttonState == HIGH)
     {
     }
     else
     {
     }
  }
  lastButtonState = buttoneState;
}
is just easier to see the structure of.

Now, your code might not be the culprit at all. You are not using the internal pullup resistor which makes wiring switches trivial, so you must have an external pullup or pulldown resistor. How IS your switch wired?
Logged

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

Thanks for the quick reply guys! Yes I am wired direct to the 5v and Grnd with my button and a 1k resistor. Should this just be wired to pin2 and Grnd without the direct power?
Logged

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

Quote
Yes I am wired direct to the 5v and Grnd with my button and a 1k resistor.
So, we know what components you are using, but not how.

Quote
Should this just be wired to pin2 and Grnd without the direct power?
That is easier. Just turn on the pullup resistor, using digitalWrite(2, HIGH) after the pinMode() statement. Then, change HIGH to LOW to mean pressed.
Logged

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

I have not tried the pull-up resistor yet. Here is a drawing of my pro to layout:


Uploaded with ImageShack.us
Logged

Lost Wages
Offline Offline
Full Member
***
Karma: 11
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here are some observations:

Your green LED needs a current-limiting resistor

Your red LED is not connected to D11.  You need to run the wire to the other side of the center rail.

If I were you, I would use the internal pull-up resistor on D2 and have the button go from D2 to ground, thus eliminating the external resistor.
Logged

Pages: [1]   Go Up
Jump to: