hello guys now i m using nextion display for my arduino projects since i am facing problem in switching manual mode to automode is not working
logic is
if manual mode then its fine but swiching to automode its always on my code is below
const int proxyPin = 2;
const int pumpPin = 12;
int proxyState = 1;
void setup()
{
Serial.begin(9600);
pinMode(pumpPin, OUTPUT);
pinMode(proxyPin, INPUT);
}
void loop()
{
proxyState = digitalRead(proxyPin); // auto mode
if(Serial.available()>0)
{
String Received = Serial.readString();
if(int(Received[0]) == 1) // manual mode
{
Serial.print("pump on ");
digitalWrite(pumpPin,LOW);
}
if(int(Received[0]) == 0)
{
Serial.print("pump off");
digitalWrite(pumpPin,HIGH);
}
if(String(Received[0]) == 2)
{
if (proxyState == LOW)
{
digitalWrite(pumpPin,LOW);
delay(5000);
digitalWrite(pumpPin,HIGH);
delay(60000);
}
}
}
}
Hello @sahiltarapada
Welcome.
Please start here How to get the best out of this forum , particularly the part about correctly posting code. Please go back and edit your post to use code tags </> for the code, as explained in the instructions.
There is nothing in your code that will read data from a Nextion display. Please read this tutorial to get some ideas about how to handle data from a Nextion display Using Nextion displays with Arduino
Your code is blocking with the delays. You are not going to get far with blocking code. The 60 second delays mean that for 60 seconds your code will do nothing, so anything anyone presses on the Nextion display will be ignored for 60 seconds. Please study the following tutorials:
Using millis for timing
Demonstration for several things at the same time
There is a recurring theme of people new to Arduino and/or microcontrollers not grasping the concept of putting what I call ‘building blocks’ of code together to achieve some goal. These short sketches are an attempt to offer some examples showing how small pieces can be used in concert to build up larger, more complex sketches.
Thanks to @PerryBebbington and @ballscrewbob for support and input. You made it better.
The examples will illustrate:
• Doing ‘several things at once ’
• pitfalls of …
I don't get the String obsession surfacing in this sketch.
If you want to react to single character commands, Strings do not help you in any way.
I think this code is a lot cleaner:
const int proxyPin = 2;
const int pumpPin = 12;
uint8_t proxyState;
void setup() {
digitalWrite(pumpPin, HIGH);
pinMode(pumpPin, OUTPUT);
digitalWrite(pumpPin, HIGH);
pinMode(proxyPin, INPUT_PULLUP);
Serial.begin(115200);
Serial.println(F("\npump control\n\n0 - off\n1 - on\n2 - one shot automatic\n"));
}
void loop() {
if (Serial.available()) {
proxyState = digitalRead(proxyPin);
uint8_t rxByte = Serial.read();
switch (rxByte) {
case '1':
Serial.println(F("pump on "));
digitalWrite(pumpPin, LOW);
break;
case '0':
Serial.println(F("pump off"));
digitalWrite(pumpPin, HIGH);
break;
case '2':
if (proxyState == LOW) {
Serial.println(F("blocking auto pump started"));
digitalWrite(pumpPin, LOW);
delay(5000);
digitalWrite(pumpPin, HIGH);
Serial.println(F("blocking auto pump switches off pump"));
delay(60000);
Serial.println(F("blocking auto pump done"));
} else {
Serial.println(F("NO auto pump, proxyState is HIGH"));
}
break;
case '\r': // ignore line ending(s)
case '\n':
break;
default:
Serial.print(F("no command attached to '"));
Serial.write(rxByte);
Serial.println(F("'"));
}
}
}
I would not use delay, but that is not the topic here.
system
Closed
March 25, 2022, 9:22am
4
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.