I am trying to make a pump pressure regulator with nextion as the pressure value input. how the value can be processed by arduino?
#include <SoftwareSerial.h>
#include <PID_v1.h>
SoftwareSerial mySerial(10, 11); /*Even though you can use the hardware serial port in this case I think it is better to
leave the hardware serial open for debugging purposes*/
double Setpoint ; // will be the desired value
double Input; // photo sensor
double Output ; //LED
double Kp=5, Ki=3.3, Kd=0.008;
//double Kp=3, Ki=1.2, Kd=0.01;
// double Kp=1.2, Ki=0.19, Kd=0.001;
//create PID instance
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
int rawValue; // A/D readings
int offset = 350; // zero pressure adjust
int fullScale = 9630; // max pressure (span) adjust
float pressure,mmhg; // final pressure
////////////////////////////////////// presure
int ardata[32]; // an array to store the received data
int count = 0;
int data;
int enA = 5;
int in1 = 4;
int in2 = 7;
int data_masuk;
boolean co=false;
void setup() {
Serial.begin(57600); //open the hardware serial port
Setpoint = 200;
//Turn the PID on
myPID.SetMode(AUTOMATIC);
//Adjust PID values
myPID.SetTunings(Kp, Ki, Kd);
myPID.SetOutputLimits(-255, 255);
myPID.SetSampleTime(3);
Serial.begin(9600); //open the hardware serial port
mySerial.begin(9600); // set the data rate for the SoftwareSerial port
}
void loop() {
rawValue = 0;
for (int x = 0; x < 10; x++) rawValue = rawValue + analogRead(A0);
pressure = (rawValue - offset) * 700.0 / (fullScale - offset); // pressure conversion
mmhg=pressure*7.50062;
delay(10);
Input = mmhg;
myPID.Compute();
analogWrite(13,Output); //LED is set to digital 3 this is a pwm pin.
if(Output < 0)
{ Output = 0; }
if(Output > 255)
{ Output = 255; }
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enA, Output);
Serial.print("t8.txt=\"");
Serial.print(mmhg, 1); // one decimal places
Serial.println(" mmhg ");
Serial.print("\"");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
Serial.print(" INPUT ");
Serial.print(Input);
Serial.print(" OUTPUT ");
Serial.print(Output);
Serial.print(" SET ");
Serial.println(Setpoint);
// Serial.println(" SET ");
delay(10);
String sendThis = ""; //Declare and initialise the string we will send
if(mySerial.available()) {
data = mySerial.read();
delay(50);
ardata[count] = data;
if(ardata[0]==79 || ardata[0]==83)
{ // Serial.print("counter ");// Serial.print(count);
Serial.print(" Data masuk ");
Serial.println(ardata[count]);
count++;
if(count == 5 && ardata[0]==83)
{
if(ardata[2]==1)
{
data_masuk = 256+ardata[1];//Serial.print("Lebih 255 :"); //Serial.println(data_masuk);
}
if(ardata[2]==2)
{
data_masuk = 512+ardata[1];
Serial.print("Lebih 511 :");
Serial.println(data_masuk);
}
data_masuk=ardata[1];
data_masuk=map(data_masuk,0,255,0,255);
Serial.println("Start");
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enA, data_masuk);
delay(10); // wait for a second
count=0;
}
if(count == 5 && ardata[0]==79)
{
data_masuk=ardata[1];
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
analogWrite(enA, ardata[1]);
delay(10);
Serial.println("Stop");
count=0;
}
if(count > 5)
{
count=0;
Serial.println("Clear");
// wait for a second
}
}
}
//delay(300); //Probably unneccessary, but I give the screen some time to respond
data=data_masuk;
sendThis = "n0.val="; //Build the part of the string that we know
sendThis.concat(data); //Add the variable we want to send
writeString(sendThis); /*Use a function to write the message character by character to the Nextion because
mySerial.write(sendThis) gives you an error due to a datatype mismatch*/
}
void writeString(String stringData) { // Used to serially push out a String with Serial.write()
for (int i = 0; i < stringData.length(); i++)
{
mySerial.write(stringData[i]); // Push each char 1 by 1 on each loop pass
}
mySerial.write(0xff); //We need to write the 3 ending bits to the Nextion as well
mySerial.write(0xff); //it will tell the Nextion that this is the end of what we want to send.
mySerial.write(0xff);
}// end writeString function