Hello, I am doing a school project that is based on measuring temperatures. As you can see on the image there are 6 LEDs (2 red 2 green and 2 blue) , red means the temp is higher then 37ºC , blue means is lower then 10 and green means its between 36-37. It also has a LCD and 2 buttons ON/OFF
The problem is all on the code, it wont show the temperatures on the LCD properly, only if I go around randomly clicking on the buttons it will show a frame of the temperatures but they wont vary anyway and also after some time it shows, it goes off again. Same for the LEDs. I think the whole problem is on the buttons, I don’t know what’s wrong on the code…
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Streaming.h>
#define DS18B20_SENSOR1 2 // pino de dados do DS18D20
#define DS18B20_SENSOR2 4 // pino de dados do DS18D20
int buttonState1 = 0;
int buttonState2 = 0;
float Temp1, Temp2;
OneWire oneWire1(DS18B20_SENSOR1); // Objeto do baramento One-wire
DallasTemperature sensor1(&oneWire1); // Objeto do sensor DS18D20
OneWire oneWire2(DS18B20_SENSOR2); // Objeto do baramento One-wire
DallasTemperature sensor2(&oneWire2); // Objeto do sensor DS18D20
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup(void)
{
Serial.begin(3000);
Serial << F("Teste do Sensor 1") << endl << endl;
Serial << F("Teste do Sensor 2") << endl << endl;
sensor1.begin();
sensor2.begin();
lcd.begin();
lcd.setBacklight(HIGH);
pinMode(5, INPUT);
pinMode(7, INPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
sensor1.begin();
sensor2.begin();
}
void loop()
{
sensor1.requestTemperatures();
Temp1 = sensor1.getTempCByIndex(0);
sensor2.requestTemperatures();
Temp2 = sensor2.getTempCByIndex(0);
buttonState1 = digitalRead(5);
buttonState2 = digitalRead(7);
if (buttonState2 == LOW){
if(Temp1 < 36){
digitalWrite(13, HIGH);
//desliga os outros
digitalWrite(11, LOW);
digitalWrite(12, LOW);
}else if(Temp1 > 38){
digitalWrite(11, HIGH);
//desliga os outros
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}else{
digitalWrite(12, HIGH);
//desliga os outros
digitalWrite(11, LOW);
digitalWrite(13, LOW);
}}
else {
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);}
if (buttonState1 == LOW){
if(Temp2 < 0){
digitalWrite(10, HIGH);
//desliga os outros
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}else if(Temp2 > 2){
digitalWrite(8, HIGH);
//desliga os outros
digitalWrite(9, LOW);
digitalWrite(10, LOW);
}else{
digitalWrite(9, HIGH);
//desliga os outros
digitalWrite(8, LOW);
digitalWrite(10, LOW);
}}
else{
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
}
if (buttonState2 == LOW){
lcd.setCursor (0,0);
lcd.print("Sensor1: ");
lcd.print(Temp1);
lcd.print("C");
}
else{lcd.setCursor (0,0);
lcd.print("Sensor1: ");
lcd.print(" off ");
}
if (buttonState1 == LOW){
lcd.setCursor (0,1);
lcd.print("Sensor2: ");
lcd.print(Temp2);
lcd.print("C");
}
else{ lcd.setCursor (0,1);
lcd.print("Sensor2: ");
lcd.print(" off ");}
}
Please help :C!!