Need help understanding toggle switches

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.

Hello drewiepooh

Post your sketch, well formated, with well-tempered comments and in so called code tags "</>" and schematic to see how we can help.

Count number of push --
1 push : fire
2 push: ice
3 push: wind
4 push: earth

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!

1 Like

I have never used boolean In any code but I will research and try this thanks

hi, I don't quite understand how to do this. Can you explain a little more?

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.

this makes sense ill try to do the thank you!!

1 Like

Do you know what a counter variable is ?

ex:

if( switch was pushed)
{
counter = counter + 1;
if(counter > 4)
{
counter = 0;
}
}

if (counter == 0)
{
print the menu options
}

if (counter == 1)
{
print “fire”
}

. . .

1 Like

didn't know but thank you ill try this option I think it makes more sense

Hints:
1. Wiring diagram
sw1led1LCD

2. Example Codes

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.

2 Likes

thank you this worked!!

thank you for this

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.