OK. I was worried about the analog write in loop()
Maybe make the values match the byte-sized storage:
byte val1 = interval / 400; // value is more than 255, approx 600000
byte val2 = tempToStar; // value is less than 255
byte val3 = potPosition; // value is between 0 and 99
byte val4 = PIDTemp; // value is less than 255
I can't fly low over your code from here just now.
I do recommend that these multiple expressions
digitalRead(47) == HIGH
be handled the same way you do
int OK = digitalRead(45); // OK pin
that is to say, read it once. I don't know what kind of signal is on pin. If you read it once, I wouldn't need to know that, for example, it was a pushbutton which can very easily reutrn different values when you look at it with the very briefest time between readings.
State = process state where you break a process into states/steps/modes that may begin with waiting for input and include error handling. Almost always a switch-case structure is used but that's not required to be a state machine. The value of state may be a global or static to a function variable, when an input is detected, the waiting for state gets a read and changes state to accept data, etc.
Every case needs code suitable to conditions and the process.
In the 80's the home computing magazines had articles about Main Loop coding letting code do many things at the same time smoothly on 1MHz 6502's and not much faster Z80's. That lesson applies to Arduino only an AVR like Uno and Mega is effectively at least 20x faster! It makes smooth tasking easier.
What state machines do is let the same function cover a process. To take a step then exit and let other functions take theirs and then the functions run again to take their next step. Note that when time is involved, a whole lot of times function may check if a wait is over before running the machine. Some functions take shorter steps.
2 Heater Solid State Relay
8 Process
47 Heater option, if it is HIGH, heater option is ON, physically not connected to anything
45 OK pin
bt8 Heater LED (light up when SSR is ON)
bt28 Heater LED on PID page 2, does the same as bt8
Basically, what this block needs to do is the following:
There is an option to start the process of Pin 8 (turn it HIGH) using heater or not. The heater option is set via pin 47, when pin 47 is LOW, heater option is OFF, when it is HIGH, heater option is ON.
When the Heater option is set to on, then it need to heat up a liquid and maintain it at the variable PIDTemp.
While the liquid is rising in temperature, the process (pin 8) need to start when temperature set via tempToStart variable is reached.
Here is my updated code for this block:
int OK = digitalRead(45); // OK pin
int HeatOption = digitalRead(47); // Used to set ON or OFF Heater option, not connected to anything
int PIDHeater = digitalRead(2); // Used to turn ON or OFF the SSR for the Heater
int Process = digitalRead(8); // Used to turn ON or OFF the Process
if ((OK == HIGH) && (HeatOption == LOW)) {
digitalWrite(Process, HIGH); // Pin 8 turn ON Process
digitalWrite(9, HIGH); // Pin 9 turn ON Cover LEDs
Serial.println("Starting Process");
}
if (((thermocouple->readCelsius() < PIDTemp) && (OK == HIGH) && (HeatOption == HIGH))) {
digitalWrite(PIDHeater, HIGH); // Pin 2 turn ON Heater via SSR
myNex.writeNum("bt8.val", 1);
myNex.writeNum("bt28.val", 1);
Serial.println("Starting PID Heater");
}
if (((thermocouple->readCelsius() >= PIDTemp-3) && (OK == HIGH) && (HeatOption == HIGH))) {
digitalWrite(PIDHeater, LOW);
myNex.writeNum("bt8.val", 0);
myNex.writeNum("bt28.val", 0);
Serial.println("Heater PID OFF");
}
if (((thermocouple->readCelsius() < PIDTemp -2) && (OK == HIGH) && (HeatOption == HIGH))) {
digitalWrite(PIDHeater, HIGH);
myNex.writeNum("bt8.val", 1);
myNex.writeNum("bt28.val", 1);
Serial.println("Starting PID Heater");
}
if (((thermocouple->readCelsius() > tempToStart) && (OK == HIGH) && (HeatOption == HIGH))) {
digitalWrite(Process, HIGH); // Pin to turn ON Process
digitalWrite(9, HIGH); // Pin to turn ON Cover LEDs
Serial.println("Starting Process");
}
Also, another case where you might benefit, and it certainly would not hurt, woukd be to read the thermocouple but once per loop, as you do with several other inputs
thermocouple->readCelsius()
I can't tell the variable type, which is part of why I was looking to see if you'd ever posted any complete it compiles and we could run it ourselves version of your code.
That code it oddly indented, and has some duplicate logic in it. Why not take the common condition of the ifs and wrap it around simpler others:
int OK = digitalRead(45); // OK pin
int HeatOption = digitalRead(47); // Used to set ON or OFF Heater option, not connected to anything
int PIDHeater = digitalRead(2); // Used to turn ON or OFF the SSR for the Heater
int Process = digitalRead(8); // Used to turn ON or OFF the Process
if ((OK == HIGH) && (HeatOption == LOW)) {
digitalWrite(Process, HIGH); // Pin 8 turn ON Process
digitalWrite(9, HIGH); // Pin 9 turn ON Cover LEDs
Serial.println("Starting Process");
}
if ((OK == HIGH) && (HeatOption == HIGH)) {
if (thermocouple->readCelsius() < PIDTemp) {
digitalWrite(PIDHeater, HIGH); // Pin 2 turn ON Heater via SSR
myNex.writeNum("bt8.val", 1);
myNex.writeNum("bt28.val", 1);
Serial.println("Starting PID Heater");
}
if (thermocouple->readCelsius() >= PIDTemp - 3) {
digitalWrite(PIDHeater, LOW);
myNex.writeNum("bt8.val", 0);
myNex.writeNum("bt28.val", 0);
Serial.println("Heater PID OFF");
}
if (thermocouple->readCelsius() < PIDTemp - 2) {
digitalWrite(PIDHeater, HIGH);
myNex.writeNum("bt8.val", 1);
myNex.writeNum("bt28.val", 1);
Serial.println("Starting PID Heater");
}
if (thermocouple->readCelsius() > tempToStart) {
digitalWrite(Process, HIGH); // Pin to turn ON Process
digitalWrite(9, HIGH); // Pin to turn ON Cover LEDs
Serial.println("Starting Process");
}
}
It makes it simpler, quicker, and easier to read. And easier to identify that the "if (thermocouple->readCelsius() < PIDTemp)" clause makes this clause appear completely redundant:
int OK = digitalRead(45); // OK pin
int HeatOption = digitalRead(47); // Used to set ON or OFF Heater option, not connected to anything
int PIDHeater = 2; // Used to turn ON or OFF the SSR for the Heater
int Process = 8; // Used to turn ON or OFF the Process
int CoverLEDs = 9; // Used to turn ON or OFF Cover LEDs
Complete block...
int OK = digitalRead(45); // OK pin
int HeatOption = digitalRead(47); // Used to set ON or OFF Heater option, not connected to anything
int PIDHeater = 2; // Used to turn ON or OFF the SSR for the Heater
int Process = 8; // Used to turn ON or OFF the Process
int CoverLEDs = 9; // Used to turn ON or OFF Cover LEDs
if ((OK == HIGH) && (HeatOption == LOW)) {
digitalWrite(Process, HIGH); // Pin 8 turn ON Process
digitalWrite(CoverLEDs, HIGH); // Pin 9 turn ON Cover LEDs
Serial.println("Starting Process");
}
if (((thermocouple->readCelsius() < PIDTemp) && (OK == HIGH) && (HeatOption == HIGH))) {
digitalWrite(PIDHeater, HIGH); // Pin 2 turn ON Heater
myNex.writeNum("bt8.val", 1);
myNex.writeNum("bt28.val", 1);
Serial.println("Starting PID Heater");
}
if (((thermocouple->readCelsius() >= PIDTemp-3) && (OK == HIGH) && (HeatOption == HIGH))) {
digitalWrite(PIDHeater, LOW); // Pin 2 turn OFF Heater
myNex.writeNum("bt8.val", 0);
myNex.writeNum("bt28.val", 0);
Serial.println("Heater PID OFF");
}
if (((thermocouple->readCelsius() < PIDTemp -2) && (OK == HIGH) && (HeatOption == HIGH))) {
digitalWrite(PIDHeater, HIGH); // Pin 2 turn ON Heater
myNex.writeNum("bt8.val", 1);
myNex.writeNum("bt28.val", 1);
Serial.println("Starting PID Heater");
}
if (((thermocouple->readCelsius() > tempToStart) && (OK == HIGH) && (HeatOption == HIGH))) {
digitalWrite(Process, HIGH); // Pin 8 to turn ON Process
digitalWrite(CoverLEDs, HIGH); // Pin to turn ON Cover LEDs
Serial.println("Starting Process");
}
Not sure what you mean, I am a newbie with Arduino coding, learning it bits by bits... ;o)
thermocouple->readCelsius()
is the reading of a temperature sensor displayed in real time on the Nextion Display.
PIDTemp is the Set temperature that the heater needs to reach and maintained.
PIDHeater is Pin 2 that turn ON or OFF a Solid State Relay connected to the Heater.
tempToStart is the temperature to start the "Process" and is set via a number field on the Nextion Display.
Do you intend to do anything different if the temperature is below PIDtemp - 2, beyond what you did if the temp is below PIDtemp?
The >= PIDTemp-3 and < PIDTemp -2 is to simulate a PID Controller, so the heater's Solid State Relay does not over/under shoot. I'm heating a liquid and it takes time to reach set temperature.
This is only a small portion of the project, everything will turn OFF after it reaches another sets of instructions..
Yes, I get what you meant now, I removed 2 if statements and changed this block to this:
int MainPower = 48; // Used to turn ON or OFF Main Power
int OK = digitalRead(45); // OK pin
int HeatOption = digitalRead(47); // Used to set ON or OFF Heater option, not connected to anything
int PIDHeater = 53; // Used to turn ON or OFF the SSR for the Heater
int Process = 8; // Used to turn ON or OFF the Process
int CoverLEDs = 9; // Used to turn ON or OFF Cover LEDs
if ((OK == HIGH) && (HeatOption == LOW)) {
digitalWrite(Process, HIGH); // Pin 8 turn ON Process
digitalWrite(CoverLEDs, HIGH); // Pin 9 turn ON Cover LEDs
Serial.println("Starting Process");
}
if (((thermocouple->readCelsius() < PIDTemp -2) && (OK == HIGH) && (HeatOption == HIGH))) {
digitalWrite(PIDHeater, HIGH); // Pin 2 turn ON Heater
myNex.writeNum("bt8.val", 1);
myNex.writeNum("bt28.val", 1);
Serial.println("Starting PID Heater");
}
if (((thermocouple->readCelsius() >= PIDTemp-7) && (OK == HIGH) && (HeatOption == HIGH))) {
digitalWrite(PIDHeater, LOW); // Pin 2 turn OFF Heater
myNex.writeNum("bt8.val", 0);
myNex.writeNum("bt28.val", 0);
Serial.println("Heater PID OFF");
}