Pages: [1] 2   Go Down
Author Topic: Single, double, triple press button to control LED - control problem  (Read 795 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

http://jmsarduino.blogspot.ca/2009/10/4-way-button-click-double-click-hold.html

Code:
#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);
}
« Last Edit: July 03, 2014, 07:22:06 am by arduno_dude » Logged

Online Online
God Member
*****
Karma: 53
Posts: 977
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

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
Logged

Online Online
God Member
*****
Karma: 53
Posts: 977
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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

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

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

Offline Offline
Faraday Member
**
Karma: 66
Posts: 2568
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: July 03, 2014, 09:49:25 am by Runaway Pancake » Logged

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

Offline Offline
Faraday Member
**
Karma: 66
Posts: 2568
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

Online Online
God Member
*****
Karma: 53
Posts: 977
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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 (38.74 KB, 320x240 - viewed 18 times.)
Logged

Offline Offline
Faraday Member
**
Karma: 66
Posts: 2568
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Insufficient size to see much.
Logged

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

Online Online
God Member
*****
Karma: 53
Posts: 977
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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.


* photo (1).JPG (2671.99 KB, 3264x2448 - viewed 20 times.)
Logged

Shelby Township, Disco, Michigan
Offline Offline
God Member
*****
Karma: 26
Posts: 766
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is an example of a menu. you can modify it to turn on LEDs
Code:
// 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.
« Last Edit: July 04, 2014, 10:53:53 am by steinie44 » Logged

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

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

Online Online
God Member
*****
Karma: 53
Posts: 977
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Quote
The Red LED is hooked up properly.

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

Logged

Pages: [1] 2   Go Up
Jump to: