Back to start
Arduino Thermometer

// Define the Functions
void up(int ledCount); //all leds up
void down(int ledCount); //all leds down
void decrease(int temperature); //leds blink from up to down if temperature decreases


// initalizes temp sensor
void OneWireReset(int Pin);
void OneWireOutByte(int Pin, byte d);
byte OneWireInByte(int Pin);

int timer = 10; // the higher the number the slower it is
int TempTemperatur; // defines variable for current temperature

int ledPins[] = {
42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22 }; // an array of pin numbers to which LEDs are attached

int pinCount = 21; // the number of pins (the length of the array)
int lasttemp; // saves last temperature
bool increase; // checks if temp has increased or not

#define TEMP_PIN 52 // temp sensor is on pin 52
float T1;
void setup() {
digitalWrite(TEMP_PIN, LOW);
pinMode(TEMP_PIN, INPUT); // sets the digital pin as input (logic 1)
Serial.begin(9600);
delay(1000);


int thisPin;
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
up(21); //leds blink from bottom to top
delay(300);
down(21); //leds blink from top to bottom

}

void loop() {
for (int i = 0; i < 1000; i++) {
//Begin Measuring Temperatur
int HighByte, LowByte, TReading, SignBit, Tc_100;
//Reads temp of sensor

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

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 & 0x8000; // test most sig bit
if (SignBit) // negative; Sensor gives float in 2 bytes with minus or plus. If negative it will eliminated
{
TReading = (TReading ^ 0xffff) + 1; //
}
//Scales temp
Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25
TempTemperatur = (TReading / 2) -10 ; //Reads current temp and refers to led pin number

digitalWrite(ledPins[TempTemperatur], HIGH); // set temp to number of led pin
delay (10); // 10 secondes reading of temp
digitalWrite(ledPins[TempTemperatur], LOW);// deletes last temp
}

//checks if temp has increased or not
if (lasttemp < TempTemperatur)
{
increase = true;
}
else if(lasttemp > TempTemperatur)
{
increase = false;
}

if (increase)
{
up (TempTemperatur);
}
else
{
decrease(TempTemperatur);
}

delay (1000);
digitalWrite(ledPins[TempTemperatur], LOW);

lasttemp = TempTemperatur; //sets last temp to current temp
}

//showtime for beginning
void up(int ledCount) { // loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < ledCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
}

void down( int ledCount){
// loop from the highest pin to the lowest:
for (int thisPin = ledCount - 1; thisPin >= 0; thisPin--) {
digitalWrite(ledPins[thisPin], HIGH); // turn the pin on:
delay(timer);
digitalWrite(ledPins[thisPin], LOW); // turn the pin off:
}
}
//showtime if temp decreases or increases

void decrease(int temperature){
// loop from the highest pin to the lowest:
for (int thisPin = pinCount; thisPin >= temperature; thisPin--) {
digitalWrite(ledPins[thisPin], HIGH); // turn the pin on:
delay(timer);
digitalWrite(ledPins[thisPin], LOW); // turn the pin off:
}
}

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 & 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);
}