Hi!
I have came up with one problem and I am not able to fix it or see my mistake in code. My project cosists of two arduinos connected by I2C. Master arduino counts time and registers temperature from two DS18B20 sensors, but for some reason I am not able to get correct temperature readings.
The reason of this THING is in code, because if I upload code with just few lines which registers temperature, everything works fine.
As soon as I upload my code it gives 85C or the last read value from previous code if i do not restart arduino.
I am using Arduino Micro
Code:
#include <LiquidCrystal.h>
#include <Wire.h>
#include <Time.h>
#include <OneWire.h>
#include <DallasTemperature.h>
struct SenBase{
String boxName;
DeviceAddress probe;
boolean online;
boolean state;
long time;
float temperature;
byte dimming;
boolean onStart;
};
SenBase sensor[2] = {
//{"1", {0x28, 0xBD, 0xAD, 0x15, 0x07, 0x00, 0x00, 0x4F}, true, false, 0, 0.00, 128, true},
//{"2", {0x28, 0x10, 0xF1, 0xF2, 0x06, 0x00, 0x00, 0x89}, true, false, 0, 0.00, 128, true}
{"1", {0x28, 0xA7, 0x2C, 0x17, 0x07, 0x00, 0x00, 0xDA}, true, false, 0, 0.00, 128, true},
{"2", {0x28, 0x77, 0x63, 0xF3, 0x06, 0x00, 0x00, 0x74}, true, false, 0, 0.00, 128, true}
};
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
byte selected = 0;
time_t time;
boolean menu = false;
#define ONE_WIRE_BUS_PIN A1
OneWire oneWire(ONE_WIRE_BUS_PIN);
DallasTemperature sensors(&oneWire);
float temperature = 0;
char key = 'n';
boolean printLCD = true;
void setup(){
selected = 0;
menu = false;
//Serial
Serial.begin(9600);
//I2C
Wire.begin();
//DS18B20
sensors.begin();
for (byte i = 0; i<=1; i++) {
sensors.setResolution(sensor[i].probe, 12);
}
}
void loop()
{
key = read_LCD_buttons();
switch (key)
{
case 'a':
{
Serial.print("RIGHT --- ");
Serial.println(menu);
//RIGHT
if (menu == true) {
sensor[selected].state = !sensor[selected].state;
Start(selected, sensor[selected].state);
}
else {
selected--;
}
printLCD = true;
menu = false;
key = 'n';
break;
}
case 'c':
{
Serial.print("LEFT --- ");
Serial.println(menu);
if (menu == true) {
}
else {
selected++;
}
printLCD = true;
menu = false;
key= 'n';
break;
}
case 'b':
{
Serial.println("SELECT");
printLCD = true;
menu = true;
break;
}
}
if (selected < 0)
selected = 1;
if (selected > 1)
selected = 0;
Serial.print("SELECTED --- ");
Serial.println(selected);
if (now() - time >= 10) {
for (byte i = 0; i<=1; i++) {
if ((sensor[i].online) && (sensor[i].state)) {
sensor[i].time += (now() - time);
temperature = sensors.getTempC(sensor[i].probe);
float diff = temperature - sensor[i].temperature;
boolean low, high = false;
float lowT = 0.0;
float highT = 0.0;
if (sensor[i].time <86400){
highT = 38.2;
lowT = 37.2;
}
else if (sensor[i].time < 172800) {
highT = 37.2;
lowT = 36.2;
}
else if (sensor[i].time < 259200) {
highT = 36.2;
lowT = 35.2;
}
else if (sensor[i].time < 345600) {
highT = 35.2;
lowT = 34.2;
}
else if (sensor[i].time < 432000) {
highT = 34.2;
lowT = 33.2;
}
else if (sensor[i].time < 518400) {
highT = 33.2;
lowT = 32.2;
}
else if (sensor[i].time < 604800) {
highT = 32.2;
lowT = 31.2;
}
else if (sensor[i].time < 777600) {
highT = 31.2;
lowT = 30.2;
}
else if (sensor[i].time < 864000) {
highT = 30.2;
lowT = 29.2;
}
else if (sensor[i].time < 950400) {
highT = 29.2;
lowT = 28.2;
}
else if (sensor[i].time < 1036800) {
highT = 28.2;
lowT = 27.2;
}
else {
highT = 27.2;
lowT = 26.2;
}
low = false;
high= false;
if ((temperature >= highT) && ((diff >= -0.1) || (sensor[i].onStart == true))) {
low = true;
sensor[i].onStart = false;
}
else if ((temperature < lowT) && ((diff <= 0.1) || (sensor[i].onStart == true))) {
//{
high = true;
sensor[i].onStart = false;
}
Serial.println();
Serial.println("DATA:");
Serial.print("Time: ");
Serial.println(sensor[i].time);
Serial.print("High: ");
Serial.println(lowT);
Serial.print("Low: ");
Serial.println(highT);
Serial.print("Temperature: ");
Serial.println(temperature);
Serial.print("Diff: ");
Serial.println(diff);
if (low == true) {
sensor[i].dimming += 5;
if (sensor[i].dimming > 128)
sensor[i].dimming = 128;
sendData(i, sensor[i].dimming);
sensor[i].temperature = temperature;
Serial.print("Go Low: ");
Serial.println(sensor[i].dimming);
} else
if (high == true) {
sensor[i].dimming -= 5;
if (sensor[i].dimming < 0)
sensor[i].dimming = 0;
sendData(i, sensor[i].dimming);
sensor[i].temperature = temperature;
Serial.print("Go High: ");
Serial.println(sensor[i].dimming);
}
delay(5000);
}
}
time = now();
printLCD = true;
}
if (printLCD == true) {
if (menu == false) {
DisplayInfo(selected);
}
else {
if (sensor[selected].state) {
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("STOP? ");
lcd.setCursor(0, 1);
lcd.print(" YES NO ");
}
else {
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("START? ");
lcd.setCursor(0, 1);
lcd.print(" YES NO ");
}
}
printLCD = false;
}
}
char read_LCD_buttons()
{
int adc_key_in = analogRead(0); // read the value from the sensor
delay(50); //switch debounce delay. Increase this delay if incorrect switch selections are returned.
int k = (analogRead(0) - adc_key_in); //gives the button a slight range to allow for a little contact resistance noise
if (5 < abs(k)) return 'n'; // double checks the keypress. If the two readings are not equal +/-k value after debounce delay, it tries again.
if (adc_key_in > 150) return 'a';//btnRIGHT;
if (adc_key_in > 85) return 'b';//btnUP;
if (adc_key_in > 30) return 'c';//btnDOWN;
return 'n';//btnNONE; // when all others fail, return this...
}
void sendData(byte i, byte dimming)
{
Wire.beginTransmission(4); // transmit to device #4
String boxName = "Box_" + sensor[i].boxName;
Wire.write(boxName.c_str()); // sends five bytes
Wire.write(dimming); // sends one byte
Wire.endTransmission(); // stop transmitting
}
void Start(byte i, boolean on){
if (on) {
sensor[i].state = true;
sensor[i].dimming = 0;
sendData(i, 0); //ON
}
else {
sensor[i].state = false;
sendData(i, 128); //OFF
sensor[i].dimming = 128;
sensor[i].time = 0;
sensor[i].onStart = true;
}
}
bool to_bool(String str) {
if (str == "1")
return true;
else
return false;
}
void DisplayInfo(byte i)
{
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Box_" + sensor[i].boxName);
lcd.setCursor(8, 0);
lcd.print(sensor[i].temperature);
lcd.setCursor(0, 1);
if (sensor[i].state) {
lcd.print("ON");
lcd.setCursor(8, 1);
byte d = sensor[i].time / 86400;
byte h = (sensor[i].time - (86400 * d)) / 3600;
byte m = (sensor[i].time - (86400 * d) - (3600 * h)) / 60;
lcd.print(d);
lcd.print(" ");
if (h<10)
lcd.print("0");
lcd.print(h);
lcd.print(":");
if (m<10)
lcd.print("0");
lcd.print(m);
}
else {
lcd.print("OFF ");
}
}
I am hoping for some ideas, i am fighting with it for a while and i am out of ideas