Hi All,
I'm pretty new to Arduino/the 'C' Language, just a couple of days, and I have a college assignment where I have to monitor temperature, and raise an alarm if outside set parameters.
This didn't sound challenging enough to be enjoyable or come with any satisfaction on completion!
So far, I have a sensor connected, and an LCD display, it starts with a welcome message, then goes into the main loop with some if statements, and has long flash red and buzz 2 degrees over set temp, short flash 1 degree, green flash normal, short blue fash 1 deg under, and long blue flash and buzz 2 degrees under. As far as my college assignment goes, I've produced something well beyond their expectations. But personally, I want improvements.
I want to allow the user to set a temperature, I've thought of a keypad, but not sure I have enough pins, I've tried an IR remote, but can't get the value from the number i'f pressed into the right position, I've decided I'm going to try and do it with push buttons, we're i'm stuck is:
During the setup, I want to ask the user to set a temperature, one button to increase temp, one to decrease, a 3rd to set temp. after setting the temp it enters the main loop.
the main loop works no problem, it's just the setup I'm stuck with.
Wiring is like this:
Heres my code at the moment:
// 4 red
// 6 green
// 5 blue
// 3 buzzer PWM
// 2 DHT11 Input
// 6 Push button to set temp
// A0 Push button to adjust temp up
// A1 Push button to adjust temp down
#include <dht.h> // include dht (temp sensor)
#include <LiquidCrystal.h> // include LCD display
LiquidCrystal lcd(7,8,9,10,11,12); // LCD display data pins
int tempnum;
dht DHT;
#define DHT11_PIN 2 // temp sensor on pin 2
int settemp = 21;
void TempSet()
{
}
void setup(){
pinMode(4,OUTPUT); // Red LED
pinMode(13, OUTPUT); // Green LED
pinMode(5, OUTPUT); // Blue LED
pinMode(3, OUTPUT); // Buzzer, PWM
pinMode(6, INPUT_PULLUP);
pinMode(A0, INPUT_PULLUP);
pinMode(A1, INPUT_PULLUP);
lcd.begin(16,2); // LCD is 16 characters b 2 rows
lcd.setCursor(0,0); // set cursor position 0 row 0
lcd.print("Temp Prog"); // program name
lcd.setCursor(0,1); // set cursor
lcd.print("By Shaun H."); // my name
delay(5000); // wait 10000 mS
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set Temp 16-25");
lcd.setCursor(0,1);
lcd.print("Current: ");
lcd.print(settemp);
while(digitalRead(6==HIGH));
{
}
delay(3000);
}
void loop() // program begins
{
int chk = DHT.read11(DHT11_PIN); // read DHT temp/humidy values
delay(1000); // delay 1000 mS
lcd.clear();
if(DHT.temperature > settemp+1) // if temp over 22 then
{digitalWrite(4,HIGH); // turn on red LED
analogWrite(3, 60); // turn on buzzer
lcd.setCursor(0,0); //sets top row 1st character
lcd.print("Temp: "); // tells temp
lcd.print(DHT.temperature); // insert temp
lcd.print((char)223); // degrees symbol
lcd.print("C"); // tells degrees C
lcd.setCursor(0,1); // set second row
lcd.print("Humidity: "); // print humidity
lcd.print(DHT.humidity); // print humidity value
lcd.print("%"); // print %
delay(5000); // wait 5 seconds
digitalWrite(4,LOW); // turn off LED
digitalWrite(3,LOW); // turn off Buzzer
delay(1000); // wait 1 second start again
}
else if(DHT.temperature > settemp) // otherwise, if over 21 degrees
{digitalWrite(4,HIGH); // turn on red LED
lcd.setCursor(0,0); //print temp & humidity details to LCD
lcd.print("Temp: ");
lcd.print(DHT.temperature);
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Humidity: ");
lcd.print(DHT.humidity);
lcd.print("%");
delay(1000); // wait 1 second
digitalWrite(4,LOW); // turn off LED
delay(1000); // wait one second start again
}
else if(DHT.temperature < settemp-1) // otherwise, if temp under 19 degrees
{digitalWrite(5,HIGH); // turn on blue LED
analogWrite(3, 60); // turn on buzzer
lcd.setCursor(0,0); //print temp & humidity details to LCD
lcd.print("Temp: ");
lcd.print(DHT.temperature);
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Humidity: ");
lcd.print(DHT.humidity);
lcd.print("%");
delay(5000); // wait 5 seconds
digitalWrite(5,LOW); // turn off LED
digitalWrite(3,LOW); // turn off buzzer
delay(1000); // wait 1 second start again
}
else if(DHT.temperature < settemp) // otherwsie if temp under 20 degrees
{digitalWrite(5,HIGH); // turn on blue LED
lcd.setCursor(0,0); //print temp & humidity details to LCD
lcd.print("Temp: ");
lcd.print(DHT.temperature);
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Humidity: ");
lcd.print(DHT.humidity);
lcd.print("%");
delay(1000); // wait 1 second
digitalWrite(5,LOW); // turn off blue LED
delay(1000); // wait 1 second start again
}
else // otherwise do this
{digitalWrite(13,HIGH); // turn on green LED
lcd.setCursor(0,0); //print temp & humidity details to LCD
lcd.print("Temp: ");
lcd.print(DHT.temperature);
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Humidity: ");
lcd.print(DHT.humidity);
lcd.print("%");
delay(1000); // wait 1 second
digitalWrite(13,LOW); // turn off LED
delay(1000); // wait 1 second start again
}
}
Any help with setting up the correct while code or another way of achieving the desired operation would be appreciated.
Thanks, Shaun