Hi, I am a very basic coder, I try to use examples then modify from there, so if my code is a bit over the place I apologise.
I have a Nextion LCD display and a pressure transducer with a relay and valve. (Relay/Valve has own power supply, relay shares same ground as my Nano)
I have the hardware working ok, however I am getting stuck on a starting variable. I want to have the initial valve set to 15 psi then the buttons on the screen can adjust from there. (ultimately I will eventually get to writing it to EEprom so if there is loss of power it is already set.)
The problem I have is once the valve is triggered and the value of the pressure drops below the threshold the set vale goes back to the initial value of 15. (I can't figure out why, I'm sure its really simple, but it evades me.
This is the trigger for the valve.
if (pressureValue1 >= setpsi1) digitalWrite(RELAY_PIN1, LOW);
if (pressureValue1 < setpsi1) digitalWrite(RELAY_PIN1, HIGH);
My Full code
#include <Arduino.h>
#include <softwareserial.h>
#define rxPin 8
#define txPin 9
SoftwareSerial mySerial = SoftwareSerial(rxPin,txPin);
const int RELAY_PIN1 = PIND7;
const int RELAY_PIN2 = PIND6;
const int pressureInput1 = A1; //select the analog input pin for the pressure transducer
const int pressureZero = 102.4; //analog reading of pressure transducer at 0psi
const int pressureMax = 921.6; //analog reading of pressure transducer at 100psi
const int pressuretransducermaxPSI = 30; //psi value of transducer being used
float pressureValue1 = 0; //variable to store the value coming from the pressure transducer
const int pressureInput2 = A7; //select the analog input pin for the pressure transducer
float pressureValue2 = 0; //variable to store the value coming from the pressure transducer
float setpsi1;
float setpsi2;
void updatesetpsi1(){
String updatepsi1(setpsi1,1);
mySerial.print(F("t3.txt=\""));
mySerial.print(updatepsi1);
mySerial.print(" psi");
mySerial.print("\"");
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
}
void updatesetpsi2(){
String updatepsi2(setpsi2,1);
mySerial.print(F("t4.txt=\""));
mySerial.print(updatepsi2);
mySerial.print(" psi");
mySerial.print("\"");
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
}
void sendData(String data_from_display){
if (data_from_display == "1up") {
setpsi1 = setpsi1 + 1;
updatesetpsi1();
}
if (data_from_display == "1down") {
setpsi1 = setpsi1 -1;
updatesetpsi1();
}
if (data_from_display == "2up") {
setpsi2 = setpsi2 + 1;
updatesetpsi2();
}
if (data_from_display == "2down") {
setpsi2 = setpsi2 - 1;
updatesetpsi2();
}
}
void setup() {
// Serial.begin(115200);
mySerial.begin(9600);
delay(50); // This dalay is just in case the nextion display didn't start yet, to be sure it will receive the following command.
mySerial.print("bauds=115200"); // Set new baud rate of nextion to 115200, but it's temporal. Next time nextion is power on,
// // it will retore to default baud of 9600.
// // To take effect, make sure to reboot the arduino (reseting arduino is not enough).
// // If you want to change the default baud, send the command as "bauds=115200", instead of "baud=115200".
// // If you change the default baud, everytime the nextion is power ON is going to have that baud rate, and
// // would not be necessery to set the baud on the setup anymore.
mySerial.write(0xff); // We always have to send this three lines after each command sent to nextion.
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.begin(115200); // Start serial comunication at baud=115200
pinMode(RELAY_PIN1, OUTPUT);
pinMode(RELAY_PIN2, OUTPUT);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
digitalWrite(RELAY_PIN1, LOW);
digitalWrite(RELAY_PIN2, LOW);
setpsi1 = 15;
setpsi2 = 15;
}
void loop(){
if (mySerial.available()){
String data_from_display = "";
delay(20);
while (mySerial.available()){
data_from_display += char(mySerial.read());
}
sendData(data_from_display);
}
pressureValue1 = analogRead(pressureInput1); //reads value from input pin and assigns to variable
pressureValue2 = analogRead(pressureInput2); //reads value from input pin and assigns to variable
pressureValue1 = ((pressureValue1-pressureZero)*pressuretransducermaxPSI)/(pressureMax-pressureZero); //conversion equation to convert analog reading to psi
pressureValue2 = ((pressureValue2-pressureZero)*pressuretransducermaxPSI)/(pressureMax-pressureZero); //conversion equation to convert analog reading to psi
if (pressureValue1 >= setpsi1) digitalWrite(RELAY_PIN1, LOW);
if (pressureValue1 < setpsi1) digitalWrite(RELAY_PIN1, HIGH);
if (pressureValue2 >= setpsi2) digitalWrite(RELAY_PIN2, LOW);
if (pressureValue2 < setpsi2) digitalWrite(RELAY_PIN2, HIGH);
delay(20);
String pressure1(pressureValue1, 1);
String pressure2(pressureValue2, 1);
mySerial.print(F("t1.txt=\""));
mySerial.print(pressure1);
mySerial.print(" psi");
mySerial.print("\"");
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.print(F("t2.txt=\""));
mySerial.print(pressure2);
mySerial.print(" psi");
mySerial.print("\"");
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
// Serial.print(setpsi1);
// Serial.print(" psi ");
// Serial.print( setpsi2 );
// Serial.println();
}