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);
}
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?
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
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.
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.
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
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?