Cet exemple illustre l’utilisation d’un capteur de température Dallas 1821, d’un écran LCD et du module Xbee.
Le code n’est pas factorisé, il est livré tel quel après quelques tests.
Code :
#include <SoftwareSerial.h>
#include <OneWire.h>
// DS1821 on digital pin 2 external
OneWire ds(2);
// DS1821 on digital pin 8 internal
OneWire ds1(8);
// LCD on digital pin 6 et 7
SoftwareSerial LCD(6, 7);
// LCD on digital pin 6 et 7
SoftwareSerial XBEE(3, 4);
int led = 13;
void setup(void)
{
LCD.begin(9600);
XBEE.begin(57600);
Serial.begin(9600);
//start 1821 conversion external
ds.reset();
ds.write(0xEE);
//start 1821 conversion internal
ds1.reset();
ds1.write(0xEE);
pinMode(led, OUTPUT);
digitalWrite(led,LOW);
}
void loop(void){
// external
byte temp_;
byte r;
byte t;
int i;
byte present;
// internal
byte temp1_;
byte r1;
byte t1;
int i1;
byte present1;
digitalWrite(led,LOW);
delay(1000);
// Read value external
present = ds.reset();
ds.reset();
ds.write(0xAA);
temp_ = ds.read();
// Read value internal
present1 = ds1.reset();
ds1.reset();
ds1.write(0xAA);
temp1_ = ds1.read();
//start 1821 conversion external
ds.reset();
ds.write(0xEE);
//start 1821 conversion internal
ds1.reset();
ds1.write(0xEE);
// negative temp external
if (temp_ >> 7 == 1){
r = temp_ ^ 0xFF; // bit inversion external
t = r + 1; // external
Serial.print("T");
Serial.print("-");
Serial.print(t);
Serial.print("C");
LCD.print("?f");
LCD.print("Text-");
LCD.print(t);
LCD.print("C");
XBEE.print("T");
XBEE.print("-");
XBEE.print(t);
XBEE.print("C");
}
// positive temp external
else
{
t = temp_; // external
Serial.print("T");
Serial.print("+");
Serial.print(t);
Serial.print("C");
LCD.print("?f");
LCD.print("Text+");
LCD.print(t);
LCD.print("C");
XBEE.print("T");
XBEE.print("+");
XBEE.print(t);
XBEE.print("C");
}
// negative temp internal
if (temp1_ >> 7 == 1){
r1 = temp1_ ^ 0xFF; // bit inversion external
t1 = r1 + 1; // external
Serial.print("-");
Serial.print(t1);
Serial.print("C");
LCD.print("?n");
LCD.print("Tint-");
LCD.print(t1);
LCD.print("C");
XBEE.print("-");
XBEE.print(t1);
XBEE.print("C");
}
// positive temp internal
else
{
t1 = temp1_; // internal
Serial.print("+");
Serial.print(t1);
Serial.print("C");
LCD.print("?n");
LCD.print("Tint+");
LCD.print(t1);
LCD.print("C");
XBEE.print("+");
XBEE.print(t1);
XBEE.print("C");
}
digitalWrite(led,HIGH);
delay(1000); // default 500
}