Pushbutton issue within code

Hello to all.

Im currently creating and writing code for my spa controller. At this stage most of the code is working fine however under one sub routine my push button fails to work when its pressed again.

Currently the code acts as follows when testing:

  1. if (potVal > temp) - both button controls work ie Can turn on and off when ever.
    2. if (potVal < temp) - this is the issue once temperature set is below the current temperature the Heat pushbutton code will not switch to off when pushed, however the pump pushbutton will switch the mode to off
  2. if (temp > overTemp) - both button controls work ie Can turn on and off.

Ive been going over the code for many days, and a fresh pair of eyes/comments would be greatly appreciated.

I hope ive explained it enough for you all to understand my issue

my code in part. if you need the whole thing just ask

void loop()
{
  digitalWrite(RELAY1, relayState1); //define relay1 as global
  digitalWrite(RELAY2, relayState2); //define relay2 as global

  buttonState1 = digitalRead(buttonPin1);     // read the state of the pushbutton value:
  buttonState2 = digitalRead(buttonPin2);     // read the state of the pushbutton value:

  // Heat BUTTON check if the pushbutton is pressed. if it is, the buttonState is HIGH:
  if (buttonState1 == HIGH && previousState1 == LOW && millis() - time1 > debounce)
  {
    if (relayState1 == HIGH) // Go to Heat Mode:
    {
      opState = HEAT; // start Heat control
    }
    else
    {
      opState = OFF; // turn off control
    }
    time1 = millis();
    previousState1 == buttonState1;
  }

  // Filter BUTTON check if the pushbutton is pressed. if it is, the buttonState is HIGH:
  if (buttonState2 == HIGH && previousState2 == LOW && millis() - time2 > debounce)
  {
    if (relayState2 == HIGH) // Go to Filter mode:
    {
      opState = AUTO; // start filter control
    }
    else
    {
      opState = OFF; // turn off control
    }
    time2 = millis();
    previousState2 == buttonState2;
  }


  // thermistor Coding
  double temp;
  temp = Thermistor(analogRead(ThermistorPIN));

  //Temp LCD data
  lcd.setCursor(0, 3);
  lcd.print("Temperature");

  lcd.setCursor(12, 3);
  lcd.print(temp);

  lcd.setCursor(17, 3);
  lcd.print((char)223);
  lcd.print("C");

  //LCD STATIC DATA
  lcd.setCursor(0, 0);
  lcd.print("Operating Mode:");

  lcd.setCursor(0, 1);
  lcd.print("Set Temp To:");

  lcd.setCursor(14, 1);
  lcd.print((char)223);
  lcd.print("C");

  lcd.setCursor(0, 2);
  lcd.print("PUMP:");

  lcd.setCursor(10, 2);
  lcd.print("HEATER:");

  switch (opState)
  {
    case OFF:
      off();
      delay(100);
      break;
    case HEAT:
      setTemp();
      delay(100);
      break;
    case AUTO:
      filter();
      delay(100);
      break;
  }
}


void off()
{
  relayState2 = HIGH; //pump off
  relayState1 = HIGH; // heater off

  lcd.setCursor(15, 0);
  lcd.print("     ");
  lcd.setCursor(15, 0);
  lcd.print("OFF");

  lcd.setCursor(12, 1);
  lcd.print("00");

  lcd.setCursor(5, 2);
  lcd.print("   ");//clear
  lcd.setCursor(5, 2);
  lcd.print("OFF");//PUMP OFF

  lcd.setCursor(17, 2);
  lcd.print("   ");//clear
  lcd.setCursor(17, 2);
  lcd.print("OFF");//HEATER OFF

}



void setTemp()
{
  lcd.setCursor(15, 0);
  lcd.print("     ");
  lcd.setCursor(15, 0);
  lcd.print("HEAT");

  int overTemp = 35; //over ride temp

  //Pot coding
  sensorValue = analogRead(potPin); // read analog in value
  outputValue = map(sensorValue, 0, 1022.90, 0.0, 35.9); // map it to the range of the analog out
  potVal = outputValue;

  // thermistor Coding
  double temp;
  temp = Thermistor(analogRead(ThermistorPIN));
  // POT LCD data
  lcd.setCursor(12, 1);
  lcd.print("  ");
  lcd.setCursor(12, 1);
  lcd.print(potVal);
  delay(100);

  if (potVal > temp)
  {
    
    lcd.setCursor(12, 1);
    lcd.print("  ");
    lcd.setCursor(12, 1);
    lcd.print(potVal);
    relayState2 = LOW; //pump on
    relayState1 = LOW; //heater on

    lcd.setCursor(5, 2);
    lcd.print("   ");//clear
    lcd.setCursor(5, 2);
    lcd.print("ON");//PUMP ON
    lcd.setCursor(17, 2);
    lcd.print("   ");//clear
    lcd.setCursor(17, 2);
    lcd.print("ON");//HEATER ON
  }

  if (potVal < temp)
  {
    relayState2 == LOW;
    lcd.setCursor(12, 1);
    lcd.print("  ");
    lcd.setCursor(12, 1);
    lcd.print(potVal);
    relayState2 = LOW; //pump on
    relayState1 = HIGH; // heater off

    lcd.setCursor(5, 2);
    lcd.print("   ");//clear
    lcd.setCursor(5, 2);
    lcd.print("ON");//PUMP ON
    lcd.setCursor(17, 2);
    lcd.print("   ");//clear
    lcd.setCursor(17, 2);
    lcd.print("OFF");//HEATER OFF

  }

  if (temp > overTemp)
  {
    lcd.setCursor(12, 1);
    lcd.print("  ");
    lcd.setCursor(12, 1);
    lcd.print(potVal);
    relayState2 = HIGH; //pump off
    relayState1 = HIGH; //heater off

    lcd.setCursor(5, 2);
    lcd.print("   ");//clear
    lcd.setCursor(5, 2);
    lcd.print("OFF");//PUMP OFF

    lcd.setCursor(17, 2);
    lcd.print("   ");//clear
    lcd.setCursor(17, 2);
    lcd.print("OFF");//HEATER OFF

  }
}

if you need the whole thing just ask

You've got that backwards. If you want help, post ALL of your code, and explain how the switches are wired. In the absence of verifiable information, we can only surmise that the switches are wired incorrectly.

  if (buttonState1 == HIGH && previousState1 == LOW && millis() - time1 > debounce)
  {

It's far easier to debug code like that in three if statements.

    previousState1 == buttonState1;

Why are you comparing values, and discarding the result of the comparison?

  outputValue = map(sensorValue, 0, 1022.90, 0.0, 35.9); // map it to the range of the analog out

Rubbish. Read what the argument types are.

    relayState2 == LOW;

Wrong.

Sorry Paul below is the entire code for you to view. its still a little rough around the edges and from you're previous comments you have found some code that i've inserted (playing around) as i'm new to coding and trying to understand how it works.

The wiring is working with the current code its just this one section of code that doesn't work.

// ************************************************
//Relay values
// ************************************************
#define RELAY1 7
#define RELAY2 8
int relayState1 = HIGH;            //define relay status
int relayState2 = HIGH;            //define relay status
// ************************************************


// ************************************************
// Button values
// ************************************************
int buttonPin1 = 2;     // the number of the pushbutton pin
int buttonPin2 = 3;     // the number of the pushbutton pin
int buttonState1 = 0;            // variable for reading the pushbutton status
int previousState1 = LOW;     // other button mode
int buttonState2 = 0;             // variable for reading the pushbutton status
int previousState2 = LOW;
long time1 = 0;                // other button modelong time = 0;
long time2 = 0;
int debounce = 150;
// ************************************************


// ************************************************
// States for state machine
//************************************************
enum operatingState { OFF = 0, AUTO, HEAT}; //can add more states
operatingState opState = OFF;
// ************************************************


// ************************************************
// LCD Code
// ************************************************
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
// ************************************************


// ************************************************
// Analog temp code
// ************************************************
#include <math.h>
#define ThermistorPIN 0   // Analog Pin 0
double Thermistor(int RawADC) {
  double Temp;
  Temp = log(10000.0 * ((1024.0 / RawADC - 1)));
  Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp )) * Temp );
  Temp = Temp - 273.15;            // Convert Kelvin to Celcius
  return Temp;
}
// ************************************************


// ************************************************
// Potentiometer values
// ************************************************
const int potPin = A2;   // select the input pin for the potentiometer
const int potOutPin = 3;    // variable to store pot value
int sensorValue = 0.0;  // value read from the potentiometer
int outputValue = 0.0;  // value output to the PWM (analog out)
int potVal = 0;
// ************************************************


void setup()
{
  // Start up the library
  Serial.begin(9600);
  lcd.begin(20, 4);        // initialize the lcd for 20 chars 4 lines and turn on backlight

  // LCD ------ Quick 3 blinks of backlight  -------------
  for (int i = 0; i < 3; i++)
  {
    lcd.backlight();
    delay(250);
    lcd.noBacklight();
    delay(250);
  }
  lcd.backlight(); // finish with backlight on

  lcd.setCursor(0, 0);
  lcd.print("Spa Controller v2.2");
  delay(5000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Relays OFF");
  lcd.setCursor(0, 1);
  lcd.print("Moving to Manual");
  lcd.setCursor(0, 2);
  lcd.print("Control");
  delay(5000);
  lcd.clear();


  //start up the buttons and relays
  pinMode(buttonPin1, INPUT);       // initialize the pushbutton pin as an input
  pinMode(RELAY1, OUTPUT);         // Initialise the Arduino data pins for OUTPUT
  pinMode(buttonPin2, INPUT);       // initialize the pushbutton pin as an input
  pinMode(RELAY2, OUTPUT);         // Initialise the Arduino data pins for OUTPUT
}

void loop()
{
  digitalWrite(RELAY1, relayState1); //define relay1 as global
  digitalWrite(RELAY2, relayState2); //define relay2 as global

  buttonState1 = digitalRead(buttonPin1);     // read the state of the pushbutton value:
  buttonState2 = digitalRead(buttonPin2);     // read the state of the pushbutton value:

  // Heat BUTTON check if the pushbutton is pressed. if it is, the buttonState is HIGH:
  if (buttonState1 == HIGH && previousState1 == LOW && millis() - time1 > debounce)
  {
    if (relayState1 == HIGH) // Go to Heat Mode:
    {
      opState = HEAT; // start Heat control
    }
    else if (relayState1 == LOW)
    {
      opState = OFF; // turn off control
    }
    time1 = millis();
    //previousState1 == buttonState1;
  }

  // Filter BUTTON check if the pushbutton is pressed. if it is, the buttonState is HIGH:
  if (buttonState2 == HIGH && previousState2 == LOW && millis() - time2 > debounce)
  {
    if (relayState2 == HIGH) // Go to Filter mode:
    {
      opState = AUTO; // start filter control
    }
    else
    {
      opState = OFF; // turn off control
    }
    time2 = millis();
    //previousState2 == buttonState2;
  }


  // thermistor Coding
  double temp;
  temp = Thermistor(analogRead(ThermistorPIN));

  //Temp LCD data
  lcd.setCursor(0, 3);
  lcd.print("Temperature");

  lcd.setCursor(12, 3);
  lcd.print(temp);

  lcd.setCursor(17, 3);
  lcd.print((char)223);
  lcd.print("C");

  //LCD STATIC DATA
  lcd.setCursor(0, 0);
  lcd.print("Operating Mode:");

  lcd.setCursor(0, 1);
  lcd.print("Set Temp To:");

  lcd.setCursor(14, 1);
  lcd.print((char)223);
  lcd.print("C");

  lcd.setCursor(0, 2);
  lcd.print("PUMP:");

  lcd.setCursor(10, 2);
  lcd.print("HEATER:");

  switch (opState)
  {
    case OFF:
      off();
      delay(100);
      break;
    case HEAT:
      setTemp();
      delay(100);
      break;
    case AUTO:
      filter();
      delay(100);
      break;
  }
}

// ************************************************
// Initial State -
// ************************************************
void off()
{
  relayState2 = HIGH;
  relayState1 = HIGH;

  lcd.setCursor(15, 0);
  lcd.print("     ");
  lcd.setCursor(15, 0);
  lcd.print("OFF");

  lcd.setCursor(12, 1);
  lcd.print("00");

  lcd.setCursor(5, 2);
  lcd.print("   ");//clear
  lcd.setCursor(5, 2);
  lcd.print("OFF");//PUMP OFF

  lcd.setCursor(17, 2);
  lcd.print("   ");//clear
  lcd.setCursor(17, 2);
  lcd.print("OFF");//HEATER OFF

}


void filter()
{
  lcd.setCursor(15, 0);
  lcd.print("     ");
  lcd.setCursor(15, 0);
  lcd.print("AUTO");

  lcd.setCursor(5, 2);
  lcd.print("   ");//clear
  lcd.setCursor(5, 2);
  lcd.print("ON");//PUMP ON
  lcd.setCursor(17, 2);
  lcd.print("   ");//clear
  lcd.setCursor(17, 2);
  lcd.print("N/A");//HEATER N/A

  relayState2 = LOW; //pump on
  relayState1 = HIGH; // heater off

}

void setTemp()
{
  lcd.setCursor(15, 0);
  lcd.print("     ");
  lcd.setCursor(15, 0);
  lcd.print("HEAT");

  int overTemp = 35; //over ride temp

  //Pot coding
  sensorValue = analogRead(potPin); // read analog in value
  outputValue = map(sensorValue, 0, 1022.90, 0.0, 35.9); // map it to the range of the analog out
  potVal = outputValue;

  // thermistor Coding
  double temp;
  temp = Thermistor(analogRead(ThermistorPIN));
  // POT LCD data
  lcd.setCursor(12, 1);
  lcd.print("  ");
  lcd.setCursor(12, 1);
  lcd.print(potVal);
  delay(100);

  if (potVal > temp)
  {
    
    lcd.setCursor(12, 1);
    lcd.print("  ");
    lcd.setCursor(12, 1);
    lcd.print(potVal);
    relayState2 = LOW; //pump on
    relayState1 = LOW; //heater on

    lcd.setCursor(5, 2);
    lcd.print("   ");//clear
    lcd.setCursor(5, 2);
    lcd.print("ON");//PUMP ON
    lcd.setCursor(17, 2);
    lcd.print("   ");//clear
    lcd.setCursor(17, 2);
    lcd.print("ON");//HEATER ON
  }

  if (potVal < temp)
  {
    
    lcd.setCursor(12, 1);
    lcd.print("  ");
    lcd.setCursor(12, 1);
    lcd.print(potVal);
    relayState2 = LOW; //pump on
    relayState1 = HIGH; // heater off

    lcd.setCursor(5, 2);
    lcd.print("   ");//clear
    lcd.setCursor(5, 2);
    lcd.print("ON");//PUMP ON
    lcd.setCursor(17, 2);
    lcd.print("   ");//clear
    lcd.setCursor(17, 2);
    lcd.print("OFF");//HEATER OFF

  }

  if (temp > overTemp)
  {
    lcd.setCursor(12, 1);
    lcd.print("  ");
    lcd.setCursor(12, 1);
    lcd.print(potVal);
    relayState2 = HIGH; //pump off
    relayState1 = HIGH; //heater off

    lcd.setCursor(5, 2);
    lcd.print("   ");//clear
    lcd.setCursor(5, 2);
    lcd.print("OFF");//PUMP OFF

    lcd.setCursor(17, 2);
    lcd.print("   ");//clear
    lcd.setCursor(17, 2);
    lcd.print("OFF");//HEATER OFF

  }
}
map(value, fromLow, fromHigh, toLow, toHigh)
//or
long map(long x, long in_min, long in_max, long out_min, long out_max)

yet you have

outputValue = map(sensorValue, 0, 1022.90, 0.0, 35.9);
void loop()
{
  digitalWrite(RELAY1, relayState1); //define relay1 as global
  digitalWrite(RELAY2, relayState2); //define relay2 as global

It is NOT necessary to tell the pins, over and over, to be in some state. Once put in a state, they stay in that state until the Arduino is reset or you change the state.

    //previousState1 == buttonState1;

Why did you comment out this code, instead of fixing it?

Thank you for those comments, while you have picked at my code for errors (which is expected for a beginner) i was hoping that youd look past this and address the issue that was associated with this post.

Moving forward the coding mistakes/issues identified will be addressed later, however I dont understand why one part of my code (the "if (potVal < temp)") does not allow the pushbutton to activate.

You help would be greatly appreciated.

you can not address coding errors later because you have no idea what the coding errors are causing.

pauls posted that the //previousState1 == buttonState1; needs to be fixed. Theres a lot of smart guy here but they are not going to fix your code (well not until you have either a interesting problem or you have at least tried to fix it with there help). They will point out where the problem is and give you pointers to what you need to learn to fix it.

small hint would be to look at the learning tab then look at references to see what == means

from the looks of your code, you keep setting it to heat and never turn it off when if(potVal < temp)