Go Down

Topic: LED's are not responding to button. (Read 962 times) previous topic - next topic

ebird97

I wrote a code to control 3 LED's that act like a stop light. The stoplight function works, however the button pressing does not turn it on or off. It simply stays on as long as the Arduino board is powered. Here is the code. I used to button function from the code in a "Getting Started with Arduino", book.

/* This is a simultion of a stop light.
Connect the color of LED specified by the define method into the pin # after
the type of color.
*/

#define green 3 //defines green LED
#define yellow 4 //defines yellow LED
#define red 5 //defines red LED
#define button 12 //defines button used to turn sequence on or off

int val = 0;
int old_val = 0;
int state = 0;

unsigned int minute = 60000;
int ten_seconds = 10000;

void setup()
{
  pinMode(green, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(red, OUTPUT);
  pinMode(button, INPUT);
}

void loop ()
{
val = digitalRead(button);

if ((val == HIGH) && (old_val == LOW))
{
    state = 1 - state;
    delay(10);
}

old_val = val;

if(state == 1)
{

digitalWrite(green,HIGH);
delay(minute);
digitalWrite(green,LOW);

digitalWrite(yellow,HIGH);
delay(ten_seconds);
digitalWrite(yellow,LOW);

digitalWrite(red,HIGH);
delay(minute);
digitalWrite(red,LOW);
}

else
{
  digitalWrite(green,LOW);
  digitalWrite(yellow,LOW);
  digitalWrite(red,LOW);
}
}

AndrewStone

How is your button hooked up?  Should be 5v->button->pin->resistor->ground.

Cheers!
Andrew
http://www.toastedcircuits.com Lightuino LED driver: 16 sources, 70 sinks, remote controlled.  Also high powered LED drivers.

Grumpy_Mike

I disagree buttons should always be wired:-
5v -> resistor -> input pin -> button -> ground
Then a press returns a low and release returns a high. 
You can even omit the resistor if you enable the internal pull up resistors.

When posting code select the code and then hit the # icon, this puts it in a nice scrolling box and doesn't scramble some code statements.

AndrewStone

@grumpy_mike:  I did not mean "should" in the sense of general advocacy.  In fact I prefer active low to take advantage of the internal pull ups.  I meant "should" as in works with his specific sketch. 

For the general reader, here is an active low, debounced, mono-stable emulates bi-stable, 3 button routine.  It should be polled.  For simplicity in the debounce logic, it only registers one button at a time (easy to fix if you need different behavior):

Code: [Select]

//   Define button handling details
enum
{
  BUTTON0_PIN = 2,  // The Arduino digital pins that the button is connected to.
  BUTTON1_PIN = 8,
  BUTTON2_PIN = 9,
};

byte buttonState=0;  // Button state ends up in here (1 = on, 0 = off)
byte debounce=0;

void handleButtons(void)
{
  byte val;

  val = digitalRead(BUTTON0_PIN);
  val |= digitalRead(BUTTON1_PIN) << 1;
  val |= digitalRead(BUTTON2_PIN) << 2;

// Bistable logic
 
  val = (~val) & 7;  // buttons are active-low (pressed = 0)

  if ((!val)&&(debounce)) // After it is let go, we wait
  {
    debounce--;
  }
 
  if (val && !debounce)  // Ok pressed and not debouncing
  {   
    int r,g,b;
    buttonState ^= val;
    debounce = 20;  // How long to debounce.  The exact time depends on how often you call this routine; 20 is good if you call it every millisec or so.
   
    if (buttonState&1)
      {               
         ButtonOn(1);  // You implement to do what you want when the switch is "on".
      }
    else ButtonOff(1); // You implement to do what you want when the switch is "off".
    if (buttonState&2)
      {               
         ButtonOn(2);
      }
    else ButtonOff(2);
    if (buttonState&4)
      {               
         ButtonOn(3);
      }
    else ButtonOff(3);
  }
}
http://www.toastedcircuits.com Lightuino LED driver: 16 sources, 70 sinks, remote controlled.  Also high powered LED drivers.

GuitarBuilder

Andrew,

I tried your 3 button routine, but it will not compile - error : 'ButtonOn' was not declared in this scope.  Did I forget something?

Cheers,

Peter

AndrewStone

No you are supposed to provide the functions ButtonOn(int buttonNumber) and ButtonOff(int buttonNumber).  I call these functions to notify you that there is a button event.

If you want just implement a "no-op" like:

void ButtonOn(int buttonNumber)
{
  Serial.print("button on");
}

Cheers!
Andrew
http://www.toastedcircuits.com Lightuino LED driver: 16 sources, 70 sinks, remote controlled.  Also high powered LED drivers.

Go Up