Pages Menu
TwitterRssFacebook

Posted by on Mar 15, 2011 in Development, Everything Else

Sackboy Thermometer

Sackboy Thermometer

This is a bit geeky, but I had a plastic Sackboy, an LCD display, a thermometer chip and a Arduino board lying around, so I thought I’d combine them to highlight my sad and stuffy office situation:

26.5 degrees! And it’s only March 🙁

If you’re interested, it’s really easy. Arduino make it simple to plug together components and then write C-style code against them. I used this thermometer and this LCD display, both from eBay. The blue casing was from Maplins and the sackboy was an accidental purcahse from Play!

Here’s the code that the Arduino is using (which has it’s roots in this Sheep Dog Guide, for which I am very thankful!)

#include #include

#define TEMP_PIN 2 //See Note 1, sheepdogguides..ar3ne1tt.htm

void OneWireReset(int Pin);//See Note 2
void OneWireOutByte(int Pin, byte d);
byte OneWireInByte(int Pin);
LiquidCrystal_I2C lcd(0x27,16,2);
double HighTemp;
double LowTemp;
int HighSign;
int LowSign;

void setup() {
digitalWrite(TEMP_PIN, LOW);
pinMode(TEMP_PIN, INPUT); // sets the digital pin as input (logic 1)
lcd.init(); // initialize the lcd
lcd.backlight();

HighTemp = 0;
LowTemp = 999999;

}

void loop(){
lcd.setCursor(0, 1);
int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;

OneWireReset(TEMP_PIN);
OneWireOutByte(TEMP_PIN, 0xcc);
OneWireOutByte(TEMP_PIN, 0x44); // perform temperature conversion, strong pullup for one sec

OneWireReset(TEMP_PIN);
OneWireOutByte(TEMP_PIN, 0xcc);
OneWireOutByte(TEMP_PIN, 0xbe);

LowByte = OneWireInByte(TEMP_PIN);
HighByte = OneWireInByte(TEMP_PIN);
TReading = (HighByte << 8 ) + LowByte; SignBit = TReading &amp; 0x8000; // test most sig bit if (SignBit) // negative { TReading = (TReading ^ 0xffff) + 1; // 2's comp } Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25 if (Tc_100 > HighTemp)
{
HighTemp=Tc_100;
HighSign = SignBit;
}

if (Tc_100 < LowTemp) { LowTemp = Tc_100; LowSign = SignBit; } lcd.setCursor(0, 0); lcd.print("L:"); PrintTemp(LowTemp,LowSign); lcd.print(" H:"); PrintTemp(HighTemp,HighSign); lcd.setCursor(0, 1); lcd.print("Now:"); PrintTemp(Tc_100,SignBit); if (HighTemp >= 3000)
{
lcd.print(” :-(“);
}

if ((HighTemp >= 2700) &amp;&amp; (HighTemp < 3000)) { lcd.print(" :-|"); } if (HighTemp < 2700) { lcd.print(" :-)"); } delay(5000); // 5 second delay. Adjust as necessary } void PrintTemp(int Temp, int SignBit) { int Whole, Fract; Whole = Temp / 100; // separate off the whole and fractional portions Fract = Temp % 100; if (SignBit) // If its negative { lcd.print("-"); } lcd.print(Whole); lcd.print("."); if (Fract < 10) { lcd.print("0"); } lcd.print(Fract); } void OneWireReset(int Pin) // reset. Should improve to act as a presence pulse { digitalWrite(Pin, LOW); pinMode(Pin, OUTPUT); // bring low for 500 us delayMicroseconds(500); pinMode(Pin, INPUT); delayMicroseconds(500); } void OneWireOutByte(int Pin, byte d) // output byte d (least sig bit first). { byte n; for(n=8; n!=0; n--) { if ((d &amp; 0x01) == 1) // test least sig bit { digitalWrite(Pin, LOW); pinMode(Pin, OUTPUT); delayMicroseconds(5); pinMode(Pin, INPUT); delayMicroseconds(60); } else { digitalWrite(Pin, LOW); pinMode(Pin, OUTPUT); delayMicroseconds(60); pinMode(Pin, INPUT); } d=d>>1; // now the next bit is in the least sig bit position.
}

}

byte OneWireInByte(int Pin) // read byte, least sig byte first
{
byte d, n, b;

for (n=0; n<8; n++) { digitalWrite(Pin, LOW); pinMode(Pin, OUTPUT); delayMicroseconds(5); pinMode(Pin, INPUT); delayMicroseconds(5); b = digitalRead(Pin); delayMicroseconds(50); d = (d >> 1) | (b<<7); // shift d to right and insert b in most sig bit position } return(d); } [/sourcecode]

Written by Tom Morgan

Tom is a Microsoft Teams Platform developer and Microsoft MVP who has been blogging for over a decade. Find out more.
Buy the book: Building and Developing Apps & Bots for Microsoft Teams. Now available to purchase online with free updates.

0 Comments

Trackbacks/Pingbacks

  1. Moving On… | Thought Stuff - [...] 27by TomToday was my last day at PineSolutions. I’ve had my exit interview, taken home Sackboy, and paid my…

Post a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.