Hello,
I am working on my first Arduino project and it's going relatively well, just need a bit of help with the programming end. I am stoked to be a part of the community and can't wait to get involved more.
I am monitoring automotive fuel temperatures at four different locations using daisy chained DS18B20's and custom made thermowells. I am using an Arduino UNO with a basic 16x2 LCD key pad shield to output the four temps. in the cab of my truck.
I have successfully been able to, thanks to some code online, show two temperatures and then after a delay show the last two. This was a feat for me but ideally I would like to use the buttons on the key pad shield to switch between the two sets of temps.
This is what I have so far:
// This Arduino sketch reads DS18B20 "1-Wire" digital
// temperature sensors.
// Tutorial:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
// Connections:
// rs (LCD pin 4) to Arduino pin 8
// rw (LCD pin 5) to Arduino pin 11
// enable (LCD pin 6) to Arduino pin 9
// LCD pin 15 to Arduino pin 13
// LCD pins d4, d5, d6, d7 to Arduino pins 4, 5, 6, 7
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int d=1000;
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
DeviceAddress Therm1 = { 0x28, 0x00, 0xFA, 0xAA, 0x03, 0x00, 0x00, 0x53 };//Pin 1-Black, 2-Red,3-White
DeviceAddress Therm2 = { 0x28, 0x78, 0x5A, 0x4B, 0x04, 0x00, 0x00, 0xE5 };//1-Green, 2-Black, 3-White
DeviceAddress Therm3 = { 0x28, 0x90, 0x51, 0x4B, 0x04, 0x00, 0x00, 0x1D };//1-Green, 2-Black, 3-White
DeviceAddress Therm4 = { 0x28, 0x17, 0x2D, 0x4B, 0x04, 0x00, 0x00, 0x3E };//1-Green, 2-Black, 3-White
void setup(void)
{
// Start up the library
sensors.begin();
// set the resolution to 10 bit (good enough?)
sensors.setResolution(Therm1, 10);
sensors.setResolution(Therm2, 10);
sensors.setResolution(Therm3, 10);
sensors.setResolution(Therm4, 10);
//analogWrite(10, 255); // set brightness on pin 10 to 0-255
lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.
lcd.print("Push one fool"); // start with a blank screen
}
void printTemperature(DeviceAddress deviceAddress)
{
double tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
lcd.print("Error");
} else {
lcd.print(tempC);
}
}
void loop(void)
{
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) // depending on which button was pushed, we perform an action
{
case btnUP:
{
delay(500);
sensors.requestTemperatures();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp1: ");
printTemperature(Therm1);
lcd.setCursor(0,1);
lcd.print(" Temp2 : ");
printTemperature(Therm2);
break;
}
case btnDOWN:
{
delay(500);
lcd.clear();
sensors.requestTemperatures();
lcd.setCursor(0,0);
lcd.print("Temp3:");
printTemperature(Therm3);
lcd.setCursor(0,1);
lcd.print("Temp4:");
printTemperature(Therm4);
break;
}
}
}
This actually does what I want it to do except that I am worried it's not going to keep updating my temperatures (all 4). Will it become stuck on getting the temperatures only once? Do I need to change up the code completely? Not sure if this is even the best way to go about what I want to do.
Any help would be greatly appreciated.
Thanks