Commit 12e0f4de authored by João Lino's avatar João Lino

Initial commit

parents
#include <dht.h>
dht DHT;
//#define __debug
#define PROGRAM_VERSION "0.4.0"
#define DHT11_PIN 2
#define RelayPin 3
#define LEDCount 6
#define LED1Pin 4
#define LED2Pin 5
#define LED3Pin 6
#define LED4Pin 7
#define LED5Pin 8
#define LED6Pin 9
#define ButtonPin 13
int leds[LEDCount];
double Setpoint;
int SetpointMode;
boolean relayState;
double temperaturaQueAchava;
boolean fermentationMode;
void setup()
{
Serial.begin(115200);
Serial.println("Kegerator");
Serial.print("PROGRAM VERSION: ");
Serial.println(PROGRAM_VERSION);
Serial.print("\tDHT LIBRARY VERSION: ");
Serial.println(DHT_LIB_VERSION);
Serial.println();
Serial.println("Type, status, Humidity (%), Temperature (C), Relay State (ON\\OFF)");
leds[0] = LED1Pin;
leds[1] = LED2Pin;
leds[2] = LED3Pin;
leds[3] = LED4Pin;
leds[4] = LED5Pin;
leds[5] = LED6Pin;
Setpoint = 24.0;
SetpointMode = 0;
temperaturaQueAchava = 200.0;
fermentationMode = false;
// Relay
relayState = false;
pinMode(RelayPin, OUTPUT);
digitalWrite(RelayPin, HIGH);
// LEDs
pinMode(LED1Pin, OUTPUT);
pinMode(LED2Pin, OUTPUT);
pinMode(LED3Pin, OUTPUT);
pinMode(LED4Pin, OUTPUT);
pinMode(LED5Pin, OUTPUT);
pinMode(LED6Pin, OUTPUT);
digitalWrite(LED1Pin, LOW);
digitalWrite(LED2Pin, LOW);
digitalWrite(LED3Pin, LOW);
digitalWrite(LED4Pin, LOW);
digitalWrite(LED5Pin, LOW);
digitalWrite(LED6Pin, LOW);
// Button
pinMode(ButtonPin, INPUT);
}
void loop()
{
if ( hasBtnPressed() ) {
// increase mode
SetpointMode++;
// reset mode back to 0
if ( SetpointMode > 63 ) {
SetpointMode = 0;
}
// change leds
setLEDs(SetpointMode);
if ( SetpointMode > 31 ) {
// set fermentation mode
fermentationMode = true;
///TODO set automatic fermentation temperatures
Setpoint = SetpointMode - 32 + 0.5;
}
else {
// set fermentation mode
fermentationMode = false;
// set manual temperatures
Setpoint = SetpointMode;
}
}
// Read data off of the DHT11 sensor
int chk = DHT.read11(DHT11_PIN);
// debug received data
debugDHT(chk);
double temperaturaQueDiz = DHT.temperature;
double tempetaturaQueEuAcho;
if (temperaturaQueAchava == 200.0)
temperaturaQueAchava = temperaturaQueDiz;
if (temperaturaQueAchava > temperaturaQueDiz)
tempetaturaQueEuAcho = temperaturaQueAchava - 0.25;
else if (temperaturaQueAchava < temperaturaQueDiz)
tempetaturaQueEuAcho = temperaturaQueAchava + 0.25;
else
tempetaturaQueEuAcho = temperaturaQueAchava;
//tempetaturaQueEuAcho = temperaturaQueDiz;
#ifdef __debug
Serial.print("temperaturaAlvo = ");
Serial.print(Setpoint, 1);
Serial.print(", temperaturaQueDiz = ");
Serial.print(temperaturaQueDiz, 1);
Serial.print(", tempetaturaQueEuAcho = ");
Serial.print(tempetaturaQueEuAcho, 1);
Serial.print(", ");
#endif
if ( tempetaturaQueEuAcho > (Setpoint + 1.0) ) {
// turn on compressor to start cooling
relayState = true;
digitalWrite( RelayPin, LOW );
}
else {
if ( tempetaturaQueEuAcho <= (Setpoint - 1.0) ) {
// turn off compressor to stop cooling
relayState = false;
digitalWrite( RelayPin, HIGH );
}
}
#ifdef __debug
if (relayState) {
Serial.println( "ON" );
}
else {
Serial.println( "OFF" );
}
#endif
temperaturaQueAchava = tempetaturaQueEuAcho;
delay(50);
}
void setLEDs( int number ) {
int calcNumber = number;
for ( int i = (LEDCount - 1); i >= 0; i++ ) {
boolean zero = calcNumber % 2 == 0 ? true : false;
calcNumber /= 2;
if (zero) {
digitalWrite(leds[i], LOW);
}
else {
digitalWrite(leds[i], HIGH);
}
}
}
boolean hasBtnPressed() {
if ( digitalRead(ButtonPin) == HIGH ) {
while ( digitalRead(ButtonPin) ) {
delay(20);
}
// debounce
delay(20);
return true;
}
else {
return false;
}
}
void debugDHT( int chk ) {
#ifdef __debug
Serial.print("DHT11, ");
#endif
switch (chk)
{
case DHTLIB_OK:
#ifdef __debug
Serial.print("OK, ");
#endif
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error, ");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("Time out error, ");
break;
case DHTLIB_ERROR_CONNECT:
Serial.print("Connect error, ");
break;
case DHTLIB_ERROR_ACK_L:
Serial.print("Ack Low error, ");
break;
case DHTLIB_ERROR_ACK_H:
Serial.print("Ack High error, ");
break;
default:
Serial.print("Unknown error, ");
break;
}
#ifdef __debug
//Serial.print(DHT.humidity, 1);
//Serial.print(", ");
#endif
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment