I'm building a temperature monitor/controller for a kegerator with an LCD display and need some help cleaning this code up ....
/*
Purpose: Temperature Regulator for a Kegerator with LCD display.
Using a LM35 and Arduino to check temperature and display current temp to an LCD.
Also the Arduino will turn a Solid State Relay on/off within a defined temp range
-LM35 connected to 5V, Grd, Analog Pin 0
-Serial LCD connected to 5V, Grd, Digital Pin 1 (tx pin)
-SS Relay connected to Grd, Digital Pin 12
-Back Light Button connected to Digital Pin 3, Grd, 5V via 10K resistor (pin pulled HIGH in open state).Most of this code was borrowed heavily from BRUTUS @ brutusweb.com .. thanks!
*/#define ledPin 13 // LED connected to digital pin 13
#define lm35 0 // LM35 on Analog 0
#define blightPin 3 // Momentary switch for back light to digital pin 3
#define relayPin 12 // Relay connected to digital pin 12int buttonState = 0 //state of back light button
void sendlcd(byte command) //Quick function to send LCD commands.
{
Serial.print(command, BYTE);
}void setup() // run once, when the sketch starts
{
analogReference(INTERNAL); //using internal voltage reference ~1.1v
pinMode(ledPin, OUTPUT); // sets the digital pin as output
pinMode(lm35, INPUT); // sets the analog pin as input
pinMode(blightPin, INPUT); // sets the digital pin as input
pinMode(relayPin, OUTPUT); // sets the digital pin as output
digitalWrite(ledPin, HIGH);delay(1000);
Serial.begin(9600);
delay(1000); //Let Ports Stabilizesendlcd(27); //Reset Command For LCD
sendlcd(122);delay(500); //Full Reset takes a bit
sendlcd(254); //Clear and Go Home on LCD
sendlcd(1);
digitalWrite(ledPin, LOW); //turn off LED
delay(250);
}void loop() // run for eternity
{
long temp;
long temp1;
long temp2;
long temp_x;
int lm35_val=0;digitalWrite(ledPin,HIGH);
delay(500); //After digital write, delay to allow stabilized analog read
lm35_val=analogRead(lm35); //read value of center leg of LM35
temp = lm35_val; //output voltage of LM35, 10mV = 1 Degree Celcius
temp1=temp/10; //creates true Celcius reading
temp2=temp%10; //modulus operation to calculate remainder for higher resolution readingif (temp2 >= 5)
{
temp_x++;
}sendlcd(254); //Clear LCD Screen
sendlcd(1);
Serial.println("Current Temp"); //Print "Current Temp" message
Serial.print(temp1); //Print Celsius Temperature
Serial.print("."); //Print decimal place
Serial.print(temp2); //Print "Tenths" place
sendlcd(223); //Print "degree" character
Serial.print("C"); //Print "C" for CelsiusdigitalWrite(ledPin,LOW); //Turn off LED
/* Fahrenheit Conversion in progress (needs some help........)
Serial.print (((long)temp1 * 9)/ 5 + 32); // convert Celsius to Fahrenheit and print
Serial.print (223);
Serial.print ("F"); // print Fahrenheit label*/
// To turn back light on LCD on for 10 seconds...
buttonState = digitalRead(blightPin); // read the back light switch input:
if (buttonState == LOW) // if the switch is closed:
{sendlcd(27); //Turn back light on (ASCII for ESC)
sendlcd(42); //ASCII for *
sendlcd(255); // backlight value (0)=off (255)=full on
delay(10000); // wait 10 seconds
sendlcd(27); //Turn back light off
sendlcd(42);
sendlcd(0);
}if ((temp1) > 7 ) //check temp is above x degrees
{
digitalWrite (relayPin, HIGH); //if temp is above x degrees turn pin "ON"
}
else if ((temp 1) < 4 ) //check temp is below x degrees
{
digitalWrite (relayPin, LOW); //if temp is below x degree turn pin "OFF"
}delay(20000); //Check temp every 20 seconds
}
The arduino is reading an LM35 for the temperature and turning a Solid State Relay on and off depending on the set temperature, and finally displaying the temp. on a serial LCD panel....
My biggest problem is trying to fix the code on calling the back-light on/off..... there is a momentary switch connected that, when press, should turn on the LCD back-light for 10 seconds
How can I avoid the use of Delay's and have the arduino listen for a button press while still running the temperature checking code?
Any pointers are much appreciated!! Thanks!