Go Down

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

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

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

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.

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

holmes4

The problem is the use of the delay() there is no way to respond to a button press while in a delay. After looking at FSM's and the blink without delay example scrap and rewrite your code.

Mark

holmes4

Oh, forgot to add,

1. You will also need to debounce the button and

2. No you can't use interrupts to get around this problem (lots to threads about why not on the forum).

Mark

PaulS

Code: [Select]
float get_Humidity(){
   float h = dht.readHumidity();
   return h;
}

I never understand why people write a function that makes exactly one function call. What was wrong with just calling dht.readHumidity() where you call this function?

Now that i am looking at it I agree with you PaulS. But in regards to the rest of the post I am not using any delays im using millis().

PaulS

Quote
But in regards to the rest of the post I am not using any delays im using millis().

Is any of that commented out code contributing to the slow reaction to the switch presses? I don't think so. So, get rid of it.

Does the
            random indenting
    of the remaining
                    code contribute
         to its readability?

I don't think so, so use Tools + Auto Format to fix it.

The way I see it, the code is substituting calls to millis to determine when to do something for calls to delay to make the code wait. I don't see that the change makes things happen more often/faster. Of course the useless code and poor structure don't help.

Code: [Select]
int setVariableZero = 0;
Is this so that you can change to value to 27 at some point? Wouldn't you then to rename the variable to avoid looking stupid? How would that make maintenance any easier?

I am completely new to this and I am sure there are many mistakes made in this chicken scratch of a sketch. I am assuming through by comment that there are definitely simpler ways of doing this.

Go Up