Cac/arduino/termostato/termostato.ino

146 lines
3.8 KiB
Arduino
Raw Normal View History

2015-11-13 12:39:20 +01:00
//board atmega1280
#include <glcd.h>
#include <fonts/allFonts.h>
#include <IRremote.h>
#include <SoftwareSerial.h>// per bluetooth
int RECV_PIN = 11; //define input pin on Arduino
IRrecv irrecv(RECV_PIN);
decode_results results;
String val = "";
double Temperature;
//parte bluetooth
SoftwareSerial mySerial(52, 53); // RX, TX
String command = ""; // Stores response of the HC-06 Bluetooth device
char blueToothVal; //value sent over via bluetooth
char lastValue; //stores last state of device (on/off)
int countTemp = 0;
double tempTemp = 0;
double tempTempTot = 0;
double Thermister(int RawADC) { //Function to perform the fancy math of the Steinhart-Hart equation
double Temp;
//Temp = log(10000.0*((1024.0/RawADC-1)));
//Serial.println(RawADC);
float R = 9870.0; // Fixed resistance in the voltage divider
float Rt,T,logRt; // Computed resistance of the thermistor
Rt = R*( 1023.0 / float(RawADC) - 1.0 );
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
logRt = log(Rt);
T = ( 1.0 / (c1 + c2*logRt + c3*logRt*logRt*logRt ) ) - 273.15;
//Serial.println(T);
// See http://en.wikipedia.org/wiki/Thermistor for explanation of formula
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
Temp = Temp - 273.15; // Convert Kelvin to Celcius
return Temp;
}
char* NormalizedTemp(double Temperature) {
char charBuf[50];
//ciaoa.toCharArray(charBuf, 50);
int intpart = (int)Temperature;
double decpart = (Temperature - intpart)*100;
int intdecpart = (int)decpart;
sprintf(charBuf, "t%d%d", intpart,intdecpart);
return charBuf;
}
void setup() {
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
// Initialize the GLCD
GLCD.Init();
// Select the font for the default text area
GLCD.SelectFont(System5x7);
GLCD.print("Formaggino dice:");
//parte bluetooth
Serial.println("Type AT commands!");
// The HC-06 defaults to 9600 according to the datasheet.
mySerial.begin(9600);
pinMode(13,OUTPUT);
Temperature = Thermister(analogRead(0));
Serial.println(Temperature);
}
void loop() {
countTemp += 1;
Serial.println(countTemp);
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
GLCD.CursorTo(0, 1);
GLCD.print("Attivo da: ");
// print the number of seconds since reset:
GLCD.print(millis()/1000);
GLCD.print(" sec");
GLCD.print("\nTemperatura: ");
tempTemp = Thermister(analogRead(0));
tempTempTot += tempTemp;
Serial.println(tempTemp);
if ((countTemp % 10) == false){
Serial.println("Dentro");
Serial.println(tempTempTot);
Temperature = tempTempTot / 10;
tempTempTot = 0;
}
GLCD.print(Temperature);
//Delay 50ms before next reading.
delay(50);
if (val == "ff30cf")
{
GLCD.println("valore preso");
}
else{
GLCD.println("no");
}
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
if (String(results.value, HEX) != "ffffffff")
val = String(results.value, HEX);
Serial.println(val);
irrecv.resume(); // Receive the next value
}
// Read user input if available.
if (mySerial.available()){
//delay(10); // The delay is necessary to get this working!
//mySerial.write(Serial.read());
blueToothVal=(char)mySerial.read(); //read it
Serial.println("sono dentro");
}
if (blueToothVal=='n')
{//if value from bluetooth serial is n
digitalWrite(13,HIGH); //switch on LED
mySerial.write(NormalizedTemp(Temperature));
blueToothVal=' ';
}
else if (blueToothVal=='f')
{//if value from bluetooth serial is n
digitalWrite(13,LOW); //turn off LED
mySerial.write(NormalizedTemp(Temperature));
blueToothVal=' ';
}
delay(1000);
}