Tri-Temp Standard Code v2.0

After a long wait here’s version 2.0 of the Tri-Temp Standard code. What changed? Lots.

  • Button response is now very very fast.
  • Button to toggle C/F temp measurements
  • Temp reading is updated by interrupt timer. Approx every 5 sec.
  • Code is documented
  • Two new libraries needed –  avr/interrupt.h and TimerOne.h

Here’s the code:

// v2.0 Tri-Temp Standard w/ Interrupt Timer
// Copyright 2012 Jeff Williams @ www.arduinoeverything.com
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see.

#include "SoftwareSerial.h"
#include "DHT.h"
#include "avr/interrupt.h"
#include "TimerOne.h"
#define rxPin 0 // rxPin is immaterial - not used - just make this an unused Arduino pin number
#define txPin 8 // set display to digital pin 8
#define DHTPIN1 7 //sensor one digital pin
#define DHTPIN2 3 //sensor two digital pin
#define DHTPIN3 5 //sensor three digital pin
#define DHTTYPE DHT22
int intTempPin = 0; //set internal temp analog pin
int pinButton1 = 1; //button pin assignments
int pinButton2 = 6;
int pinButton3 = 4;
int pinButton4 = 2;
int valButton1 = 0; //setup default values
int valButton2 = 0;
int valButton3 = 0;
int valButton4 = 0;
int old_valButton3 = 0; //setup default old values
int old_valButton4 = 0;
int stateButton3 = 1; //setup inital states
int stateButton4 = 0;
int displayInfo = 1; //set default sensor to display on startup (1-4)
int TempScale = 1; //sets default temp scale to F
DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);
DHT dht3(DHTPIN3, DHTTYPE);
SoftwareSerial myDisplay = SoftwareSerial(rxPin, txPin); //setup myDisplay as object for working with LCD

void setup(){

pinMode(txPin, OUTPUT); //set LCD pin to output

myDisplay.begin(9600); // 9600 baud is chip comm speed
myDisplay.print("?f"); // clear the LCD

myDisplay.print("?G216"); // set display geometry, 2 x 16 characters in this case
delay(50); // pause to allow LCD EEPROM to program

myDisplay.print("?Bff"); // set backlight to ff hex, maximum brightness
delay(50); // pause to allow LCD EEPROM to program

myDisplay.print("?f"); // clear the LCD
delay(10);

myDisplay.print("?c0"); // turn cursor off

myDisplay.print("..Starting Up..");

dht1.begin(); //initialize the dht sensors
dht2.begin();
dht3.begin();
pinMode(pinButton1, INPUT); //setup the button pins
pinMode(pinButton2, INPUT);
pinMode(pinButton3, INPUT);
pinMode(pinButton4, INPUT);

Timer1.initialize(50000000); // initialize timer1, and set a 1/2 second period
Timer1.attachInterrupt(UpdateReadings); // attaches callback() as a timer overflow interrupt
UpdateReadings();
}

void loop(){
//check the buttons as fast as possible. Only interrupted by timer.
checkButton1();
checkButton2();
checkButton3();
checkButton4();
}

void displayDHT1(){ //get and display data from sensor one
myDisplay.print("?x00?y0"); // cursor to first character of line 0
float h1 = dht1.readHumidity();
float t1 = dht1.readTemperature();
myDisplay.print("?f"); // clear the LCD
delay(10);
// check if returns are valid, if they are NaN (not a number) then something went wrong and display an error message!
if (isnan(t1) || isnan(h1)) {
myDisplay.print("No read on DHT1"); //error getting data from sensor
} else {
myDisplay.print("#1 H:"); //start displaying sensor data
myDisplay.print(h1); //print the humidity percentage
if (TempScale == 1) { //check to see what temp scale to print. 1 for F and 0 for C
myDisplay.print("%"); //print F scale
myDisplay.print("?x00?y1");
myDisplay.print("Temp:");
float tF = (t1 * 9.0 / 5.0) + 32.0;
myDisplay.print(tF);
myDisplay.print("F");
} else {
myDisplay.print("%"); //print C scale
myDisplay.print("?x00?y1");
myDisplay.print("Temp:");
myDisplay.print(t1);
myDisplay.print("C");
}
}
delay(50);
}

void displayDHT2(){ //see displayDHT1 for comments. The code is the same

myDisplay.print("?x00?y0");
float h2 = dht2.readHumidity();
float t2 = dht2.readTemperature();
myDisplay.print("?f"); /
delay(10);
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t2) || isnan(h2)) {
myDisplay.print("No read on DHT2");
} else {
myDisplay.print("#2 H:");
myDisplay.print(h2);
if (TempScale == 1) {
myDisplay.print("%");
myDisplay.print("?x00?y1");
myDisplay.print("Temp:");
float tF = (t2 * 9.0 / 5.0) + 32.0;
myDisplay.print(tF);
myDisplay.print("F");
} else {
myDisplay.print("%");
myDisplay.print("?x00?y1");
myDisplay.print("Temp:");
myDisplay.print(t2);
myDisplay.print("C");
}
}
delay(50);
}

void displayDHT3(){ //see displayDHT1 for comments. The code is the same

myDisplay.print("?x00?y0");
float h3 = dht3.readHumidity();
float t3 = dht3.readTemperature();
myDisplay.print("?f");
delay(10);
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t3) || isnan(h3)) {
myDisplay.print("No read on DHT3");
} else {
myDisplay.print("#3 H:");
myDisplay.print(h3);
if (TempScale == 1) {
myDisplay.print("%");
myDisplay.print("?x00?y1");
myDisplay.print("Temp:");
float tF = (t3 * 9.0 / 5.0) + 32.0;
myDisplay.print(tF);
myDisplay.print("F");
} else {
myDisplay.print("%");
myDisplay.print("?x00?y1");
myDisplay.print("Temp:");
myDisplay.print(t3);
myDisplay.print("C");
}
}
delay(50);
}

void displayInternalTemp(){ //this displays the temp from the internal thermister sensor TMP36
myDisplay.print("?f"); // clear the LCD
delay(10);
int intTempReading = analogRead(intTempPin); //get temp from the internal sensor
// converting that reading to voltage, for 3.3v arduino use 3.3
float voltage = intTempReading * 5.0;
voltage /= 1024.0;
float intTemperatureC = (voltage - 0.5) * 100 ; //convert the voltage to the temp in C
float intTemperatureF = (intTemperatureC * 9.0 / 5.0) + 32.0; //convert the temp in C to F
myDisplay.print("?x00?y0");
myDisplay.print("#4 Int Temp:");
myDisplay.print("?x00?y1");
if (TempScale == 1) {
myDisplay.print(intTemperatureF);
myDisplay.print("F");
} else {
myDisplay.print(intTemperatureC);
myDisplay.print("C");
}
delay(50);
}

void checkButton1(){ //check button one to see if it was pressed
valButton2 = digitalRead(pinButton2); //read the pin state
if (valButton2 == HIGH ) { //if it's high de-increment the displayInfo var by 1
myDisplay.print("?f");
displayInfo -= 1;
if (displayInfo < 1) //if the new value is lower than 1 make it 1 { displayInfo = 1; UpdateReadings(); //update the display } else { UpdateReadings(); //update the display } } } void checkButton2(){ valButton1 = digitalRead(pinButton1); if (valButton1 == HIGH ) { //do something here displayInfo += 1; //if it's high increment the displayInfo var by 1 myDisplay.print("?f"); if (displayInfo > 4)
{
displayInfo = 4; //if the new value is higher than 4 make it 4
UpdateReadings();
} else {
UpdateReadings();
}
}
}

void checkButton3(){ //check to see if button 3 is high
valButton3 = digitalRead(pinButton3);

if ((valButton3 == HIGH ) ){ //if it's high subtract one from the button's state var
stateButton3 = 1 - stateButton3;

old_valButton3 = valButton3; //set the old val to the new one

if (stateButton3 == 1) { //if the state is 1 then the tempscale is F, otherwise it's C
TempScale = 1;
UpdateReadings();
} else {
TempScale = 0;

UpdateReadings();
}
}
}

void checkButton4(){ //turn the back light on/off same principal as for button3

valButton4 = digitalRead(pinButton4);

if ((valButton4 == HIGH ) && (old_valButton4 == LOW)){
stateButton4 = 1 - stateButton4;

}
old_valButton4 = valButton4;

if (stateButton4 == 1) {
//do something here
myDisplay.print("?Bff"); // set backlight to ff hex, maximum brightness
delay(50); // pause to allow LCD EEPROM to program

} else {
//do something else
myDisplay.print("?B00"); // set backlight to ff hex, maximum brightness
delay(50); // pause to allow LCD EEPROM to program
}
}

void UpdateReadings(){ //this function is called by the timer interrupt. It refreshes the display based on which sensor we're looking at.
switch (displayInfo){
case 1:
//display DHT1
displayDHT1();
break;

case 2:
//display DHT2
displayDHT2();
break;

case 3:
//display DHT3
displayDHT3();
break;

case 4:
//display internal temp
displayInternalTemp();
break;

default:
myDisplay.print("?f");
break;
}
}

Advertisements

Author: Helicopter Jeff

I'm a vinyl decal cutting and designing, Astrophotography and general photography capturing, RC helicopter/quad copter pilot, Arduino and Raspberry Pi hardware/software developer, network, scripting and troubleshooting ninja living in the metro NY area. I am passionate about my hobbies so much I do them for a living. There's nothing better than getting paid to do what you love.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s