Go Down

Topic: working with 2 buttons to make a menu system (Read 93 times) previous topic - next topic

MrAbalogu

Im trying to make a menu system on OLED 96' display (Adafruit) and Im using 2 buttons. 1 button to scroll through the menu and the 2nd button to Enter menu. But with my code when I press the Scroll button it wont work anymore till I press the ENTER button dont know why..
 
Heres my code:

Code: [Select]

  int i = 0;
  int j = 0;

void setup(){
 
  pinMode(ENTER_PIN, INPUT); //Set pin for time/date set button to input
  digitalWrite(ENTER_PIN, HIGH); //Turn on pullup resistors
 
  pinMode(SCROLL_PIN, INPUT); //Set pin for time/date set button to input
  digitalWrite(SCROLL_PIN, HIGH); //Turn on pullup resistors

  display.begin(SSD1306_SWITCHCAPVCC, 0x3c);  // initialize with the I2C addr 0x3D (for the 128x64)
  display.clearDisplay();
 
 
}

void loop(){
 
  DisplayMenu();
  ScrollMenu();
}


void DisplayMenu(){
 
  display.setCursor(30,0);
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setTextWrap(false);
  display.print("MAIN MENU");
 
 // display.clearDisplay();
  display.setCursor(35,30);
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setTextWrap(false);
  display.print("BEEP TEST");
 
  // display.clearDisplay();
  display.setCursor(30,40);
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setTextWrap(false);
  display.print("RUN HISTORY");
 
 // display.clearDisplay();
  display.setCursor(10,50);
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setTextWrap(false);
  display.print("Calorie burnt today");
  display.display();
 
}


void ScrollMenu(){
 
    if ( digitalRead(SCROLL_PIN) == HIGH) {
      do {} while (digitalRead(SCROLL_PIN) == HIGH);
       
        i=i+1;
        if ( i == 4 )
        {
          i = 1;
        }
       
            display.drawRect(32,28,59,12,BLACK);
            display.drawRect(28,38,70,12,BLACK) ;
            display.drawRect(8,48,200,12,BLACK);
       
        switch(i)
        {
          case 1 :  display.drawRect(32,28,59,12,WHITE); delay(25); break;
          case 2 :  display.drawRect(28,38,70,12,WHITE); delay(25); break;
          case 3 :  display.drawRect(8,48,200,12,WHITE); delay(25); break;
        };
       
        display.display();
      }
     
      if ( digitalRead(ENTER_PIN)) {
      do {} while (digitalRead(ENTER_PIN));
       
        switch (i) {
         
          case 1:
          BeepTest();
          break;
         
          case 2:
          RunHistory();
          break;
         
          case 3:
          CaloriesBurntToday();
          break;
         
        }
         display.display();   
     
   }   
 
}

void BeepTest (){

  display.clearDisplay();
  display.setCursor(35,30);
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setTextWrap(false);
  display.print("START TEST");
 
  display.setCursor(10,50);
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setTextWrap(false);
  display.print("BACK");
  display.display();
 
}

void RunHistory(){
  display.clearDisplay();
  display.setCursor(10,0);
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setTextWrap(false);
  display.print("Last run:  ");
 
  display.setCursor(10,20);
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setTextWrap(false);
  display.print("Total run distance:  ");
 
  display.setCursor(10,50);
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setTextWrap(false);
  display.print("BACK");
  display.display();
}

void CaloriesBurntToday(){
  display.clearDisplay();
  display.setCursor(0,10);
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setTextWrap(false);
  display.print(" Calories Burnt:   ");
 
  display.setCursor(10,50);
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setTextWrap(false);
  display.print("BACK");
  display.display();
}

CommonRodent

If you have the up to date environment you should be able to do

  pinMode(ENTER_PIN, INPUT_PULLUP); //Set pin for time/date set button to input

instead of

  pinMode(ENTER_PIN, INPUT); //Set pin for time/date set button to input
  digitalWrite(ENTER_PIN, HIGH); //Turn on pullup resistors



I think you need to look up switch debouncing, there is an example project for this that comes with the tool. In short a switch can 'bounce' several times between 0 and 1 when it is pressed and released.

You might also be mistaking the 'ON' level of your switches, which would be '0' when pressed. But this depends on your external circuit, you might want to include a series resistor for safety.

MrAbalogu

Actually I have a leg of my button connected to ground and the other leg connected to a pin. in my code i have set the pin to HIGH so its always high till you press the button which leads to ground (LOW)... and I have done this for both buttons

CommonRodent

These lines will wait forever until Enter is 0 (LOW), your control loop is getting stuck half way through

Code: [Select]
    if ( digitalRead(ENTER_PIN)) {
      do {} while (digitalRead(ENTER_PIN));

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy