I am a beginner and I like to copy projects that I find online to learn how to use arduino.
I have made a thermometer with LCD display 16X2. The potentiometer controls the contrast and I would use the button to turn on and off the display backlight.
The thermometer works perfectly and the display shows the temperature but the button does not turn on the light.
In the attached file of the circuit, the cable “button to PIN9” missing but in reality it is physically connected.
Why my code not work?
Where I’m wrong?
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
// Il terminale data del sensore è connesso
// alla porta 2 di Arduino
#define ONE_WIRE_BUS 7
// Imposta la comunicazione oneWire per comunicare
// con un dispositivo compatibile
OneWire oneWire(ONE_WIRE_BUS);
// Passaggio oneWire reference alla Dallas Temperature.
DallasTemperature sensors(&oneWire);
// RS EN D4 D5 D6 D7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int button_blink = 9; //BOTTONE CHE ACCENDE LA RETROILLUMINAZIONE DEL DISPLAY
const int power_blink = 13; //PIN CHE ALIMENTA L'ACCENSIONE DEL DISPLAY a 5V
int statoBottone = 0;
void setup(void)
{
// Start up the library
sensors.begin();
// Imposta il valore di righe e colonne del display LCD
lcd.begin(16, 2);
pinMode(button_blink, INPUT);
pinMode(power_blink, OUTPUT);
}
void loop(void)
{
statoBottone = digitalRead(button_blink);
if (button_blink == HIGH) {
digitalWrite(power_blink, HIGH); } else {
digitalWrite(power_blink, LOW);}
sensors.requestTemperatures(); // Invia il comando di lettura delle temperatura
lcd.clear();
lcd.setCursor(0, 0); // bottom left
lcd.print("Temperatura di: ");
lcd.setCursor(0, 1); // bottom left
lcd.print (sensors.getTempCByIndex(0));
lcd.print (" C");
}
In the attached file of the circuit, the cable "button to PIN9" missing but in reality it is physically connected.
What does this mean? You need to fix your circuit diagram if we are to understand how your switch is wired. Your diagram shows pin 9 connected to the LCD, not the switch.
It would be easier to connect one leg of the switch to ground, one leg to the digital pin, and to turn on the internal pullup resistor.
/*
Andrew Mascolo
1/15/2013
Simple button latch
*/
byte LEDpin = 13; //on-board LED
byte ButtonPin = 2; //digital pin 2
int button, lastState = LOW;
int lastButtonState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
boolean latch = false;
void setup() {
pinMode(LEDpin, OUTPUT);
pinMode(ButtonPin, INPUT);
}
void loop() {
button = digitalRead(ButtonPin);
if ( button != lastButtonState) {
lastDebounceTime = millis();
lastButtonState = button;
}
if ((millis() - lastDebounceTime) > debounceDelay) { // debounce loop
if (button == HIGH && button != lastState) { //If button is pressed (in this case, high) and it does not equal its last recorded state
latch = !latch; // When the conditions above are TRUE, latch(set to 0) becomes 1 and vs.
if(latch == true)
{
digitalWrite(LEDpin, HIGH);
}
else
{
digitalWrite(LEDpin, LOW);
}
/* This is the exact same representation */
//latch ? digitalWrite(LEDpin, HIGH): digitalWrite(LEDpin, LOW);
//This works too, digitalWrite(LEDpin, latch ? HIGH : LOW)
} // end of button check
lastState = button; //Update last state
} // end of debounce loop
} // end of loop
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
// Il terminale data del sensore è connesso
// alla porta 2 di Arduino
#define ONE_WIRE_BUS 7
// Imposta la comunicazione oneWire per comunicare
// con un dispositivo compatibile
OneWire oneWire(ONE_WIRE_BUS);
// Passaggio oneWire reference alla Dallas Temperature.
DallasTemperature sensors(&oneWire);
// RS EN D4 D5 D6 D7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int powerBlink = 13; //PIN CHE ALIMENTA L'ACCENSIONE DEL DISPLAY a 5V
const int buttonPin = 9; //BOTTONE CHE ACCENDE LA RETROILLUMINAZIONE DEL DISPLAY
int bottone, statoPrecedente = LOW;
int statoPrecedenteBottone = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
boolean latch = false;
void setup(void)
{
// Start up the library
sensors.begin();
// Imposta il valore di righe e colonne del display LCD
lcd.begin(16, 2);
pinMode(buttonPin, INPUT);
pinMode(powerBlink, OUTPUT);
}
void loop(void)
{
bottone = digitalRead(buttonPin);
if (bottone != statoPrecedenteBottone) {
lastDebounceTime = millis();
statoPrecedenteBottone = bottone;
}
if ((millis() - lastDebounceTime) > debounceDelay) { // debounce loop
if (bottone == HIGH && bottone != statoPrecedente) { //If button is pressed (in this case, high) and it does not equal its last recorded state
latch = !latch; // When the conditions above are TRUE, latch(set to 0) becomes 1 and vs.
if(latch == true)
{
digitalWrite(buttonPin, HIGH);
}
else
{
digitalWrite(powerBlink, LOW);
}
/* This is the exact same representation */
//latch ? digitalWrite(LEDpin, HIGH): digitalWrite(LEDpin, LOW);
//This works too, digitalWrite(LEDpin, latch ? HIGH : LOW)
} // end of button check
statoPrecedente = bottone; //Update last state
} // end of debounce loop
sensors.requestTemperatures(); // Invia il comando di lettura delle temperatura
lcd.clear();
lcd.setCursor(0, 0); // bottom left
lcd.print("Temperatura di: ");
lcd.setCursor(0, 1); // bottom left
lcd.print (sensors.getTempCByIndex(0));
lcd.print (" C");
} // end of loop
You have bottone, statoPrecedente, and statoPrecedenteBottone that all appear to refer to the state of the switch now or last time through loop(). You should have only 2. I find it easier to understand code that uses names like currState and prevState.
The switch is either pressed this time, or it isn't. It was either pressed last time, or it wasn't. There is no third thing to keep track of.
How long does it take to get temperatures to display? If it takes longer than 50 milliseconds to get the temperature and display the data on the LCD, then the whole business with debouncing the switch (and 50 is a long time for a switch to bounce) is unnecessary.