Hi,
New to arduino and this forum. I'm hoping someone can help me out with something i'm trying to achieve.
I wrote a piece of code that takes a measurement with a temperature sensor and outputs this to a lcd screen. It also changes a LED from red to green if the temperature gets to high.
The temperature value where the LED changes color is displayed on the second line of the LCD.
Took me a few hours but this worked. The problem is the following part.
Is it possible to change the value of the set temperature with a push button to another temperature?
This is my current code.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_TMP006.h"
Adafruit_TMP006 tmp006(0x40);
LiquidCrystal_I2C lcd(0x27, 16, 2);
int B_LED = 10;
int R_LED = 11;
int set_temp = 32; //temperature 1
int set_temp2 = 40; // temperature 2, not in use yet
void setup()
{
lcd.begin();
lcd.backlight();
pinMode (R_LED, OUTPUT); // declaring the LED as an output
pinMode (B_LED, OUTPUT); // declaring the LED as an output
}
void loop() {
// part that controls the LED
float objt = tmp006.readObjTempC(); // take measurement of tmp006 sensor
if (objt < set_temp) // if measurement is lower than set temperature 1
{
analogWrite(10, 50); // led green
analogWrite(11, 0); } // led red
else if (objt > set_temp)
{
analogWrite(10, 0); // led green
analogWrite(11, 50); } // led red
// part that controlls the LCD output
lcd.print("Temp: "); lcd.print(objt); lcd.print(" C "); // first line
lcd.setCursor(0,1) ; //sets cursor to second line first row
lcd.print("Max 1: "); lcd.print(set_temp); lcd.print(" C "); // second line
delay(2000);
// Clear the screen
lcd.clear();
}
And this is one of the versions that failed, also tried one with if and else.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_TMP006.h"
Adafruit_TMP006 tmp006(0x40);
LiquidCrystal_I2C lcd(0x27, 16, 2);
// constants
const int B_LED = 10; //groene led, temp low
const int R_LED = 11; //rode led, temp to high
const int buttonPin = 8; //switch for temp setting
int val = 0; // variable for reading the pin status
int Set_temp_OL = 30; //temperatuur parameter low
int Set_temp_DL = 40; //temperatuur parameter high
void setup()
{
lcd.begin();
lcd.backlight();
pinMode (R_LED, OUTPUT); // declaring the LED as an output
pinMode (B_LED, OUTPUT); // declaring the LED as an output
pinMode (buttonPin, INPUT_PULLUP); // decraring as set_temp toggle switch
Serial.begin(9600);
}
void loop() {
switch (buttonPin)
case (HIGH):
float objt = tmp006.readObjTempC(); // take measurement
if (objt < Set_temp_OL)
{ analogWrite(10, 50); // led green
analogWrite(11, 0); } // led red
else if (objt > Set_temp_OL)
{ analogWrite(10, 0); // led green
analogWrite(11, 50); } // led red
lcd.print("Asfalt: "); lcd.print(objt); lcd.print(" C "); // eerste regel
lcd.setCursor(0,1) ; //sets cursor to second line first row
lcd.print("MAX: "); lcd.print(Set_temp_OL); lcd.print(" C "); // tweede regel
break;
case (LOW):
float objt = tmp006.readObjTempC(); // take measurement
if (objt < Set_temp_DL)
{ analogWrite(10, 50); // led green
analogWrite(11, 0); } // led red
else if (objt > Set_temp_DL)
{ analogWrite(10, 0); // led green
analogWrite(11, 50); } // led red
lcd.print("Asfalt: "); lcd.print(objt); lcd.print(" C "); // eerste regel
lcd.setCursor(0,1) ; //sets cursor to second line first row
lcd.print("MAX: "); lcd.print(Set_temp_DL); lcd.print(" C "); // tweede regel
delay(2000);
// Clear the screen
lcd.clear();
}
Hope some one can put me on the right track!