Single, double, triple press button to control LED - control problem

Hi all,
I need some one to help me to solve this problem since i'm stuck for a while.
I'm writing a program to control 3 LEDs with a single press button.
By pressing once --> Green LED is ON
By pressing twice ---> Red LED is ON
By pressing thrice ---> Blue LED is ON
ect.

I found this good tutorial. However, it didn't work for me when putting the 2 parts together. I'm a new bee. Can any one please have a quick look to see whether they are at the appropriate position. The program is successfully complied however, there is no response on neither LEDs OR press Button when running the program.

Thanks

#define buttonPin 28 // analog input pin to use as a digital input on Arduino Uno board A5 pin
#define ledPin1 12 // digital output pin for LED 1
#define ledPin2 13 // digital output pin for LED 2
#define ledPin3 15 // digital output pin for LED 3
#define ledPin4 14 // digital output pin for LED 4

// LED variables
boolean ledVal1 = false; // state of LED 1
boolean ledVal2 = false; // state of LED 2
boolean ledVal3 = false; // state of LED 3
boolean ledVal4 = false; // state of LED 4

//=================================================
// Button timing variables
int debounce = 20; // ms debounce period to prevent flickering when pressing or releasing the button
int DCgap = 250; // max ms between clicks for a double click event
int holdTime = 2000; // ms hold period: how long to wait for press+hold event
int longHoldTime = 5000; // ms long hold period: how long to wait for press+hold event

// Other button variables
boolean buttonVal = HIGH; // value read from button
boolean buttonLast = HIGH; // buffered value of the button's previous state
boolean DCwaiting = false; // whether we're waiting for a double click (down)
boolean DConUp = false; // whether to register a double click on next release, or whether to wait and click
boolean singleOK = true; // whether it's OK to do a single click
long downTime = -1; // time the button was pressed down
long upTime = -1; // time the button was released
boolean ignoreUp = false; // whether to ignore the button release because the click+hold was triggered
boolean waitForUp = false; // when held, whether to wait for the up event
boolean holdEventPast = false; // whether or not the hold event happened already
boolean longHoldEventPast = false;// whether or not the long hold event happened already

int checkButton()
{ 
int event = 0;
// Read the state of the button
buttonVal = digitalRead(buttonPin);
// Button pressed down
if (buttonVal == LOW && buttonLast == HIGH && (millis() - upTime) > debounce) {
downTime = millis();
ignoreUp = false;
waitForUp = false;
singleOK = true;
holdEventPast = false;
longHoldEventPast = false;
if ((millis()-upTime) < DCgap && DConUp == false && DCwaiting == true) DConUp = true;
else DConUp = false;
DCwaiting = false;
}
// Button released
else if (buttonVal == HIGH && buttonLast == LOW && (millis() - downTime) > debounce) { 
if (not ignoreUp) {
upTime = millis();
if (DConUp == false) DCwaiting = true;
else {
event = 2;
DConUp = false;
DCwaiting = false;
singleOK = false;
}
}
}
// Test for normal click event: DCgap expired
if ( buttonVal == HIGH && (millis()-upTime) >= DCgap && DCwaiting == true && DConUp == false && singleOK == true) {
event = 1;
DCwaiting = false;
}
// Test for hold
if (buttonVal == LOW && (millis() - downTime) >= holdTime) {
// Trigger "normal" hold
if (not holdEventPast) {
event = 3;
waitForUp = true;
ignoreUp = true;
DConUp = false;
DCwaiting = false;
//downTime = millis();
holdEventPast = true;
}
// Trigger "long" hold
if ((millis() - downTime) >= longHoldTime) {
if (not longHoldEventPast) {
event = 4;
longHoldEventPast = true;
}
}
}
buttonLast = buttonVal;
return event;
}

void setup() 
{
// Set button input pin
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH );
// Set LED output pins
pinMode(ledPin1, OUTPUT);
digitalWrite(ledPin1, ledVal1);
pinMode(ledPin2, OUTPUT);
digitalWrite(ledPin2, ledVal2);
pinMode(ledPin3, OUTPUT); 
digitalWrite(ledPin3, ledVal3);
pinMode(ledPin4, OUTPUT); 
digitalWrite(ledPin4, ledVal4);
Serial.begin(9600);
}

void loop()
{
// Get button event and act accordingly
int b = checkButton();
if (b == 1) clickEvent();
if (b == 2) doubleClickEvent();
if (b == 3) holdEvent();
if (b == 4) longHoldEvent();
}

//=================================================
// Events to trigger by click and press+hold

void clickEvent() {
ledVal1 = !ledVal1;
digitalWrite(ledPin1, ledVal1);
Serial.print("i'm here 1");
}

void doubleClickEvent() {
ledVal2 = !ledVal2;
digitalWrite(ledPin2, ledVal2);
Serial.print("i'm here 2");
}

void holdEvent() {
ledVal3 = !ledVal3;
digitalWrite(ledPin3, ledVal3);
}

void longHoldEvent() {
ledVal4 = !ledVal4;
digitalWrite(ledPin4, ledVal4);
}

Which board are you using? Which pin have you attached the pushbutton switch?

#define buttonPin 28 // analog input pin to use as a digital input on Arduino Uno board A5 pin

I'm using Arduino Uno ATmega 328 chip.
The pushbotton switch was plugged into pin #28 as labeled A5 on board via 10Kohm register.
Is it correct when i put the 2 parts together according to the tutorial?

Thanks

arduno_dude:
I'm using Arduino Uno ATmega 328 chip.
The pushbotton switch was plugged into pin #28 as labeled A5 on board via 10Kohm register.
Is it correct when i put the 2 parts together according to the tutorial?

Thanks

I know that this can be a bit confusing but A5 is pin19. You want what the website posted, below.

Once you get that corrected, try again and we shall see if that is your issue. To me it looks like you put the code together correctly.

#define buttonPin 19 // analog input pin to use as a digital input on Arduino Uno board A5 pin

I know that this can be a bit confusing but A5 is pin19. You want what the website posted, below.

pin 19, Is it the port # that has a pushbutton? If i change to 19, will my pushbutton wire staying at the same place as at port A5 ?

It is because the Arduino Uno Atmel 328 layout pin saying A5 is #28. May be analog pin is different.
Please educate

Thanks

IC pin 28 is Arduino "A5".

In void setup ()
pinMode(A5, OUTPUT);

then use
digitalWrite(A5, HIGH);
digitalWrite(A5, LOW);
as needed.

Write a simple sketch to verify that, blink with A5.

Likewise using it as an input.

pinMode (A5, INPUT);

val = digitalRead(A5);

Verify that with a sketch that generates a message in Serial Monitor upon a switch closure.

right, and for digital

A5 == 19

two ways of referring to the same pin.

Hi all,

Thanks for your advises. I tried with different values: 19, 28 and A5 on the buttonPin. None of them works.
I attached the circuit for you to have a look.
Please let me know if you spot an issue.

Thanks

photo.JPG

arduno_dude:
I attached the circuit for you to have a look.
Please let me know if you spot an issue.

Insufficient size to see much.

agree, I couldn't even figure out how the white led was powered. Also couldn't see the resistor for the red LED.

Sorry about that.
The pushbotton works on other programs.
The Red LED is hooked up properly.
The White LED is not shown clearly. However, i used SerialPrint to see program stages.

Here is an example of a menu. you can modify it to turn on LEDs

// Button_Menu 09 June 2012 JDS
// For Switch with pull down reststor
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 20 chars and 4 line display
byte count = 1;
byte x = 0;
byte xx = 0;
byte key = 10; //key pin
byte led = 6; //red and green leds
long int Timer;

void setup() {
  pinMode(led, OUTPUT);
  lcd.init();                      // initialize the lcd
  delay(100);
  lcd.display();
  lcd.clear();
  lcd.home();
  lcd.noCursor();
  lcd.noAutoscroll();
  lcd.backlight();
  delay(100);
  lcd.setCursor(2, 0);
  lcd.print("Button Menu");
  //Serial.begin(9600);
}

void loop() {
  lcd.setCursor(0, 1);
  if (count == 1)
  {lcd.print(count); lcd.print("  First Selection  ");}
  if (count == 2)
  {lcd.print(count); lcd.print("  Second Selection  ");}
  if (count == 3)
  {lcd.print(count); lcd.print("  Third Selection  ");}
  if (count == 4)
  {lcd.print(count); lcd.print("  Forth Selection  ");}
  x = digitalRead(key);
  if (x == 0)
  {
    xx = 0;
    digitalWrite(led, x);
  }
  if (x == 1)
  { Timer = 50000;
    while (Timer > 0)
    {
      Timer--;
    }
    x = digitalRead(key);
    if (x == 1)
    { if (xx == 0)
      { count++;
      if (count > 4)
      {count = 1;}
        digitalWrite(led, x);
        xx = 1;
      }
    }
  }
}

A bit crude, but it works.

It leads to a different situation.
I prefer to trouble shout my current issue to make it works
Thanks

so where does the white LED get its power from? I cannot tell from the photo.

The Red LED is hooked up properly.

Do you think you burned out the red LED by powering it with 5V directly?

The white LED is unhooked during the test.
I mainly used SerialPrint window to see where i'm up to within the program, and checked on red LED for physical response. I got no response at all from push button on neither red LED and Serial print window

With the attached circuit

this sketch

// A5Digin
// A5 as digital input
//

byte onbdLED = 13;
byte sw_pin = A5;
byte sw_state;

void setup()
{
  pinMode(onbdLED, OUTPUT);
  pinMode(sw_pin, INPUT);
}

void loop ()
{
  sw_state = digitalRead(sw_pin);
  if(sw_state == 1)
  {
    digitalWrite(onbdLED,HIGH);
  }
  else
  {
    digitalWrite(onbdLED,LOW);
  }
}

will verify your switch circuit.
It uses the on board LED (13).
[Sketch Verified.]

IMG_1706.jpg

Yeah, the switch circuit works fine.

The double click program still does not work ]:smiley:

At first, the switch input was in question.
So, I guess that's been put to bed.

Now, it's not a matter where after one it's Red, and then after another (now two) it's Green, and then after another (now three) it's Blue.
Is it that you want to find and differentiate a single-click,from a double-click, from a triple-click?