Switch Case for Toggle Button

Iv'e bee bashing my head against a wall for sometime trying to work out the best way to write the code for a settings toggle button and was hoping someone could point me in the right direction.

I am using buttons on a touch screen and want one of the buttons to toggle between setting two alarms. I have done the code for the up & down buttons but need to toggle between;

#define Alarm0Hour 0
#define Alarm0Min 1
#define Alarm1Hour 2
#define Alarm1Min 3
#define ClearSelection 4

I thought a switch case would be the way to go but I'm not sure Im using the right approach. I have extracted out the relevant code.

What I'm trying to do is toggle between 5 states so when I press the UP buttons it will look to see which boolean is true & then do that.

Can anyone tell me whether I'm on the right track?

boolean  Alarm0Hour_Set;
boolean  Alarm0Minute_Set;
boolean  Alarm1Hour_Set;
boolean  Alarm1Minute_Set;

#define Alarm0Hour 0
#define Alarm0Min 1
#define Alarm1Hour 2
#define Alarm1Min 3
#define ClearSelection 4

 else  if (pressed_button == but11) { //Toggle within each setting

    if (selectMode == true) {

      settingsMillis = millis();  // Start being-in-select-mode timer
      select++;  // Move to next select option
      myGLCD.setColor(255, 255, 0);
    }

    // switch (select % (MAXPARAM + 3))

    switch (select) {
      
      case Alarm0Hour:
        Alarm0Hour_Set = true;
        myGLCD.fillRect(218, 65, 16, 24); // Alarm0Hour
        settingsMillis = millis();  // Start being-in-select-mode timer
        break;
        
      case Alarm0Min:
        Alarm0Hour_Set = false;
        Alarm0Minute_Set = true;
        myGLCD.fillRect(266, 65, 12, 24); // Alarm0Min
        myGLCD.print(":", 218, 65);
        settingsMillis = millis();  // Start being-in-select-mode timer
        break;
        
      case Alarm1Hour:
        Alarm0Minute_Set = false;
        Alarm1Hour_Set = true;
        myGLCD.fillRect(218, 102, 16, 24); // Alarm1Hour
        myGLCD.print(":", 266, 65);
        settingsMillis = millis();  // Start being-in-select-mode timer
        break;
        
      case Alarm1Min:
        Alarm1Hour_Set = false;
        Alarm1Minute_Set = true;
        myGLCD.fillRect(266, 102, 12, 24); // Alarm1Min
        myGLCD.print(":", 218, 102);
        settingsMillis = millis();  // Start being-in-select-mode timer
        break;
        
      case ClearSelection:
        Alarm1Minute_Set = false;
        myGLCD.print(":", 266, 102);
        settingsMillis = millis();  // Start being-in-select-mode timer
        break;
    }
  }
}

There's too much to put it all in so I have just put the relevant button's in, in the hope that is enough.

This code is in a Void Buttons section of my code, all of the other buttons work and I know the actual setting of the alarm should work as it's virtually identical to setting the time which does work. It's just getting the toggle button (but11) to toggle between each hour/minute so I can set them. Once I have toggled thru then the toggle button needs to ClearSelection so if I accidently press the alarm setting buttons the they wont do anything. I hope that is clear.

Iv'e setup the booleans & definitions below to use in the toggling and changing of the alarm times.

boolean  Alarm0Hour_Set;
boolean  Alarm0Minute_Set;
boolean  Alarm1Hour_Set;
boolean  Alarm1Minute_Set;

#define Alarm0Hour 0
#define Alarm0Min 1
#define Alarm1Hour 2
#define Alarm1Min 3
#define ClearSelection 4





 else if (pressed_button == but9) //Up Arrow for Setting Alarm
  {
    if (Alarm0Hour_Set == true)
    {
      alarm.hour++;
      rtc.writeAlarm(0, &alarm); // for alarm 0
      settingsMillis = millis(); // Reset being-in-select-mode timer
    }
    else if (Alarm0Minute_Set == true)
    {
      alarm.minute++;
      rtc.writeAlarm(0, &alarm); // for alarm 0
      settingsMillis = millis(); // Reset being-in-select-mode timer
    }
    else if (Alarm1Hour_Set == true)
    {
      alarm.hour++;
      rtc.writeAlarm(1, &alarm); // for alarm 0
      settingsMillis = millis(); // Reset being-in-select-mode timer
    }
    else if (Alarm1Minute_Set == true)
    {
      alarm.minute++;
      rtc.writeAlarm(1, &alarm); // for alarm 0
      settingsMillis = millis(); // Reset being-in-select-mode timer
    }
  }

  //********************************************************************************************/

  else if (pressed_button == but10) //Down Arrow for setting alarm
  {
    if (Alarm0Hour_Set == true)
    {
      alarm.hour--;
      rtc.writeAlarm(0, &alarm); // for alarm 0
      settingsMillis = millis(); // Reset being-in-select-mode timer
    }
    else if (Alarm0Minute_Set == true)
    {
      alarm.minute--;
      rtc.writeAlarm(0, &alarm); // for alarm 0
      settingsMillis = millis(); // Reset being-in-select-mode timer
    }
    else if (Alarm1Hour_Set == true)
    {
      alarm.hour--;
      rtc.writeAlarm(1, &alarm); // for alarm 0
      settingsMillis = millis(); // Reset being-in-select-mode timer
    }
    else if (Alarm1Minute_Set == true)
    {
      alarm.minute--;
      rtc.writeAlarm(1, &alarm); // for alarm 0
      settingsMillis = millis(); // Reset being-in-select-mode timer
    }
  }

  //********************************************************************************************/

  else  if (pressed_button == but11) { //Toggle within each setting

    if (selectMode == true) {

      settingsMillis = millis();  // Start being-in-select-mode timer
      select++;  // Move to next select option
      myGLCD.setColor(255, 255, 0);
    }

    // switch (select % (MAXPARAM + 3))

    switch (select) {
            
      case Alarm0Hour:
        Alarm0Hour_Set = true;
        myGLCD.fillRect(218, 65, 16, 24); // Alarm0Hour
        settingsMillis = millis();  // Start being-in-select-mode timer
        break;
        
      case Alarm0Min:
        Alarm0Hour_Set = false;
        Alarm0Minute_Set = true;
        myGLCD.fillRect(266, 65, 12, 24); // Alarm0Min
        myGLCD.print(":", 218, 65);
        settingsMillis = millis();  // Start being-in-select-mode timer
        break;
        
      case Alarm1Hour:
        Alarm0Minute_Set = false;
        Alarm1Hour_Set = true;
        myGLCD.fillRect(218, 102, 16, 24); // Alarm1Hour
        myGLCD.print(":", 266, 65);
        settingsMillis = millis();  // Start being-in-select-mode timer
        break;
        
      case Alarm1Min:
        Alarm1Hour_Set = false;
        Alarm1Minute_Set = true;
        myGLCD.fillRect(266, 102, 12, 24); // Alarm1Min
        myGLCD.print(":", 218, 102);
        settingsMillis = millis();  // Start being-in-select-mode timer
        break;
        
      case ClearSelection:
        Alarm1Minute_Set = false;
        myGLCD.print(":", 266, 102);
        settingsMillis = millis();  // Start being-in-select-mode timer
        break;
    }
  }
}
//buttons finished

There's too much to put it all in

Not true. Use Reply, not the stupid Quick Reply field. There is a link (though it hardly looks like one) below the text entry field, Additional Options. Click that, and you can attach a file to your post. The one containing your code would be a good choice.

I have attached the code.

You will see there is already a timer function (void TurnRelayOnOff() ) that doesn't work properly so I am trying to replace it with the alarm function that can also be set via the touch screen.

Once I get the void DisplayAlarms() and the setting functions working properly the intention is to then work out how to trigger the relays once the alarms fire.

PS I would be very grateful for any suggestion re how to improve the code. Obviously parts of the code are from other people.

It's the function within "but11" that I'm wanting to get working properly.

If you can tell me why the relays wont trigger when I set the timer in void TurnRelayOnOff() as it is set in the attached code that would be great as well. When I was testing & setting the timer to say turn the relays on at 17:50 & off at 17:52, it worked every time. But when I try and turn the relays on at 5:00 nothing happens.

_15-11_20_V2-7_Alarms_For_Forum.ino (22.3 KB)

OCR_A_Extended_M.c (23.2 KB)

int  but1, but2, but3, but4, but5, but6, but7, but8, but9, but10, but11, pressed_button;

Instead of an array? Why?

  float led2Temp1;
  led2Temp1 = sensors.getTempCByIndex(1); //Sensor on Plate adjacent to LED1 = Cree 3050 = middle LED

Why do you need two lines to do this?

  float led2Temp1 = sensors.getTempCByIndex(1);

In ButtonPressed(), a switch statement would be better than all those if/else if statements. Where do but1, but2, etc. get assigned values?

You will see there is already a timer function (void TurnRelayOnOff() ) that doesn't work properly

I'm too tired to guess what it actually does, or how that differs from what you want. There are NO Serial.print() statements in the function, so how do you know that the RTC is even working?

PaulS:

int  but1, but2, but3, but4, but5, but6, but7, but8, but9, but10, but11, pressed_button;

Instead of an array? Why?

You over estimate my knowledge I doont know why :), the reason I do it is because there are libraries for the buttons & it has worked in the past & they mostly work now,

PaulS:

  float led2Temp1;

led2Temp1 = sensors.getTempCByIndex(1); //Sensor on Plate adjacent to LED1 = Cree 3050 = middle LED



Why do you need two lines to do this?

I dont know I was coping other peoples code & it works so I'm good with that.

PaulS:

  float led2Temp1 = sensors.getTempCByIndex(1);

In ButtonPressed(), a switch statement would be better than all those if/else if statements. Where do but1, but2, etc. get assigned values?

I used if else because I generally know how it works, I'm not really sure what you mean by values but as mentioned there is a 3rd party library that I am using.

PaulS:
I'm too tired to guess what it actually does, or how that differs from what you want. There are NO Serial.print() statements in the function, so how do you know that the RTC is even working?

Because it displays on the screen I am using.

I have attach images of the Main Screen & the Settings screen.

The Idea is the Main screen displays the info I want to see from the senors and then when I press the ST button (but7) the screen changes to the Settings screen.

In the settings screen you will see 5 buttons, the three at the top are used to set the time, the ST button at the top left toggles between the hour & minute and the up & down arrows are used to adjust the time. This all works fine. The buttons at the bottom adjust the temp setpoint for the fan on one of the LED's

I want to add buttons under the time setting buttons (as per the drawing) to set the two alarms, but I only want to use one ST button (but11) to toggle between four selections, alarm1 hour, alarm2 hour, alarm1 minute & alarm2 minute. It's that toggling that I need help with.

Everything appears to be working now :slight_smile: