Hello,
I'm new to the forum and Arduino in general, I wrote my very first code a few months ago now I'm already on my third Arduino project, it's been a bit addicting.
My current project involves reading a temperature from a Dallas thermometer to regulate a heating element via a relay. I have an LCD that displays the current temperature, and two buttons to allow input of what temperature you want.
I wired everything up and coded it, and it works perfectly, as long as it is powered through the USB port. If I unplug the USB with battery for backup, it still works fine. After resetting it and running on only 9V battery, the buttons don't work properly anymore, they don't register most hits, and there is a second delay between each time it registers. When it works right I can hold the button and it increases the variable quickly. Switching back to USB power it still has the same problem. Only when reuploading the code does it start working properly again.
I couldn't narrow it down between a hardware or program issue so I posted it here. I suspected the battery and tried powering with a 12V computer PSU, same issue. I ran all of the ground connections directly to the DC ground since I plan to run too big of a current for the ground pin, not sure if that could hurt.
Here's the code I'm using.
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
float temp = 0;
int tempu = 20;
int up = 6;
int down = 7;
int buttonState = 0;
int buttonState2 = 0;
// Data wire is plugged into pin 9 on the Arduino
#define ONE_WIRE_BUS 9
// 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);
DeviceAddress insideThermometer = { 0x28, 0x4F, 0x54, 0xBF, 0x03, 0x00, 0x00, 0x2A };
void setup(void)
{
pinMode(led, OUTPUT);
pinMode(relay, OUTPUT);
pinMode(up, INPUT);
Serial.begin(9600);
sensors.begin();
sensors.setResolution(insideThermometer, 10);
lcd.begin(16, 2);
}
void printTemperature(DeviceAddress deviceAddress)
{ float tempC = sensors.getTempC(deviceAddress);
temp = tempC;
if (tempC == -127.00) {
Serial.print("Error getting temperature");
}
else {
Serial.print(temp);
}
}
void loop(void){
buttonState = digitalRead(up);
if (buttonState == HIGH) {
tempu ++;
}
if (buttonState2 == HIGH) {
tempu --;
}
sensors.requestTemperatures();
printTemperature(insideThermometer);
Serial.print("\n\r");
lcd.setCursor(0, 0);
lcd.print("Temp ");
lcd.print((char)223);
lcd.print("C:");
lcd.setCursor(10, 0);
lcd.print(temp);
lcd.setCursor(0, 1);
lcd.print("Target:");
lcd.setCursor(10, 1);
lcd.print(tempu);
}