Arduino with nextion display

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

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.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.