0

I'm creating a Temperature logging device with thermocouple K and SD card breakout and a RGB LCD Shield Kit w/ 16x2 Character Display (with buttons).

Everything runs ok: temperature displaying on the LCD and logging on txt file on the micro SD card work well

Now I try to configure button on the shield

  1. button UP to reset measurements and timer , it works
  2. button Down to pause measurements, then when pushed again, continue measurements. I don't know how to do that (how to go out the while condition).

     if (buttons) {
    
    if (buttons & BUTTON_UP) {
    
      lcd.clear();
      lcd.print("RESET ");
      if (SD.exists("datalog.txt")) {
    
        SD.remove("datalog.txt");
        File dataFile = SD.open("datalog.txt", FILE_WRITE);
      }
    
      else {
    
        // open the file. note that only one file can be open at a time,
        // so you have to close this one before opening another.
        File dataFile = SD.open("datalog.txt", FILE_WRITE);
        // if the file is available, write to it:
    
      }
     StartTime = millis();
    
      delay(500);
    
    }
    if (buttons & BUTTON_DOWN) {
      lcd.clear();
      lcd.print("STOP");
      while (buttons & BUTTON_DOWN){
    
          ;
      }
    

    }

I already thank you for your help,

Vincent

  • think about your first two if statements .... why are you testing buttons in the second if statement? .... you already tested it in the first if statement .... same with the last if statement ..... also, check the while (buttons & BUTTON_DOWN){ statement at the bottom ..... do you think that the values of buttons and BUTTON_DOWN will ever change? .... if you think that they will change, then what changes them? – jsotola Dec 10 '18 at 18:41
  • This way of testing the buttons comes from the example given with the adafruit RGB LCD shield library. I tried not to re-test "buttons" in the IF statement after the first if(buttons) but the lcd diplays all statements of the next if(BUTTON_DIRECTION) like all the if test give a TRUE. Sorry it is difficult to be clear. – Vincent Dec 11 '18 at 8:38
0

You are not saying much about how buttons is valorized or its data type, but assuming it is a numeric variable which gets somehow set by, let's say a readButtons() function, then consider the following snippet replacing your while-loop:

do {
  delay(10);
  // re-evaluate whatever sets the buttons variable
  buttons = readButtons();
while(!(buttons & BUTTON_DOWN));

The loop above will continue until the button down is pressed and it's a little resilient regarding contact bouncing, but not 100% reliable, so I assume the readButtons() performs also some debouncing.

  • Thank you Roberto, you have seen right. I'm using the LCD shield library, with a lcd.readButtons function. – Vincent Dec 11 '18 at 8:32
  • @Vincent if this solves your problem then please accept the answer so that others will be able to use it as a reference – Roberto Lo Giacco Dec 11 '18 at 22:55
  • I'm new on stack exchange so thank you! – Vincent Dec 12 '18 at 11:37

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.