Okay, so I am COMPLETELY new to the Arduino and microcontrollers in general and have never written code before I took on this project. I’m currently working on having a TFT screen with buttons to open and close the valve and also to adjust the volume of liquid running through the flow sensor before the valve shuts on its own. So essentially the screen will have a start/stop button and a Gal/min live readout of the flow. the start/stop screen just opens and closes a valve. There will be another screen where you set the amount of fluid you want to dispense. So you would punch in like 5 gallons for example. Then, you would hit the start button and the valve would open to allow the water to flow until it hit 5 gallons, then it would automatically shut off. I have a feeling I’ll need to use interrupts, but I’m just unsure of how to write the sketch and research isn’t getting me far. Any help would be appreciated. I’ll include my sketch also.
As far as hardware goes I’m using an arduino uno, a 5v relay cube, a digiten FL-608 3/4" flow meter, and a 3.2" ITEAD Nextion TFT.
The Nextion TFT screens and buttons were programmed with the Nextion Software.
Again, I am a complete amateur, so try not to be too turned off by my code.
#include <SoftwareSerial.h>
#include <Nextion.h>
float value = 0; // value for amount to flow starts at 0
int valve=12;// valve goes on pin 12
int valveState;
SoftwareSerial nextion(2, 3);// Nextion TX to pin 2 and RX to pin 3 of Arduino
Nextion myNextion(nextion, 9600); //create a Nextion object named myNextion using the nextion serial port @ 9600bps
void incAmt()
{
value = (value+0.5);//add .5 to the value
myNextion.setComponentText("t0", String(value));//change the value on Nextion
}
void decAmt()
{
value = (value-0.5);//subtract .5 from the value
myNextion.setComponentText("t0", String(value));//change the value on the Nextion
}
void startFlow()
{
digitalWrite(valve,HIGH);//set the valve high
Serial.println(valveState);// and print whether the valve is high or low to the monitor
}
void stopFlow()
{
digitalWrite(valve,LOW);//set the valve low
Serial.println(valveState);// and print whether the valve is high or low to the monitor
}
void setAmt()
{
myNextion.setComponentText("t0", String(value));//display the current value set for the amount
}
void backToSetAmtScreen()
{
myNextion.setComponentText("t0", String(value));//set the amt value to the current amt
}
void setup() {
pinMode(valve,OUTPUT);//valve pin is an output
digitalWrite(valve,LOW);//valve pin starts out as low/shut
Serial.begin(9600);//open communication with serial monitor through usb
myNextion.init();//initialize connection with nextion screen
myNextion.setComponentText("t0", String(value));//Set starting value for amt
}
void loop() {
String message = myNextion.listen(); //check for message
valveState=digitalRead(valve);//valveState set to current state of the valve pin(high or low)
if(message !="")//if a message is received from nextion
{
Serial.println(message);//print message to serial monitor
}
if(message =="65 2 2 0 ffff ffff ffff")//if +.5 gallon button is pushed
{
incAmt();
}
if(message == "65 2 3 0 ffff ffff ffff")//if -.5 gallon button is pushed
{
decAmt();
}
if(message == "65 0 3 0 ffff ffff ffff" && (valveState==LOW))//if the start/stop button is pressed and the valve is currently low
{
startFlow();
}
if(message == "65 0 3 0 ffff ffff ffff"&& (valveState==HIGH))//if the stop/start button is pressed and the valve is currently high
{
stopFlow();
}
if(message=="65 1 3 0 ffff ffff ffff")// if the set amt button is pressed
{
setAmt();
}
if(message=="65 4 2 0 ffff ffff ffff")// if the back button is pressed from the enter amt screen
{
backToSetAmtScreen();
}
}