由于传感器读数而按下按钮时,菜单不起作用

时间:2018-09-11 11:06:47

标签: arduino

#include <LiquidCrystal.h>
#include <dht.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //To lcd (RS,E,D4,D5,D6,D7)

dht DHT;

#define DHT11_PIN 7


//Counter to change positions of pages

int page_counter = 1 ;     //To move beetwen pages
//-------Pins-----//
int up = 18;               //Up button
int down = 19;           //Down button
//---------Storage debounce function-----//
boolean current_up = LOW;
boolean last_up = LOW;
boolean last_down = LOW;
boolean current_down = LOW;


void setup() {
  lcd.begin(16, 2);
  pinMode(up, INPUT_PULLUP);
  pinMode(down, INPUT_PULLUP);

}

void loop() 
{

  current_up = debounce(last_up, up);         //Debounce for Up button
  current_down = debounce(last_down, down);   //Debounce for Down button

  //----Page counter function to move pages----//

  //Page Up
  pageUP();
  //Page Down
  pageDOWN();

   float chk = DHT.read11(DHT11_PIN);
  //------- Switch function to write and show what you want---//

  switch (page_counter) 
  {

    case 1: 
      {    //Design of home page 1 

        lcd.setCursor(3, 0);
        lcd.print("Temperature" );
        lcd.setCursor(3, 1);delay(2000);
        lcd.print(DHT.temperature);//here is the problem ********
        lcd.print((char)223);
        lcd.print("C");


      }
      break;

    case 2: 
      { //Design of page 2
        lcd.setCursor(5, 0);
        lcd.print("Humidity");
        lcd.setCursor(5, 1);
       lcd.print(DHT.humidity); //here is the problem ********
       lcd.print("%");

      }
      break;

    case 3: 
      {   //Design of page 3
        lcd.setCursor(1, 0);
        lcd.print("You are now on");
        lcd.setCursor(4, 1);
        lcd.print("Page 3");
      }
      break;

  }//switch end
//delay(2000);
}//loop end


//---- De-bouncing function for all buttons----//
boolean debounce(boolean last, int pin)
{
  boolean current = digitalRead(pin);
  if (last != current)
  {
    //delay(5);
    current = digitalRead(pin);
  }
  return current;
}


void pageUP()
{
  if (last_up == LOW && current_up == HIGH) 
  { //When up button is pressed
    lcd.clear();                     //When page is changed, lcd clear to print new page
    if (page_counter < 3) {           //Page counter never higher than 3(total of pages)
      page_counter = page_counter + 1; //Page up

    }
    else {
      page_counter = 1;
    }
  }
    last_up = current_up;

}

void pageDOWN()
{
   if (last_down == LOW && current_down == HIGH) 
   { //When down button is pressed
    lcd.clear();                     //When page is changed, lcd clear to print new page
    if (page_counter > 1) {           //Page counter never lower than 1 (total of pages)
      page_counter = page_counter - 1; //Page down

    }
    else {
      page_counter = 3;
    }
  }

  last_down = current_down;
}`enter code here`

当我要打印温度和湿度等变化的值时,这些按钮不起作用。我认为这是因为dht22传感器需要延迟。 我只是使用Arduino的初学者,我在互联网上找到了此代码。我在面包板上放置的2个按钮仅在我打印字符串时起作用,但是当我打印循环更改的值时,我会遇到一些问题。

0 个答案:

没有答案