Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15374

Simple mode change on button press failing

$
0
0

Hello, I am encountering strange behaviour in my code which I cant explain. First a little bit of background info: I am using a keypad to change modes in my program which are displayed on an OLED screen. I am using pinchangeinterrupts for the three buttons. The circuit is built on a custom board using the ATMEGA 328P - AU chip set up as a Pro-mini 5V 16MHz within the Arduino IDE. The circuit design is one I have used before in a very similar way with no problems .
I have 4 Modes in the code , 0, 1, 3 and 4. When a button is pressed the interrupt flag is set for the relevant button and if found to be true in the main loop, the Mode is changed according to the current Mode. So for example, if Mode is currently '1' when the UP key is pressed the Mode is changed to '0'. The problem is that sometimes the mode value is changing to odd values like for example 12288. this results in the code locking up and no longer responding to the button presses.
I have identified the spurious values by printing the value of Mode to the Serial monitor as seen in this extract:
6:43:32.970 -> countdownSet is 20000
16:43:33.198 -> Drawing Timer.......DONE
16:43:33.198 -> Mode is 3
16:43:34.197 -> Mode is 3
16:43:35.179 -> Mode is 3
16:43:36.146 -> EnterFlag true
16:43:36.335 -> Mode is 0
16:43:36.368 -> countdownSet is 20000
16:43:36.503 -> Mode is 0
16:43:36.873 -> EnterFlag true
16:43:37.088 -> Mode is 12288
16:43:37.126 -> countdownSet is 20000
16:43:37.202 -> Drawing Timer.......DONE
16:43:37.496 -> Mode is 12288
16:43:37.702 -> EnterFlag true

 Here is an extract from the Code:  

I would welcome any thoughts as to why this is happening please.  

    //--------Check for Up Key pressed-------

    if(UpFlag == true && millis() >= 1000){
    
    if (Mode ==0){                                  // Duration setting mode  Duration will be highlighted
      delay(50);
        Mode = 1;                                   // switch to Arm mode 
        delay(50);
       Serial.print("Mode is ");
            Serial.println(Mode);
       delay(50);
        topmenu_current = 1;                        // menu page with ARM highlighted
        menu_redraw_required =1;
        if (  menu_redraw_required != 0 ) {      // redraw if required
            u8g.firstPage();
            do  {
              drawTopMenu();
            } while( u8g.nextPage() );
            menu_redraw_required = 0;
          }
        
        //delay(50);
        


        }else{
        if (Mode == 1 ){                        // ARM mode is highlighted
          delay(50);
            Mode = 0;                           // Switch to Duration highlighted
            delay(50);
            Serial.print("Mode is ");
            Serial.println(Mode);
            delay(50);
            topmenu_current = 0;
            menu_redraw_required =1;
            if (  menu_redraw_required != 0 ) {      // redraw if required
            u8g.firstPage();
            do  {
              drawTopMenu();
            } while( u8g.nextPage() );
            menu_redraw_required = 0;
          }
         
            //delay(50);
            

             }else{ 
              if (Mode == 3 ) {
                delay(50);
                
             
        //if (currentMillis - previousMillis >= interval) {         // Save the current time to compare against next time
        //previousMillis = currentMillis;

        // Increase the countdown time if requested and within limits
          if (increaseTime && countdown < 720000) { // Limit increase to 12 minutes (12 * 60 * 1000 = 720000)
           countdown += 10000; // Increase by 10 seconds
           
            if (countdown >= 720000) { // Check if reached maximum time
              countdown = 720000; // Limit to maximum time
           
            }
            countdownSet = round(countdown/10000)*10000;                   // store the new value of 
                                                                                               countdown rounded to the nearest 10 seconds
            Serial.print("countdownSet is  ");
                Serial.println (countdownSet);
                delay(50);
            increment = countdownSet/1000;                                  // convert countdownSet to seconds
            EEPROM.write(1, increment );                                   //update the stored value of increment
              increaseTime = false;       // Stop increasing
              }
             drawTimer();            // Display the countdown timer on the OLED display 
          }
        } 
     }
    UpFlag = false;  
  }
             
   //--------Check for Down Key pressed-------

Thanks everyone!

8 posts - 4 participants

Read full topic


Viewing all articles
Browse latest Browse all 15374

Trending Articles