Ho un problema con uno sketch per cambiare la visualizzazione di testo su LCD.
Quando premo il pulsante il cambio del testo non avviene immediatamente ma devo tenere premuto.
Mi potete dare qualche suggerimento?
// Import needed library:
#include "DHT.h" // Library for communication with DHTxx:
#include "DHT_U.h" // Library for communication with DHTxx:
#include "Wire.h" // Library for communicating with the LCD:
#include "LiquidCrystal_I2C.h" // Library for communication with the LCD:
// Initializing variables:
#define DHT_PIN 11 // Pin that the DHT probe is attached to:
#define BTN_PIN 10 // Pin that the Push Button is attached to:
#define TMP_PIN A0 // Pin that the Temperature proble is attached to:
// Uncomment whatever type you're using!
#define DHT_TYPE DHT11 // DHT 11
//#define DHT_TYPE DHT22 // DHT 22 (AM2302, AM2321)
//#define DHT_TYPE DHT21 // DHT 21 (AM2301)
float value = 0; // Store the value coming from the sensor:
float volts = 0; // Store the volt coming from the sensor:
float tempC = 0; // Store the temperature coming from the sensor;
int ButtonState = 0; // Current state of the button:
int PushButtonCounter = 0; // Counter for the number of button presses:
int LastButtonState = 0; // Previous state of the button:
boolean DEBUG = true; // Set to true if you want to read output on the Serial:
// Declare object:
LiquidCrystal_I2C lcd(0x27,16,2); // Set the LCD address to 0x27 in PCF8574 for a 16 chars and 2 line display:
DHT dht(DHT_PIN, DHT_TYPE); // Store an instance of the DHT sensor:
// The setup function runs once when you press reset or power the board:
void setup () {
pinMode(BTN_PIN, INPUT); // Initialize the button pin as a input:
Wire.begin();
dht.begin(); // Start the DHT library code:
// Start LCD library to output our messages:
lcd.init(); // Start LCD to output our messages:
lcd.backlight(); // Turn ON backlight:
lcd.clear(); // Clears the LCD screen:
lcd.setCursor(0,0);
lcd.print(F("Sensor"));
lcd.setCursor(0,1);
lcd.print(F("initialising..."));
if (DEBUG) { // Start serial to output our messages:
Serial.begin(9600);
delay(10);
}
while (!Serial) {
; // Wait for serial port to connect. Needed for native USB port only:
}
Serial.println(F("\nSetup done"));
delay(2000);
} // End setup:
// The loop function runs over and over again forever:
void loop () {
// Read the pushbutton input pin:
ButtonState = digitalRead(BTN_PIN);
// Compare the buttonState to its previous state:
if (ButtonState != LastButtonState) {
// if the state has changed, increment the counter
if (ButtonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
PushButtonCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(PushButtonCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing:
delay(50);
}
// save the current state as the last state, for next time through the loop
LastButtonState = ButtonState;
if (PushButtonCounter % 2 == 0) {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
// Send DHT values to serial:
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %, ");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" °C ");
Serial.print(f);
Serial.print(" °F,\t");
Serial.print("Heat index: "); // Human-perceived temperature:
Serial.print(hic);
Serial.print(" °C ");
Serial.print(hif);
Serial.println(" °F");
// Send DHT values to LCD:
lcd.clear();
lcd.setCursor(0,0); // Set cursor top left:
lcd.print(F("Humi: "));
lcd.print(h);
lcd.print(F(" %"));
lcd.setCursor(0,1); // Set cursor bottom left:
lcd.print(F("Temp: "));
lcd.print(t);
lcd.print(F(" "));
lcd.write(223); // Print out degree symbol:
lcd.print(F("C"));
} else {
// Read temperature as Celsius:
value = analogRead(TMP_PIN); // Read from analog pin:
volts = ((value*5.0)/1024.0); // Conversion to volts:
tempC = ((volts)-0.5)/0.01; // Convert millivolts into temperature:
// Send Temperature values to serial:
Serial.print(F("ADC: "));
Serial.print(value,2); // Print ADC value
Serial.print(", \t");
Serial.print(F("Volts: "));
Serial.print(volts,2);
Serial.print(F(" mV, \t"));
Serial.print("Temperature: ");
Serial.print(tempC,2);
Serial.println(F(" °C"));
// Send Temperature values to LCD:
lcd.clear();
lcd.setCursor(0,0); // Set cursor top left:
lcd.print(F("Volt: "));
lcd.print(volts,2);
lcd.print(F(" mV"));
lcd.setCursor(0,1); // Set cursor bottom left:
lcd.print(F("Temp: "));
lcd.print(tempC,2);
lcd.print(F(" "));
lcd.write(223); // Print out degree symbol:
lcd.print(F("C"));
}
delay (2000);
} // End loop:
Perché la " if (PushButtonCounter % 2 == 0) {" la devi mettere dentro alla "if (ButtonState == HIGH) {" altrimenti la esegui sempre.
Per il resto, visto che fai "PushButtonCounter % 2 == 0" la variabile PushButtonCounter per me non serve di tipo intero ma di fatto è una bool... Ma la cosa peggiore è che la incrementi senza mai resettarla, quindi teoricamente essendo una "int" dopo 32,767 pressioni vai in overflow.
docdoc:
Perché la " if (PushButtonCounter % 2 == 0) {" la devi mettere dentro alla "if (ButtonState == HIGH) {" altrimenti la esegui sempre.
Ciao docdoc,
grazie per i tuoi suggerimenti.
Ho modificato lo sketch ma in questo modo i dati non vengono aggiornati, puoi darmi qualche altro suggerimento?
Allego il codice.
// Import needed library:
#include "DHT.h" // Library for communication with DHTxx:
#include "DHT_U.h" // Library for communication with DHTxx:
#include "Wire.h" // Library for communicating with the LCD:
#include "LiquidCrystal_I2C.h" // Library for communication with the LCD:
// Initializing variables:
#define DHT_PIN 11 // Pin that the DHT probe is attached to:
#define BTN_PIN 10 // Pin that the Push Button is attached to:
#define TMP_PIN A0 // Pin that the Temperature proble is attached to:
// Uncomment whatever type you're using!
#define DHT_TYPE DHT11 // DHT 11
//#define DHT_TYPE DHT22 // DHT 22 (AM2302, AM2321)
//#define DHT_TYPE DHT21 // DHT 21 (AM2301)
float value = 0; // Store the value coming from the sensor:
float volts = 0; // Store the volt coming from the sensor:
float tempC = 0; // Store the temperature coming from the sensor;
int ButtonState = 0; // Current state of the button:
int PushButtonCounter = 0; // Counter for the number of button presses:
int LastButtonState = 0; // Previous state of the button:
boolean DEBUG = true; // Set to true if you want to read output on the Serial:
// Declare object:
LiquidCrystal_I2C lcd(0x27,16,2); // Set the LCD address to 0x27 in PCF8574 for a 16 chars and 2 line display:
DHT dht(DHT_PIN, DHT_TYPE); // Store an instance of the DHT sensor:
// The setup function runs once when you press reset or power the board:
void setup () {
pinMode(BTN_PIN, INPUT); // Initialize the button pin as a input:
Wire.begin();
dht.begin(); // Start the DHT library code:
// Start LCD library to output our messages:
lcd.init(); // Start LCD to output our messages:
lcd.backlight(); // Turn ON backlight:
lcd.clear(); // Clears the LCD screen:
lcd.setCursor(0,0);
lcd.print(F("Sensor"));
lcd.setCursor(0,1);
lcd.print(F("initialising..."));
if (DEBUG) { // Start serial to output our messages:
Serial.begin(9600);
delay(10);
}
while (!Serial) {
; // Wait for serial port to connect. Needed for native USB port only:
}
Serial.println(F("\nSetup done"));
delay(2000);
} // End setup:
// The loop function runs over and over again forever:
void loop () {
// Read the pushbutton input pin:
ButtonState = digitalRead(BTN_PIN);
// Compare the buttonState to its previous state:
if (ButtonState != LastButtonState) {
// if the state has changed, increment the counter
if (ButtonState == HIGH) {
if (PushButtonCounter % 2 == 0) {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
// Send DHT values to serial:
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %, ");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" °C ");
Serial.print(f);
Serial.print(" °F,\t");
Serial.print("Heat index: "); // Human-perceived temperature:
Serial.print(hic);
Serial.print(" °C ");
Serial.print(hif);
Serial.println(" °F");
// Send DHT values to LCD:
lcd.clear();
lcd.setCursor(0,0); // Set cursor top left:
lcd.print(F("Humi: "));
lcd.print(h);
lcd.print(F(" %"));
lcd.setCursor(0,1); // Set cursor bottom left:
lcd.print(F("Temp: "));
lcd.print(t);
lcd.print(F(" "));
lcd.write(223); // Print out degree symbol:
lcd.print(F("C"));
} else {
// Read temperature as Celsius:
value = analogRead(TMP_PIN); // Read from analog pin:
volts = ((value*5.0)/1024.0); // Conversion to volts:
tempC = ((volts)-0.5)/0.01; // Convert millivolts into temperature:
// Send Temperature values to serial:
Serial.print(F("ADC: "));
Serial.print(value,2); // Print ADC value
Serial.print(", \t");
Serial.print(F("Volts: "));
Serial.print(volts,2);
Serial.print(F(" mV, \t"));
Serial.print("Temperature: ");
Serial.print(tempC,2);
Serial.println(F(" °C"));
// Send Temperature values to LCD:
lcd.clear();
lcd.setCursor(0,0); // Set cursor top left:
lcd.print(F("Volt: "));
lcd.print(volts,2);
lcd.print(F(" mV"));
lcd.setCursor(0,1); // Set cursor bottom left:
lcd.print(F("Temp: "));
lcd.print(tempC,2);
lcd.print(F(" "));
lcd.write(223); // Print out degree symbol:
lcd.print(F("C"));
}
// if the current state is HIGH then the button went from off to on:
PushButtonCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(PushButtonCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing:
delay(50);
}
// save the current state as the last state, for next time through the loop
LastButtonState = ButtonState;
} // End loop:
il loop, prima c'era delay(2000) ora solo delay(50)
ti ho separato le funzioni di scrittura su lcd e di lettura dei dati così segui meglio il programma...
/*
FILE: push.buttons.01NOV18
TAG: DHT11, DHT, Humidity, Temperature, LCD, PUSH BUTTON, TMP36
PURPOSE: Example testing sketch for various DHT
humidity / temperature sensors
PROBES : DHT11, TMP36
ARDUINO : 1.8.4
DISPLAY : LiquidCrystal_I2C 16x2 PCF8574T
COMFORT : HR 45 %, 21°C
LOCATION : Monteriggioni (200m slm)
AUTHOR : Antonio Cannavale
LIBRARY : Adafruit DHT - sensor - library
LiquidCrystal_I2C.h, Wire.h
REFERENCE : https : //github.com/adafruit/DHT-sensor-library
https : //github.com/marcoschwartz/LiquidCrystal_I2C
DHT11 connect to Arduino as follow :
GND -> GND
VCC -> +5V
DIG -> Digital pin
LCD connect to Arduino as follow :
GND -> GND
VCC -> +5V
SDA -> SDA
SCL -> SCL
TMP36 connected to Arduino as follow :
GND -> GND
SIG -> Analog pin
DIG -> Digital pin
VCC -> +5V
Push button connect to Arduino as follow :
GND -> GND
VCC -> +5V
DIG -> Digital pin
*/
// Import needed library:
#include "DHT.h" // Library for communication with DHTxx:
#include "DHT_U.h" // Library for communication with DHTxx:
#include "Wire.h" // Library for communicating with the LCD:
#include "LiquidCrystal_I2C.h" // Library for communication with the LCD:
// Initializing variables:
#define DHT_PIN 11 // Pin that the DHT probe is attached to:
#define BTN_PIN 10 // Pin that the Push Button is attached to:
#define TMP_PIN A0 // Pin that the Temperature proble is attached to:
// Uncomment whatever type you're using!
#define DHT_TYPE DHT11 // DHT 11
float value = 0.0; // Store the value coming from the sensor:
float volts = 0.0; // Store the volt coming from the sensor:
float tempC = 0.0; // Store the temperature coming from the sensor;
int ButtonState = 0; // Current state of the button:
bool PushButtonCounter = false; // Counter for the number of button presses:
int LastButtonState = 0; // Previous state of the button:
float h = 0.0;
float t = 0.0;
float f = 0.0;
float hic = 0.0;
float hif = 0.0;
boolean DEBUG = true; // Set to true if you want to read output on the Serial:
// Declare object:
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 in PCF8574 for a 16 chars and 2 line display:
DHT dht(DHT_PIN, DHT_TYPE); // Store an instance of the DHT sensor:
// The setup function runs once when you press reset or power the board:
void setup () {
pinMode(BTN_PIN, INPUT); // Initialize the button pin as a input:
Wire.begin();
dht.begin(); // Start the DHT library code:
// Start LCD library to output our messages:
lcd.init(); // Start LCD to output our messages:
lcd.backlight(); // Turn ON backlight:
lcd.clear(); // Clears the LCD screen:
lcd.setCursor(0, 0);
lcd.print(F("Sensor"));
lcd.setCursor(0, 1);
lcd.print(F("initialising..."));
delay(2000);
} // End setup:
// The loop function runs over and over again forever:
void loop () {
// Read the pushbutton input pin:
ButtonState = digitalRead(BTN_PIN);
if (ButtonState != LastButtonState) {
if (ButtonState == HIGH) {
if (PushButtonCounter) { // se è vero entra nell'if (passaggio pari)
readDht(); // legge dal dht
printLcdDht(); // e stampa sull'lcd
} else { // se è falso entra nell'else (passaggio dispari)
readTmp(); // legge dal tmp36
printLcdTmp(); // e stampa sull'lcd
}
PushButtonCounter=!PushButtonCounter; // diventa il contrario (da true a false e viceversa)
}
}
LastButtonState = ButtonState;
delay(2000); // 1 lettura ogni 2 secondi
} // End loop:
//funzione di stampa sull'lcd i dati del DHT
void printLcdDht() {
// Send DHT values to LCD:
lcd.clear();
lcd.setCursor(0, 0); // Set cursor top left:
lcd.print(F("Humi: "));
lcd.print(h);
lcd.print(F(" %"));
lcd.setCursor(0, 1); // Set cursor bottom left:
lcd.print(F("Temp: "));
lcd.print(t);
lcd.print(F(" "));
lcd.write(223); // Print out degree symbol:
lcd.print(F("C"));
}
//funzione di stampa sull'lcd i dati del TMP36
void printLcdTmp() {
lcd.clear();
lcd.setCursor(0, 0); // Set cursor top left:
lcd.print(F("Volt: "));
lcd.print(volts, 2);
lcd.print(F(" mV"));
lcd.setCursor(0, 1); // Set cursor bottom left:
lcd.print(F("Temp: "));
lcd.print(tempC, 2);
lcd.print(F(" "));
lcd.write(223); // Print out degree symbol:
lcd.print(F("C"));
}
// funzione lettura dati da DHT
void readDht() {
h = dht.readHumidity();
// Read temperature as Celsius (the default)
t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
f = dht.readTemperature(true);
// Compute heat index in Fahrenheit (the default)
hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
hic = dht.computeHeatIndex(t, h, false);
}
// funzione lettura dati da TMP
void readTmp() {
// Read temperature as Celsius:
value = analogRead(TMP_PIN); // Read from analog pin:
volts = ((value * 5.0) / 1024.0); // Conversion to volts:
tempC = (volts - 0.5) / 0.01; // Convert millivolts into temperature:
}
Patrick_M:
il loop, prima c'era delay(2000) ora solo delay(50)
ti ho separato le funzioni di scrittura su lcd e di lettura dei dati così segui meglio il programma...
Ciao,
ho provato il tuo sketch ma ho lo stesso problema dell'inizio cioè: devo premere e ri-premere più volte per cambiare visualizzazione.
ho sbagliato io, il delay in fondo non ci vuole ma ci vuole il debounce sul pulsante....
/*
FILE: push.buttons.01NOV18
TAG: DHT11, DHT, Humidity, Temperature, LCD, PUSH BUTTON, TMP36
PURPOSE: Example testing sketch for various DHT
humidity / temperature sensors
PROBES : DHT11, TMP36
ARDUINO : 1.8.4
DISPLAY : LiquidCrystal_I2C 16x2 PCF8574T
COMFORT : HR 45 %, 21°C
LOCATION : Monteriggioni (200m slm)
AUTHOR : Antonio Cannavale
LIBRARY : Adafruit DHT - sensor - library
LiquidCrystal_I2C.h, Wire.h
REFERENCE : https : //github.com/adafruit/DHT-sensor-library
https : //github.com/marcoschwartz/LiquidCrystal_I2C
DHT11 connect to Arduino as follow :
GND -> GND
VCC -> +5V
DIG -> Digital pin
LCD connect to Arduino as follow :
GND -> GND
VCC -> +5V
SDA -> SDA
SCL -> SCL
TMP36 connected to Arduino as follow :
GND -> GND
SIG -> Analog pin
DIG -> Digital pin
VCC -> +5V
Push button connect to Arduino as follow :
GND -> GND
VCC -> +5V
DIG -> Digital pin
*/
// Import needed library:
#include "DHT.h" // Library for communication with DHTxx:
#include "DHT_U.h" // Library for communication with DHTxx:
#include "Wire.h" // Library for communicating with the LCD:
#include "LiquidCrystal_I2C.h" // Library for communication with the LCD:
// Initializing variables:
#define DHT_PIN 11 // Pin that the DHT probe is attached to:
#define BTN_PIN 10 // Pin that the Push Button is attached to:
#define TMP_PIN A0 // Pin that the Temperature proble is attached to:
// Uncomment whatever type you're using!
#define DHT_TYPE DHT11 // DHT 11
float value = 0.0; // Store the value coming from the sensor:
float volts = 0.0; // Store the volt coming from the sensor:
float tempC = 0.0; // Store the temperature coming from the sensor;
int buttonState = 0; // Current state of the button:
bool PushButtonCounter = false // Counter for the number of button presses:
int LastButtonState = 0; // Previous state of the button:
float h = 0.0;
float t = 0.0;
float f = 0.0;
float hic = 0.0;
float hif = 0.0;
unsigned long debounceDelay = 50;
unsigned long lastDebounceTime = 0;
boolean DEBUG = true; // Set to true if you want to read output on the Serial:
// Declare object:
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 in PCF8574 for a 16 chars and 2 line display:
DHT dht(DHT_PIN, DHT_TYPE); // Store an instance of the DHT sensor:
// The setup function runs once when you press reset or power the board:
void setup () {
pinMode(BTN_PIN, INPUT); // Initialize the button pin as a input:
Wire.begin();
dht.begin(); // Start the DHT library code:
// Start LCD library to output our messages:
lcd.init(); // Start LCD to output our messages:
lcd.backlight(); // Turn ON backlight:
lcd.clear(); // Clears the LCD screen:
lcd.setCursor(0, 0);
lcd.print(F("Sensor"));
lcd.setCursor(0, 1);
lcd.print(F("initialising..."));
delay(2000);
} // End setup:
// The loop function runs over and over again forever:
void loop () {
// Read the pushbutton input pin:
int lettura = digitalRead(BTN_PIN);
if (lettura != LastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (lettura != buttonState) {
buttonState = lettura;
if (buttonState == HIGH) {
if (PushButtonCounter ) { // se vero passaggio pari
readDht(); // legge dal dht
printLcdDht(); // e stampa sull'lcd
} else { // se falso passaggio dispari
readTmp(); // legge dal tmp36
printLcdTmp(); // e stampa sull'lcd
}
PushButtonCounter = !PushButtonCounter; // scambio ad ogni giro (da vero a falso e viceversa)
}
}
}
LastButtonState = lettura;
} // End loop:
void printLcdDht() {
// Send DHT values to LCD:
lcd.clear();
lcd.setCursor(0, 0); // Set cursor top left:
lcd.print(F("Humi: "));
lcd.print(h);
lcd.print(F(" %"));
lcd.setCursor(0, 1); // Set cursor bottom left:
lcd.print(F("Temp: "));
lcd.print(t);
lcd.print(F(" "));
lcd.write(223); // Print out degree symbol:
lcd.print(F("C"));
}
void printLcdTmp() {
lcd.clear();
lcd.setCursor(0, 0); // Set cursor top left:
lcd.print(F("Volt: "));
lcd.print(volts, 2);
lcd.print(F(" mV"));
lcd.setCursor(0, 1); // Set cursor bottom left:
lcd.print(F("Temp: "));
lcd.print(tempC, 2);
lcd.print(F(" "));
lcd.write(223); // Print out degree symbol:
lcd.print(F("C"));
}
void readDht() {
h = dht.readHumidity();
// Read temperature as Celsius (the default)
t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
f = dht.readTemperature(true);
// Compute heat index in Fahrenheit (the default)
hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
hic = dht.computeHeatIndex(t, h, false);
}
void readTmp() {
// Read temperature as Celsius:
value = analogRead(TMP_PIN); // Read from analog pin:
volts = ((value * 5.0) / 1024.0); // Conversion to volts:
tempC = (volts - 0.5) / 0.01; // Convert millivolts into temperature:
}
Si, certo, se è risolto, si.
Ma se il problema era (anche) il debounce, sempre meglio farlo via hardware (condensatore 100nF verso massa, ma per i circuiti dettagliati cerca nel forum o con Google, se ne è parlato parecchie volte..).