Help with operation using if{} else if{}

Hello and sorry if this is drawn out, but I'm trying to change a couple if{} statements that both run the same operations when either is met (which is currently working perfectly), into an if{} / else if{} so when one is met and true, a set of operations runs, then if the other is met and tru, it runs a separate set of operations.

Right now its set so if a long button press is met for 5 seconds, or the 5 minutes time elapses, the same set of operations happen (again, this runs great with no issues). I would like to change it so when the long button press occurs, it runs a set of operations, then if the 5 minutes time elapses, it runs a different set of operations.

Ive tried editing using the if{} / else if{} statements, but when I do, the long button press no longer waits the set time of 5 seconds, as soon as the button is pressed, it runs the if{} functions, but when the time elapses, it doesnt run all the else if{} functions, just clears the lcd and back to the main loop menu, with the called outputs still active and high and not shutting them off.

Any help is appreciated :slight_smile:

Here is the current code not using the if{} / else if{} statements that both process the same operations when either or are met, its not the entire sketch, just the section that refers to the issue...

void menuItem50() {  //Function executes when you select from the list

  //Clearing Tiny85 and LCD
  int activeButton = 0;
  menu_clear();

  //UART to Tiny85
  //Tell it to play file 50 by sending it a '50' or pin to read it in with
  byte msg[3];             //Creates message to send
  msg[0] = 0x35;           //Send a 5 for the receiver to decode and use it in the case\switch statement
  msg[1] = 0x30;           //Send a 0 for the receiver to decode and use it in the case\switch statement
  msg[2] = 0x0A;           //New line character
  mySerial.write(msg, 3);  //Sends message to Tiny85
  //Change lcd display
  lcd.setCursor(0, 0);     //LCD column 1 row 0
  lcd.print("HEADACHE");   //Line 1 display
  lcd.setCursor(0, 1);     //LCD column 1 row 1
  lcd.print("RUNNING");    //Line 2 display

  //----------------------------------------MINUTE TIMER----------------------------------------
  startTime = millis();    //Start the timer before it goes into loop. and set it 0 after it exits
  counter = 0;             //Start with fresh counter when it gets in the function.
}


  //While loop keeps the menu jammed and the attiny85 are independent to continu running

  while ((millis() - startTime < FIVE_MINUTES))                  //While it is less than, it will hold the loop
  {
    bool val = digitalRead(LCD_ADC);                             //Pin with 3 functions attached to it. Interrupt pin, menu control and forced sleep
    if (val == 0)
      counter++;  
    Serial.print("Counter for forced sleep: ");
    Serial.println(counter);

// NOW here if it goes greater than - minutes\seconds or the Forced Sleep counter; it stops the attiny85 from playing

    if (millis() - startTime > FIVE_MINUTES || counter > 500)    //Here if '>' it will execute function, if not, it will print 'its ticking in loop fashion
    {

// - AFTER - MINUTES-: Send the three Attiny85 the reset signal to stop generatinng frequencies, and go to sleep.

      digitalWrite(reset_tiny, LOW);                            //Resets Tiny85
      Serial.println("Tiny are now in Reset");
      lcd.clear();                                              //Clears LCD
      beep();                                                   //Executes buzzer function
      delay(100);                                               //100ms delay
      Going_To_Sleep();                                         //Executes sleep mode
      break;  
    }
  }  //It exits the while loop
}

  while (activeButton == 0) {
    int button;
    readKey = analogRead(0);
    if (readKey < 790) {
      delay(10);
    }

    button = 4;

    switch (button) {
      case 4:
        button = 0;
        activeButton = 1;
        break;
    }
  }
}

Here is the code with the if{} / else if{} statements...

void menuItem50() {  //Function executes when you select from the list

  //Clearing Tiny85 and LCD
  int activeButton = 0;
  menu_clear();

  //UART to Tiny85
  //Tell it to play file 50 by sending it a '50' or pin to read it in with
  byte msg[3];             //Creates message to send
  msg[0] = 0x35;           //Send a 5 for the receiver to decode and use it in the case\switch statement
  msg[1] = 0x30;           //Send a 0 for the receiver to decode and use it in the case\switch statement
  msg[2] = 0x0A;           //New line character
  mySerial.write(msg, 3);  //Sends message to Tiny85
  //Change lcd display
  lcd.setCursor(0, 0);     //LCD column 1 row 0
  lcd.print("HEADACHE");   //Line 1 display
  lcd.setCursor(0, 1);     //LCD column 1 row 1
  lcd.print("RUNNING");    //Line 2 display

  //----------------------------------------MINUTE TIMER----------------------------------------
  startTime = millis();  // start the timer before it goes into loop. and set it 0 after it exits
  counter = 0;           // start with fresh counter when it gets in the function.
}


//----------------------------------------MENU 2----------------------------------------
void menu_2() {

//----------------------------------------TIMING FUNCTION----------------------------------------

//While loop keeps the menu jammed and the attiny85 are independent to continu running

  while ((millis() - startTime < FIVE_MINUTES))                  //While it is less than, it will hold the loop
  {
    bool val = digitalRead(LCD_ADC);                             //Pin with 3 functions attached to it. Interrupt pin, menu control and forced sleep
    if (val == 0){
      counter++;  
    Serial.print("Counter for forced sleep: ");
    Serial.println(counter);

    digitalWrite(reset_tiny, LOW);                            //Resets Tiny85
    Serial.println("Tiny are now in Reset");
    lcd.clear();                                              //Clears LCD
    beep();                                                   //Executes buzzer function
    delay(100);                                               //100ms delay
    Going_To_Sleep();                                         //Executes sleep mode
    break;  
    }
// NOW here if it goes greater than - minutes\seconds or the Forced Sleep counter; it stops the attiny85 from playing

    else if (millis() - startTime > FIVE_MINUTES || counter > 500)    //Here if '>' it will execute function, if not, it will print 'its ticking in loop fashion
    {

// - AFTER - MINUTES-: Send the three Attiny85 the reset signal to stop generatinng frequencies, and go to sleep.

      digitalWrite(reset_tiny, LOW);                            //Resets Tiny85
      Serial.println("Tiny are now in Reset");
      digitalWrite(power_pin, LOW);                             //Tiny85 powered off
      delay(100);                                               //100ms delay      
      lcd.clear();                                              //Clears LCD                                                  
      delay(100);                                               //100ms delay
      loop();                                                   //Back to main loop
      break;  
    }
  }  //It exits the while loop
}

  while (activeButton == 0) {
    int button;
    readKey = analogRead(0);
    if (readKey < 790) {
      delay(10);
    }

    button = 4;

    switch (button) {
      case 4:
        button = 0;
        activeButton = 1;
        break;
    }
  }
}

Your if ends here...

After that there is code and then elseif. Is that what you want?

Edit: the above is not true (see post #4). The below is still a useful hint...

In ide press ctrl-t. That will format your code. It will make these things more visible.
You should see:

if () {
    Blabla;
    Blabla;

}
else if ()  {
    What else to do;
}

Are you saying thats where the if statement is supped to end and the { goes? So it should be like?

bool val = digitalRead(LCD_ADC);                             //Pin with 3 functions attached to it. Interrupt pin, menu control and forced sleep
    if (val == 0)
      counter++;  
    Serial.print("Counter for forced sleep: ");{
    Serial.println(counter);

    digitalWrite(reset_tiny, LOW);                            //Resets Tiny85
    Serial.println("Tiny are now in Reset");
    lcd.clear();                                              //Clears LCD
    beep();                                                   //Executes buzzer function
    delay(100);                                               //100ms delay
    Going_To_Sleep();                                         //Executes sleep mode
    break; 
    }

if so, i get an error: Compilation error: expected ';' before '{' token

I am sorry, but your use of tabs not according to convention confused me...
Your original setup is ok. But it would be more clear if all the code within the if block were aligned.

That was fun, but now have it working :slight_smile:

Using the if{} else if{} was the way to go, but needed to add another if{} statement as well as a defined "TIME_OUT" thats longer then the "FIVE_MINUTES".

So in the while() loop, the first "if" statement appears to be tied to the loop count while the button is pressed, which is set in the second if{} statement

if (millis() - startTime > TIME_OUT || counter > 500)

thusly, it is really the first if{} statement when used with the else if{} statement, which had to be added.

So now the first if{} statement is looking for a long button press for 500 loops within a time that is longer then the run time and if that happens, it runs a set of functions. Then the else if{} statement is to run the program for the alloted time of five minutes and when completed, runs another set of functions.

All tested and appears to function properly :slight_smile:

Heres the change...

void menuItem50() {  //Function executes when you select from the list

  //Clearing Tiny85 and LCD
  int activeButton = 0;
  menu_clear();

  //UART to Tiny85
  //Tell it to play file 50 by sending it a '50' or pin to read it in with
  byte msg[3];             //Creates message to send
  msg[0] = 0x35;           //Send a 5 for the receiver to decode and use it in the case\switch statement
  msg[1] = 0x30;           //Send a 0 for the receiver to decode and use it in the case\switch statement
  msg[2] = 0x0A;           //New line character
  mySerial.write(msg, 3);  //Sends message to Tiny85
  lcd.setCursor(0, 0);     //LCD column 1 row 0
  lcd.print("HEADACHE");   //Line 1 display
  lcd.setCursor(0, 1);     //LCD column 1 row 1
  lcd.print("RUNNING");    //Line 2 display

  //----------------------------------------TIMING FUNCTION----------------------------------------
  startTime = millis();  // start the timer before it goes into loop. and set it 0 after it exits
  counter = 0;           // start with fresh counter when it gets in the function.
}

//----------------------------------------TIMING FUNCTION----------------------------------------

//While loop keeps the menu jammed and the attiny85 are independent to continu running

while ((millis() - startTime < FIVE_MINUTES))  //While it is less than, it will hold the loop
{
  bool val = digitalRead(LCD_ADC);  //Pin with 3 functions attached to it. Interrupt pin, menu control and forced sleep
  if (val == 0)
    counter++;
  Serial.print("Counter for forced sleep: ");
  Serial.println(counter);

  // NOW here if it goes greater than - minutes\seconds or the Forced Sleep counter; it stops the attiny85 from playing

  if (millis() - startTime > TIME_OUT || counter > 500)  //Here if '>' it will execute function, if not, it will print 'its ticking in loop fashion
  {

    // - AFTER - LONG PRESS-: Send the three Attiny85 the reset signal to stop generatinng frequencies, and go to sleep.

    digitalWrite(reset_tiny, LOW);  //Resets Tiny85
    Serial.println("Tiny are now in Reset");
    lcd.clear();       //Clears LCD
    beep();            //Executes buzzer function
    delay(100);        //100ms delay
    Going_To_Sleep();  //Executes sleep mode
    break;
  }

  else if (millis() - startTime > FIVE_MINUTES)  //Here if '>' it will execute function, if not, it will print 'its ticking in loop fashion

  {

    // - AFTER - MINUTES-: Send the three Attiny85 the reset signal to stop generatinng frequencies, and go to main loop.

    digitalWrite(reset_tiny, LOW);  //Resets Tiny85
    Serial.println("Tiny are now in Reset");
    lcd.clear();  //Clears LCD
    delay(100);   //100ms delay
    loop();       //Return to main loop
    break;
  }

}  //It exits the while loop
}

while (activeButton == 0) {
  int button;
  readKey = analogRead(0);
  if (readKey < 790) {
    delay(10);
  }

  button = 4;

  switch (button) {
    case 4:
      button = 0;
      activeButton = 1;
      break;
  }
}
}

Great that you fixed it yourself and reported your success here! :+1:

this still wrong. it won't compile because you're missing a function preface before the while

Thanks :slight_smile:

It stumped me for sure and I was following the if{} else if{} structure, but that original second if{} statement was throwing it off., I didn't write the origninal code, just making modifications from it because the individual I paid to write it, took my requirements and used most of them and added in his own and was very difficult to work with to get anything else done with it, so I figured I would do the rest and this particular part pretty much had me at my wits end, lol.

On the contrary, it compiles and runs just fine :slight_smile:

not the snippet you posted

So technically, that sniplet was simplified, but heres how it really is. I just created it as the calls to menu_1(); and menu_2(); included for sake of ease when originally posting, but heres what it really is and it does in fact compile, program and run with no issues. I have since added more functions for the if{} and else if {} statements, but it works...

void menuItem50() {  //Function executes when you select from the list

  //Clearing Tiny85 and LCD
  int activeButton = 0;
  menu_clear();

  //UART to Tiny85
  //Tell it to play file 50 by sending it a '50' or pin to read it in with
  byte msg[3];             //Creates message to send
  msg[0] = 0x35;           //Send a 5 for the receiver to decode and use it in the case\switch statement
  msg[1] = 0x30;           //Send a 0 for the receiver to decode and use it in the case\switch statement
  msg[2] = 0x0A;           //New line character
  mySerial.write(msg, 3);  //Sends message to Tiny85
  lcd.setCursor(0, 0);     //LCD column 1 row 0
  lcd.print("HEADACHE");   //Line 1 display
  lcd.setCursor(0, 1);     //LCD column 1 row 1
  lcd.print("RUNNING");    //Line 2 display

  menu_1();
  menu_2();

  while (activeButton == 0) {
    int button;
    readKey = analogRead(0);
    if (readKey < 790) {
      delay(10);
    }

    button = 4;

    switch (button) {
      case 4:
        button = 0;
        activeButton = 1;
        break;
    }
  }
}


//----------------------------------------MENU ITEMS----------------------------------------
//----------------------------------------MENU 1----------------------------------------
void menu_1() {

  //----------------------------------------TIMING FUNCTION----------------------------------------
  startTime = millis();  // start the timer before it goes into loop. and set it 0 after it exits
  counter = 0;           // start with fresh counter when it gets in the function.
}


//----------------------------------------MENU 2----------------------------------------
void menu_2() {

  //----------------------------------------TIMING FUNCTION----------------------------------------

  //While loop keeps the menu jammed and the attiny85 are independent to continue running

  while ((millis() - startTime < FIVE_MINUTES))  //While it is less than, it will hold the loop
  {
    bool val = digitalRead(LCD_ADC);  //Pin with 3 functions attached to it. Interrupt pin, menu control and forced sleep
    if (val == 0)
      counter++;
    Serial.print("Counter for forced sleep: ");
    Serial.println(counter);

    //If it goes longer then FIVE_MINUTES or the long button press is for longer then 500 loops, will trigger

    if (millis() - startTime > TIME_OUT || counter > 500)  //Here if '>' it will execute function, if not, it will print 'its ticking in loop fashion
    {

      //Send the three Attiny85 the reset signal to stop generatinng frequencies, and go to sleep

      digitalWrite(reset_tiny, LOW);  //Resets Tiny85
      Serial.println("Tiny are now in Reset");
      lcd.clear();  //Clears LCD

      lcd.setCursor(0, 0);    //LCD column 1 row 0
      lcd.print("SHUTTING");  //Line 1 display
      lcd.setCursor(2, 1);    //LCD column 1 row 1
      lcd.print("DOWN");      //Line 2 display

      beep();            //Executes buzzer function
      lcd.clear();       //Clears LCD
      Going_To_Sleep();  //Executes sleep mode
      break;
    }

    //When the FIVE_MINUTES is reached, will trigger

    else if (millis() - startTime > FIVE_MINUTES)  //Here if '>' it will execute function, if not, it will print 'its ticking in loop fashion

    {

      //Send the three Attiny85 the reset signal to stop generatinng frequencies, and go to main menu

      digitalWrite(reset_tiny, LOW);  //Resets Tiny85
      Serial.println("Tiny are now in Reset");
      digitalWrite(power_pin, LOW);   //Tiny85 powered off
      digitalWrite(power_pin, HIGH);  //Tiny85 powered on
      lcd.clear();                    //Clears LCD
                                      //delay(100);                     //100ms delay
      for (int x = 0; x < 2; x++) {   //2 beeps
        analogWrite(Beep3, 200);      //Set to 200
        delay(200);                   //Time in ms
        analogWrite(Beep3, 0);        //0 is off
        delay(200);                   //Time in ms
      }
      loop();  //Runs main loop
      break;
    }

  }  //It exits the while loop
}

Ahhhh, I see what you mean here... That } at the top there? thats actually a copy/paste error on my part. so yes, if that's there, it won't compile. But in the actual code where it calls menu_1(); and menu_2(); thats not there, so it's all good.