Components and supplies
Bulb (as a warm body)
Test Accessory, AC Power Adaptor
Resistor 220 ohm
relay 5v to 220v
Tactile Switch, Top Actuated
LED (generic)
Alphanumeric LCD, 16 x 2
Arduino UNO
Project description
Code
Tempeh.ino
h
This code based on 16x2 seeed LCD with I2C interface, so edit the library if you use any other device. Also this code contains "bulb on" counter feature - (represented by state_change_counter integer) that collect amount of bulb toggles.
1#include <Wire.h> // I2C/TWI comunication library 2#include "rgb_lcd.h" // "Grove" lcd library 3#include <dht.h> // sensor library using lib from https://www.ardumotive.com/how-to-use-dht-22-sensor-en.html 4 5 6 7#define DHTTYPE DHT22 // defines sensor type - DHT 22 (AM2302) 8 9 10 11// replacements 12dht DHT; 13rgb_lcd lcd; 14 15 16 17// pins set up 18const int DHT22_PIN = 2; // set up DHT22 19const int Up_buttonPin = 12; // set up the pushbutton (+) 20const int Down_buttonPin = 13; // set up the pushbutton (-) 21const int Bulb_ON = 3; // set up the ON led (green) 22const int Bulb_OFF = 5; // set up the OFF led (red) 23const int Relay = 4; // set up the relay 24 25 26 27// variables 28int Temp_limit = 30; // counter for the number of button presses 29int up_buttonState = 0; // current state of the up button 30int up_lastButtonState = 0; // previous state of the up button 31int down_buttonState = 0; // current state of the up button 32int down_lastButtonState = 0; // previous state of the up button 33int i; // loop counter 34int state_change_counter = 0; // num of state change during the procces 35float hum; // stores humidity value 36float temp; // stores temperature value 37bool bPress = false; // helps us to detect when the state of buttons has changed 38bool state_helper = true; // helps to determine num of state change during the procces 39 40 41 42void setup () 43{ 44 Serial.begin (9600); 45 // set up I/O 46 pinMode (Up_buttonPin , INPUT_PULLUP); 47 pinMode (Down_buttonPin , INPUT_PULLUP); 48 pinMode (Relay, OUTPUT); 49 pinMode (Bulb_OFF, OUTPUT); 50 pinMode (Bulb_ON, OUTPUT); 51 lcd.begin (16, 2); // set up the LCD's number of columns and rows: 52 PrintTLimit (); 53 delay (2000); 54 PrintTandH (); 55 digitalWrite (Relay, LOW); 56 digitalWrite (Bulb_OFF, LOW); 57 digitalWrite (Bulb_ON, LOW); 58} 59 60 61 62void loop () 63{ 64 if (temp <= Temp_limit - 0.5) 65 { 66 while (temp < Temp_limit + 0.5) 67 { 68 Relay_ON (); 69 if (state_helper) // counts relay state changes 70 { 71 state_change_counter ++; 72 state_helper = false; 73 } 74 checkUp (); 75 checkDown (); 76 if (temp >= Temp_limit + 0.5) 77 break; 78 if (bPress) 79 Delay_screen (); 80 PrintTandH (); 81 } 82 } 83 else 84 { 85 Relay_OFF (); 86 state_helper = true; 87 checkUp (); 88 checkDown (); 89 if (bPress) 90 Delay_screen (); 91 PrintTandH (); 92 } 93} 94 95 96 97// checks UP button 98void checkUp () 99{ 100 up_buttonState = digitalRead (Up_buttonPin); 101 // compare the buttonState to its previous state 102 if (up_buttonState != up_lastButtonState) 103 { 104 if (up_buttonState == LOW) // if the current state is LOW then the button went from on to off: 105 { 106 bPress = true; 107 Temp_limit ++; 108 i = 0; 109 } 110 delay (50); // delay to avoid bouncing 111 } 112 up_lastButtonState = up_buttonState; // save the current state as the last state, for next time through the loop 113} 114 115 116 117// checks DOWN button 118void checkDown () 119{ 120 down_buttonState = digitalRead (Down_buttonPin); 121 if (down_buttonState != down_lastButtonState) // compares the buttonState to its previous state 122 { 123 // if the state has changed, increment the counter 124 if (down_buttonState == LOW) // if the current state is LOW then the button went from on to off: 125 { 126 bPress = true; 127 Temp_limit --; 128 i = 0; 129 } 130 delay (50); // delay to avoid bouncing 131 } 132 down_lastButtonState = down_buttonState; // save the current state as the last state, for next time through the loop 133} 134 135 136 137// turns on the relay 138void Relay_ON () 139{ 140 // configuration for Relay and leds 141 digitalWrite (Bulb_OFF, LOW); 142 digitalWrite (Relay, HIGH); 143 analogWrite (Bulb_ON, 10); // reduces power supply to decrease the led's brightness 144} 145 146 147 148// turns off the relay 149void Relay_OFF () 150{ 151 // configuration for Relay and leds 152 analogWrite (Bulb_OFF, 40); // reduces power supply to decrease the led's brightness 153 digitalWrite (Relay, LOW); 154 digitalWrite (Bulb_ON, LOW); 155} 156 157 158 159// captures temperature limit screen for couple seconds 160void Delay_screen () 161{ 162 Save_Mode (); 163 for (i = 0; i < 10; ++i) // delays the temperature limit print on LCD 164 { 165 checkUp (); 166 checkDown (); 167 PrintTLimit (); 168 } 169 bPress = false; 170 PrintTandH (); 171} 172 173 174 175// configuration for Relay and leds, saves the system from overheating while pushing buttons 176void Save_Mode () 177{ 178 digitalWrite (Relay, LOW); 179 analogWrite (Bulb_OFF, 40); 180 digitalWrite (Bulb_ON, LOW); 181 lcd.clear (); 182} 183 184 185 186// prints tempearure limit screen 187void PrintTLimit () 188{ 189 lcd.clear (); 190 lcd.setCursor (0, 0); 191 lcd.print ("Limit (T)"); 192 lcd.setCursor (2,1); 193 lcd.print (Temp_limit); 194 lcd.setCursor (6,1); 195 lcd.print ("O/I="); 196 lcd.setCursor (10,1); 197 lcd.print (state_change_counter); 198 delay (100); // delay to avoid LCD bouncing 199} 200 201 202 203// prints the main screen with temperature and humidity observation 204void PrintTandH () 205{ 206 lcd.clear (); 207 int chk = DHT.read22 (DHT22_PIN); 208 hum = DHT.humidity; // read humidity data and store it to variable hum 209 temp = DHT.temperature; // read temerature data and store it to variable temp 210 lcd.setCursor (0, 0); // set the cursor to column 0, line 0 211 lcd.print ("Humidity(%):"); // print text 212 lcd.print (hum); // print humidity value 213 lcd.setCursor (0, 1); // set the cursor to column 0, line 1 214 lcd.print ("Temperature:"); // print text 215 lcd.print (temp); // print temperature value 216 delay (100); // delay to avoid LCD bouncing 217} 218
Comments
Only logged in users can leave comments