hey there I have a particular problem. I have a pushbutton switch (4 pins little black ones)
I have programmed it with an lcd to create a menu system
now, here's what I want it to do
I push button one time and list of items how up for ex:
button push and release and this displays:
fire
ice
wind
earth
then push and release again and this displays:
fire
my problem is here where I should push again and it should say
ice
it should toggle through menu items but I can only make it exist in two states.
how can I make it toggle through menu items in 4 states?
ps. I do not want to long press or slow press just regular press.
If you have programmed this as most people do, you only test to see if the switch is pressed or the switch is not pressed.
Your project requires you to know the instant the switch changes from not-pressed to pressed and possibly from pressed to not pressed. See the difference?
To know exactly when the switch changes, you need to remember what the switch position was before it was pressed or before it was release.
People usually use a boolean that can be set to "TRUE" or to "FALSE". Default is FALSE.
So add a boolean to your code, using a name that relates to your switch. FALSE will mean the switch is not pressed. When you code discovers the switch is pressed AND the boolean is false, set the boolean to TRUE. Now you know for a certainty that the switch has just been pressed,so do you code for a button press.
At all other time you discover the button is pressed, the boolean will tell you this is NOT the first time.
When you discover the button is not pressed, set the boolean back to FALSE, so it is ready for the next time you actually press the button.
Good luck!
After you have been able to discover when a button becomes pushed, add 1 to a counter. Then based your menu on the counter value. Reset the counter when you get the maximum count.
void loop()
{
if (digitalRead(2) == LOW)
{
pushCount++;
switch (pushCount)
{
case 1:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("fire");
break;
case 2:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ice");
break;
case 3:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("wind");
break;
case 4:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("earth");
pushCount = 0;
break;
}
}
}
3. If you want that if Button is pressed for once within 5-sec then "fire" would appear on LCD; if Button is pressed for twice within 5-sec, then "ice" would appear on LCD; so on, then the codes would be different from that of Step-2.