Skip to content

Instantly share code, notes, and snippets.

@hletrd
Created March 31, 2016 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hletrd/6535b3f5aa555c5bd1c683117dd74c69 to your computer and use it in GitHub Desktop.
Save hletrd/6535b3f5aa555c5bd1c683117dd74c69 to your computer and use it in GitHub Desktop.
/************************************************************
* Arduino Controlled Refrigerator
* For Arduino Mega Only
*
* Code written on 2013-07-05, modified on 2016-03-31 by HLETRD
************************************************************/
#include <EEPROM.h>
#include <LiquidCrystal.h>
#define temp_default 2
////////////////////////////////////////////
#define Sensor1 8 //analog pin, for LM35DZ
#define Sensor2 9 //analog pin, for LM35DZ
#define Power1 20 //Power for sensor 1
#define Power2 21 //Power for sensor 2
#define GND1 18 //GND for sensor 1
#define GND2 19 //GND for sensor 2
#define Relay_pin 22 //pin for driving relays / FETs / etc..
//////////Modify here to configure//////////
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //for LCD & Keypad Shield
int Box_Count;
int tempset, tempreal = 0;
bool buttonnew = false;
byte arrowUD[8] = {
0x04,0x0E,0x1F,0x00,0x00,0x1F,0x0E,0x04};
byte degree[8] = {
0x06,0x09,0x09,0x06,0x00,0x00,0x00,0x00};
void setup(){
tempset = EEPROM.read(0);
if (tempset < 0 || tempset > 70) tempset = temp_default;
lcd.begin(16, 2);
lcd.createChar(0, arrowUD);
lcd.createChar(1, degree);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Simple Incubator");
lcd.setCursor(0, 1);
lcd.print("Preparing...");
pinMode(Relay_pin, OUTPUT);
digitalWrite(Relay_pin, LOW);
pinMode(Power1, OUTPUT);
digitalWrite(Power1, HIGH);
pinMode(Power2, OUTPUT);
digitalWrite(Power2, HIGH);
pinMode(GND1, OUTPUT);
digitalWrite(GND1, LOW);
pinMode(GND2, OUTPUT);
digitalWrite(GND2, LOW);
delay(1000);
}
void loop(){
analogReference(DEFAULT); //set analogRead's range to 1.1V
int ain;
for(int i = 0; i < 50; i++){
ain = analogRead(0); //few input values following analogReference() may not be accurate.
delay(1);
}
//LCD printing start
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("A refrigerator");
lcd.setCursor(0, 1);
lcd.print("SET");
lcd.setCursor(3, 1);
lcd.write((byte)0);
lcd.setCursor(4, 1);
if (tempset < 10) {
lcd.print("0");
lcd.setCursor(5, 1);
}
lcd.print(tempset);
lcd.setCursor(6, 1);
lcd.write((byte)1);
lcd.setCursor(7, 1);
lcd.print("C");
lcd.setCursor(9, 1);
lcd.print("NOW");
lcd.setCursor(12, 1);
if (tempreal < 10) {
lcd.print("0");
lcd.setCursor(13, 1);
}
lcd.print(tempreal);
lcd.setCursor(14, 1);
lcd.write((byte)1);
lcd.setCursor(15, 1);
lcd.print("C");
//LCD Printing End
//Feedback to button input
ain = analogRead(0);
if ((ain < 850) && (buttonnew == true)){
buttonnew = false;
if (ain < 650){
if (ain < 450){
if (ain < 250){
if (ain < 50){
//Right Key Pressed
if (tempset < 70) {
tempset++;
EEPROM.write(0, tempset);
}
}
else {
//Up Key Pressed
}
}
else {
//Down Key Pressed
}
}
else {
//Left Key Pressed
if (tempset > 0) {
tempset--;
EEPROM.write(0, tempset);
}
}
}
else {
//Select Key Pressed
}
}
else {
//No Valid Key Pressed
buttonnew = true;
}
//Feedback End
//temperature control
analogReference(INTERNAL1V1);
for(int i = 0; i < 50; i++){
ain = analogRead(Sensor1); //few input values following analogReference() may not be accurate.
ain = analogRead(Sensor2);
delay(1);
}
long int analoginput = 0;
for (int j = 0; j < 50; j++) {
analoginput += analogRead(Sensor1);
analoginput += analogRead(Sensor2);
delay(1);
}
float tempC;
tempC = (float)analoginput / 931;
tempreal = (int)tempC;
if (tempC < (float)tempset - 0.5 && tempset >= 2) {
digitalWrite(Relay_pin, LOW);
}
else {
//Cooling
digitalWrite(Relay_pin, HIGH);
}
//temparature control end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment