Flowchart van smart bierglas met hierin de 3 mogelijke ouputs op het lcd scherm.
De uiteindelijke code:
#include <OneWire.h>
#include <Wire.h>
#include "rgb_lcd.h"
int led = 4;
OneWire ds(2);
float temperature;
int B=3975;
float resistance;
rgb_lcd lcd;
void setup(void) {
lcd.begin(16, 2);
Serial.begin(9600);
}
void loop(void) {
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
if ( !ds.search(addr)) {
Serial.println("No more addresses.");
Serial.println();
ds.reset_search();
delay(250);
return;
}
Serial.print("ROM =");
for( i = 0; i < 8; i++) {
Serial.write(' ');
Serial.print(addr[i], HEX);
}
if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return;
}
Serial.println();
switch (addr[0]) {
case 0x10:
Serial.println(" Chip = DS18S20");
type_s = 1;
break;
case 0x28:
Serial.println(" Chip = DS18B20");
type_s = 0;
break;
case 0x22:
Serial.println(" Chip = DS1822");
type_s = 0;
break;
default:
Serial.println("Device is not a DS18x20 family device.");
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44);
delay(1000);
present = ds.reset();
ds.select(addr);
ds.write(0xBE);
Serial.print(" Data = ");
Serial.print(present, HEX);
Serial.print(" ");
for ( i = 0; i < 9; i++) {
data[i] = ds.read();
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.print(" CRC=");
Serial.print(OneWire::crc8(data, 8), HEX);
Serial.println();
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3;
if (data[7] == 0x10) {
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
if (cfg == 0x00) raw = raw & ~7;
else if (cfg == 0x20) raw = raw & ~3;
else if (cfg == 0x40) raw = raw & ~1;
}
celsius = (float)raw / 16.0;
lcd.setCursor(0,0);
lcd.print(celsius);
lcd.print((char)223);
lcd.print("C");
if (celsius > 10)
{
lcd.setRGB(255,0,0);
lcd.setCursor(0,1);
lcd.print("Roep ober! ");
digitalWrite(led, HIGH);
delay(100);
}
else if (celsius <= 8)
{
lcd.setRGB(0,0,225);
lcd.setCursor(0,1);
lcd.print("Proost! ");
digitalWrite(led, LOW);
}
else if (celsius > 8)
{
lcd.setRGB(255,170,0);
lcd.setCursor(0,1);
lcd.print("Doordrinken!");
digitalWrite(led, HIGH);
delay(100);
digitalWrite(led, LOW);
delay(100);
}
}
Geen opmerkingen:
Een reactie posten