Go Down

Topic: Unresponsive Button Presses (Read 664 times) previous topic - next topic

ScottCaval

I have written code that you can toggle through a few pages that display on a parallax lcd screen through button presses. The issue i am having is when I add code to read a humidity sensor in the "void loop()" my button presses become slow. Any ideas?

Nick Gammon


Quick5pnt0

That doesn't look like all of your code. You keep referring to get_Humidity() but I dont see that declared anywhere. Please place your code in code tags as well.

ScottCaval

Code: [Select]


#include "SoftwareSerial.h"
#include "DHT.h"


#define DHTPIN 5     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE);

//Variable
int buttonStateSet = 0;
int buttonStateUp = 0;
int buttonStateDown = 0;

int buttonSetPin   = 12;
int buttonUpPin    = 10;
int buttonDownPin  = 11;
int lastButtonState = 0;
int lastButtonStateUp=0;
int lastButtonStateDown=0;

int relayPin = 4;

int buttonStateSetCount;
int buttonStateHourCount;
int buttonStateHumCount;
int setVariableZero = 0;
const int TxPin = 9;
int displayCount = 0;
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);
float lastTempChange  =0;
float lastHumChane  =0;
long lastTimeStamped;
long lastTimeStamped_2;
void setup() {
 
  dht.begin();
 
  pinMode(TxPin, OUTPUT);
  digitalWrite(TxPin, HIGH);
 
  pinMode(buttonSetPin,INPUT);
  pinMode(buttonUpPin,INPUT);
  pinMode(buttonDownPin,INPUT);
  pinMode(relayPin,OUTPUT);
 
  mySerial.begin(9600); 
 
 
  delay(100);
  mySerial.write(12);                 // Clear             
  mySerial.write(17);                 // Turn backlight on
  delay(5);                           // Required delay
  mySerial.print("== Jar Test ==");          // First line
  mySerial.write(13);                 // Form feed
  mySerial.print("1.01v");             // Second line
  mySerial.write(212);                // Quarter note
  mySerial.write(220);                // A tone
 
  delay(3000);
  mySerial.write(12);
  display_Stat();
 
}

void loop() {
 
  unsigned long currentMillis = millis();
 
//if( digitalRead(12)==HIGH){
buttonSetCall();
// }
//if(digitalRead(10) == HIGH){
buttonUpCall();
//}
// if(digitalRead(11)==HIGH){
buttonDownCall();
//}
 
 
  if(buttonStateSetCount==0){
    if(currentMillis - lastTimeStamped > 5000){
      lastTimeStamped = currentMillis; 
      if(get_Humidity()!=lastHumChane)
      {
        display_Stat();
      }
      lastHumChane=get_Humidity();
       if(get_Temp()!= lastTempChange)
      {
        display_Stat();
      }
      lastTempChange=get_Temp();
    }
  }
// buttonStateUp   = digitalRead(buttonUpPin);
// buttonStateDown = digitalRead(buttonDownPin);
if(buttonStateSetCount==0){
    if(currentMillis - lastTimeStamped_2 > 5000){
if(get_Humidity()>=buttonStateHumCount){
  digitalWrite(relayPin,HIGH);
}
if(get_Humidity()<=buttonStateHumCount){
  digitalWrite(relayPin,LOW);
}
    }
}

}

float get_Humidity(){
   float h = dht.readHumidity();
   return h;
}
float get_Temp(){
  float t = dht.readTemperature();
  return t;
}
void buttonSetCall(){
 
  buttonStateSet  = digitalRead(buttonSetPin);
  if(buttonStateSet!=lastButtonState){
    if(buttonStateSet==HIGH){
       buttonStateSetCount++;
      // mySerial.write(12);
      // delay(5);
       //mySerial.write(12);
      // mySerial.print(buttonStateSetCount);
        if(buttonStateSetCount>=3){
            buttonStateSetCount=setVariableZero;
          //  mySerial.write(12);
           // delay(5);
          //  mySerial.print(buttonStateSetCount);
         }
        if(buttonStateSetCount==0){
            display_Stat();
        }
        if(buttonStateSetCount==1){
          display_Day();
        }
        if(buttonStateSetCount==2){
          display_Hum();
        }
    }
 
  }
  lastButtonState=buttonStateSet;
}

void buttonUpCall(){
  buttonStateUp  = digitalRead(buttonUpPin);
    if(buttonStateSetCount==1){
   
    if(buttonStateUp!=lastButtonStateUp){
     
      if(buttonStateUp==HIGH){
         buttonStateHourCount++;
         
          if(buttonStateHourCount>=25){
              buttonStateHourCount=setVariableZero;
           }
           display_Day();
        }
    }
    }
     
   
    if(buttonStateSetCount==2){
   
    if(buttonStateUp!=lastButtonStateUp){
     
      if(buttonStateUp==HIGH){
         buttonStateHumCount++;
         
          if(buttonStateHumCount>=101){
              buttonStateHumCount=setVariableZero;
           }
           display_Hum();
        }
      }
   
    }
    lastButtonStateUp=buttonStateUp;
}
void buttonDownCall(){
    buttonStateDown  = digitalRead(buttonDownPin);
    if(buttonStateSetCount==1){
   
    if(buttonStateDown!=lastButtonStateDown){
     
      if(buttonStateDown==HIGH){
          if(buttonStateHourCount>=1){
          buttonStateHourCount--;
      }
         
          if(buttonStateHourCount==0){
              buttonStateHourCount=24;
           }
           
             
 
           display_Day();
        }
    }
    }
     
   
    if(buttonStateSetCount==2){
   
    if(buttonStateDown!=lastButtonStateDown){
     
      if(buttonStateDown==HIGH){
              if(buttonStateHumCount>=0){
          buttonStateHourCount--;
      }
          if(buttonStateHumCount==0){
              buttonStateHumCount=100;
           }
           
           display_Hum();
        }
      }
   
    }
    lastButtonStateDown=buttonStateDown;

}





void display_Stat(){

  mySerial.write(12);                 // Clear   
  delay(5);   
  mySerial.print("Hum:");          // First line
  mySerial.print(get_Humidity());
  mySerial.print("%");
  mySerial.write(13);                 // Form feed
  mySerial.print("Temp:");             // Second line
  mySerial.print(get_Temp());
  mySerial.print("C");
  mySerial.write(212);


}
void display_Day(){
  delay(5);
  mySerial.write(12);                 // Clear             
  mySerial.print("Vents per day");          // First line
  mySerial.write(13);                 // Form feed
  mySerial.print("Set to:");             // Second linebuttonStateUpCount
  mySerial.print(buttonStateHourCount);
  mySerial.write(212);

}
void display_Hum(){
  delay(5);
  mySerial.write(12);                 // Clear             
  mySerial.print("Max humidity");          // First line
  mySerial.write(13);                 // Form feed
  mySerial.print("Set to:");             // Second line
  mySerial.print(buttonStateHumCount);
  mySerial.print("%");
  mySerial.write(212);

}








Quick5pnt0

I don't know much about them but according to an example file I found from adafruit "Reading temperature or humidity takes about 250 milliseconds!" with the DHT sensors. I can't say that is definitely your problem but if the sketch is delaying only when adding the DHT code I'd guess that is probably the problem.

https://github.com/adafruit/DHT-sensor-library/blob/master/examples/DHTtester/DHTtester.pde

Go Up