What I'm looking to do is have 4 digital inputs and an LCD counter. As you can see in the code there are two inputs to control the up and down of the counter and the other two inputs control the adjust, the logic is that the temperature set point can only be changed when the adjusted is pulled low. These are SPDT switches outer poles connected to a D/IO with the center grounded. I need a current value always being displayed of the "current" temperature", the adjust is used to change the set point. The set point adjust should modify the current temperature. Please assist me in this code. The temperature set point can only be changed when the adjust toggle is pulled LOW.
#include <LiquidCrystal.h>
byte switchPin = 50; // choose the input pin (for a pushbutton)
byte buttonPin = 51;
byte adjustOne = 52;
byte adjust = 53;
int val = 0; // variable for reading the pin status
int val1 = 0;
int val2 = 0;
int val3 = 0;
int currentTemp = 0;
int setPoint = 0;
int currentState = 0;
int currentState1 = 0;
int previousState = 0;
int previousState1 = 0;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
pinMode(switchPin, INPUT_PULLUP); // declare pushbutton as input
pinMode(buttonPin, INPUT_PULLUP);
pinMode(adjustOne, INPUT_PULLUP);
pinMode(adjust, INPUT_PULLUP);
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print(currentTemp);
}
void loop()
{
val = digitalRead(switchPin); // read input value
val1 = digitalRead(buttonPin); // read input value
val2 = digitalRead(adjust);
val3 = digitalRead(adjustOne);
lcd.setCursor(0, 0);
lcd.print(currentTemp);
if (val2 == LOW)
{
if (val == LOW)
{ // check if the input is HIGH (button released)
currentState = 1;
}
else
{
currentState = 0;
}
if (currentState != previousState)
{
if (currentState == 1)
{
setPoint++;
Serial.println(setPoint);
}
}
previousState = currentState;
setPoint = currentTemp;
delay(50);
if (val1 == LOW)
{ // check if the input is HIGH (button released)
currentState1 = 1;
}
else
{
currentState1 = 0;
}
if (currentState1 != previousState1)
{
if (currentState1 == 1)
{
setPoint--;
Serial.println(setPoint);
}
}
previousState1 = currentState1;
setPoint = currentTemp;
delay(50);
}
else if (val3 == LOW)
{
if (val == LOW)
{ // check if the input is HIGH (button released)
currentState = 1;
}
else
{
currentState = 0;
}
if (currentState != previousState)
{
if (currentState == 1)
{
setPoint++;
Serial.println(setPoint);
}
}
previousState = currentState;
setPoint = currentTemp;
delay(50);
if (val1 == LOW)
{ // check if the input is HIGH (button released)
currentState1 = 1;
}
else
{
currentState1 = 0;
}
if (currentState1 != previousState1)
{
if (currentState1 == 1)
{
setPoint--;
Serial.println(setPoint);
}
}
previousState1 = currentState1;
setPoint = currentTemp;
delay(50);
}
}
val = digitalRead(switchPin); // read input value
val1 = digitalRead(buttonPin); // read input value
val2 = digitalRead(adjust);
val3 = digitalRead(adjustOne);
Once this code scrolls off the screen, I can't remember what val2 means. Why not use names that make sense, with respect to the pins? currSwitchState, currButtonState, currAdjectState, and currAdjustOneState make more sense.
If course, how a switch differs from a button, in your world, is not clear. They are quite different in my world, where buttons hold shirts from gaping open, but are useless with an Arduino.
if (val == LOW)
{ // check if the input is HIGH (button released)
currentState = 1;
}
else
{
currentState = 0;
}
So, currentState = !val. Why not just say so? One line of code, instead of 8.
I wanted to understand if the logic I am implementing corresponds with the code. PaulS, I can work to fix these few coding bugs. But from an overall stand point does this code logic make sense? Unfortunately I have not been able to upload and test the code. Some of the variable names are just arbitrary.
I have done some modification to the code, changed variable names, simplified the logic. But does this accomplish what I want it to. Set Point is only overriding current state if they do not equal each other meaning the set point has changed.
#include <LiquidCrystal.h>
byte switchInc = 50; // choose the input pin (for a pushbutton)
byte switchDec = 51;
byte Adjust = 52;
byte AdjustOne = 53;
int valSwitchInc = 0; // variable for reading the pin status
int valSwitchDec = 0;
int valAdjust = 0;
int valAdjustOne = 0;
int currentTemp = 0;
int setPoint = 0;
int currentState = 0;
int currentState1 = 0;
int previousState = 0;
int previousState1 = 0;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
pinMode(switchInc, INPUT_PULLUP); // declare pushbutton as input
pinMode(switchDec, INPUT_PULLUP);
pinMode(Adjust, INPUT_PULLUP);
pinMode(AdjustOne, INPUT_PULLUP);
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print(currentTemp);
}
void loop()
{
valSwitchInc = digitalRead(switchInc); // read input value
valSwitchDec = digitalRead(switchDec);
valAdjust = digitalRead(Adjust);
valAdjustOne = digitalRead(AdjustOne);
lcd.setCursor(0, 0);
lcd.print(currentTemp);
if (Adjust == LOW || AdjustOne == LOW)
{
if (valSwitchInc == LOW)
{ // check if the input is HIGH (button released)
currentState = !valSwitchInc;
}
if (currentState != previousState)
{
if (currentState == 1)
{
setPoint++;
Serial.println(setPoint);
}
}
previousState = currentState;
delay(50);
if (valSwitchDec == LOW)
{ // check if the input is HIGH (button released)
currentState1 = !valSwitchDec;
}
if (currentState1 != previousState1)
{
if (currentState1 == 1)
{
setPoint--;
Serial.println(setPoint);
}
}
previousState1 = currentState1;
delay(50);
if (setPoint != currentTemp)
{
setPoint = currentTemp;
}
}
}
If you are not in adjust mode, does the state of the increment and/or decrement switch matter?
if (setPoint != currentTemp)
{
setPoint = currentTemp;
}
I don't understand the logic here. Suppose that hold one of the adjust switches and press the increment switch. setPoint will be increased. Supposedly, it will no longer match currentTemp. So, this sets setPoint back to currentTemp. Similarly, if I try to decrement setPoint, the same thing happens.
currentTemp never gets a new value, so at the end of loop(), any attempt to change setPoint will result in setPoint being set back to 0. Am I missing something?
This is a toggle switch connected to two IO's. The center pin is grounded and the outer leads are connected to. I/Os. So if the toggle switch is held down or up you can enter adjust mode. Adjust mode is for setting the new temperature. After I exit adjust mode, (toggle switch moves to the center off position for SPDT switch), the current temperature should equal the setpoint value from the adjust mode. The state of the counter does not matter when you are not in adjust mode. I should mention that I will be using a 16x2 LCD.
After I exit adjust mode, (toggle switch moves to the center off position for SPDT switch), the current temperature should equal the setpoint value from the adjust mode.
I'm sorry. That does not make sense, and that is not what the code is currently doing.
The current temperature should be measured. If currentTemperature means something else, then it needs to be renamed.
Regardless, it appears that you want to assign setPoint to currentTemperature, not assign currentTemperature to setPoint.
This is meant for a simulated temperature control. Current temp isn't being read from sensors I just need a program that sets the value from the adjust mode to the current temp.
This is an amazing community. Thank you Paul and everyone for your generous help. I will be deploying my code later today when I receive my electronic hardware. Will keep you updated.