Hi All !!
I've written a code for my room thermostat and that works fine. But now I have a problem that whe the temperature rises and the led was HIGH before it won't change to LOW when it fall's below temperature trigger. But if the previous button state was LOW everything works fine and the led turns LOW. So I need to change the button state to LOW. but how ??
here is the code:
#include <LiquidCrystal.h>
const int button = A3;
const int button1 = A4;
const int ledPin1 = 8;
const int ledPin = 9;
const int TEMPTRIGGER = 22.00;
const int TEMPTRIGGER1 = 25.00;
int state1 = LOW;
int state2 = LOW;
int read1 = 0;
int read2 = 0;
int previous = LOW;
long time = 0;
long debounce = 200;
float temperature = 0; // this stores the value for
LiquidCrystal lcd(10, 11, 12, 13, 14, 15, 16);
void setup()
{
pinMode(button, INPUT);
pinMode(button1, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
lcd.begin(16, 2); // tells Arduino the LCD dimensions
lcd.setCursor(0,0);
lcd.print("Made By Elvis K.");
// print text and move cursor to start of next line
lcd.setCursor(0,1);
lcd.print("Prosim pocakaj...");
delay(2000);
lcd.clear(); // clear LCD screen
lcd.setCursor(0,0);
lcd.print("Temp je:");
lcd.setCursor(0,1);
lcd.print("Pump:1");
lcd.setCursor(11,1);
lcd.print("2");
}
void loop()
{
read1 = digitalRead(button);
read2 = digitalRead(button1);
if (read1 == HIGH && previous == LOW && millis() - time > debounce) {
if (state1 == HIGH)
state1 = LOW;
else
state1 = HIGH;
time = millis();
}
digitalWrite(ledPin, state1);
previous = read1;
if (read2 == HIGH && previous == LOW && millis() - time > debounce) {
if (state2 == HIGH)
state2 = LOW;
else
state2 = HIGH;
time = millis();
}
digitalWrite(ledPin1, state2);
previous = read2;
if(temperature >= TEMPTRIGGER || state1 == HIGH) {
digitalWrite( ledPin, HIGH);
lcd.setCursor(7,1);
lcd.print("ON ");
}
else if (temperature <= TEMPTRIGGER){
digitalWrite( ledPin, LOW);
digitalWrite(state1, LOW);//Here is the problem <<<<<<<<<<<< i dont know wich command to use
lcd.setCursor(7,1);
lcd.print("OFF");
}
if(temperature >= TEMPTRIGGER1 || state2 == HIGH ) {
digitalWrite(ledPin1, HIGH);
lcd.setCursor(13,1);
lcd.print("ON");
}
else if (temperature <= TEMPTRIGGER1 || state2 == LOW || state2 == HIGH ){
digitalWrite( ledPin1, LOW);
lcd.setCursor(13,1);
lcd.print("OFF");
}
temperature = analogRead(5); // store value from temp brick
temperature = temperature +252-500;
temperature = temperature / 10;
// maths to convert reading to temperature in Celsius.
// may need calibrating, by comparing with real thermometer
// and adjusting value of -500
delay (100); // wait for 100 milliseconds
lcd.print(" Temperature is ");
lcd.setCursor(9,0);
// move cursor to first character of second line
lcd.print(temperature);
lcd.println("C. ");
delay(500); // wait half a second
}
Thanks