0

Some ideas please? I'm stuck and also don't judge me for how i wrote the code. I'm new into coding and still learn. So, the idea is that i want to display data and clock on lcd but when the lamp is on i want a message to be printed for 5 sec then the clock and date will be printed again... and so on. Data from serial is comming from an esp which trigger some event wireless.Arduino receive feedback that the job has been done and then display a message on lcd. More exactly.. Using mqtt - from browser i push a button to turn on a lamp. Once the lamp is on, a message arrives at esp from the mqtt broker that lamp is on now. esp then send data to arduino using serial. and arduino should display the text " Lamp on "

    #include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <DS3231.h>

LiquidCrystal_I2C lcd(0x3F,20,4);
DS3231 rtc (SDA, SCL);

int verificare_stare_releu = A13;
char a;

void setup(){
  Serial.begin(115200);
  rtc.begin();
  lcd.init();
  lcd.backlight();
  pinMode(A13,INPUT);
}

void loop(){

  int b = analogRead(verificare_stare_releu);
    if(!Serial.available()){
    display_clock();
    }
    if(!Serial.available()){
    if(b >= 900){
      lcd.print("Lamp ON ");
    }

    if( b < 900 ){
      lcd.print("Lamp OFF");  
    }

    }



  if(Serial.available()>0){
    a = Serial.read();
    if(a == 102){
    lcd.setCursor(0,0);
    lcd.print("Lamp ON ");
    }
  if(a == 110){
    lcd.setCursor(0,0);
    lcd.print("Lamp OFF");
  }  
  }


}

  //a = digitalRead(verificare_stare_releu);


void display_clock (){
   lcd.setCursor(0,0);
  lcd.print("Ora: "+(String)rtc.getTimeStr());
  lcd.setCursor(0,1);
  lcd.print((String)rtc.getDOWStr()+(String) rtc.getDateStr());
}
  • Have a look at the state-change-detection example: arduino.cc/en/Tutorial/StateChangeDetection When you turn the lamp into a on/off variable and remember the lastLampState, then you can detect a change and put the message on the display for 5 seconds. A analog input can be used without pinMode as you can see here: arduino.cc/reference/en/language/functions/analog-io/analogread Can you describe what the data from the serial port is used for? The sketch tests three times for input from the serial port, but you should test it once. Try to seperate the different things. – Jot Apr 14 at 7:09
  • Data from serial is comming from an esp which trigger some event wireless.Arduino receive feedback that the job has been done and then display a message on lcd. More exactly.. Using mqtt - from browser i push a button to turn on a lamp. Once the lamp is on, a message arrives at esp from the mqtt broker that lamp is on now. esp then send data to arduino using serial. and arduino should display the text " Lamp on " – Iulian Chirvasa Apr 14 at 7:19
  • Could you add that information to your question please. Please explain also the difference between serial data from the esp and the data from the analog pin. – Jot Apr 14 at 8:00
  • Can't say i was thinking clear when i thought that it can be done but i want to use both boards. Arduino Mega because it has so many I/O and some wireless device. Because i have only Nodemcu i use this.So what i want to do, is to communicate between Raspberry(as mqtt broker), Esp (mqtt client) and arduino. Arduino will have LCD and some sensors. I would like to display data,hour, temperature,humidity,pressure on it. But also, i want to display some events that i activate from the web side. As i said ..if i turn on a lamp, the esp will communicate with arduino using serial. – Iulian Chirvasa Apr 14 at 8:08
  • if the lamp is on then lcd will display some message to inform that for 5 sec. then will switch to swipe between rtc,pressure,dht, etc. – Iulian Chirvasa Apr 14 at 8:13
1

Although I still do not understand how to combine the data from the serial port with the analog input, I can show how to display a message for 5 seconds, using the Blink Without Delay example.

A boolean variable 'message' is used to prevent the normal code to use the display during the 5 seconds. The same boolean variable is used to check if 5 seconds have passed (using millis). Once the 5 seconds have passed, the boolean variable is set to 'false' and that releases the display so it can be used for normal things.

To start the 5 seconds, these two lines are required:

previousMillis = millis();
message = true;

When 'message' is set to true, the variable 'previousMillis' must be valid. Once 'message' is false, the value of 'previousMillis' is not used anymore.

The sketch can be like this:

unsigned long previousMillis;
const unsigned long interval = 5000;  // 5 seconds
bool message = false;     // true if message is displayed

void setup() {
  // the code for setup here
}

void loop() {
  if(!message) {          // message not displaying ?
    // show temperature, time, etc.
  }

  if(Serial.available() > 0) {
    int a = Serial.read();
    switch(a) {
      case 102:
        lcd.setCursor(0,0);
        lcd.print("Lamp ON ");
        previousMillis = millis(); // remember this moment
        message = true;    // set boolean variable
        break;
    }
  }

  if(message) {
    if(millis() - previousMillis >= interval) {
      // 5 seconds have passed ?
      message = false;    // release the display
    }
  }
}
  • @jsotola I tried to make a better explanation. Let me know if I need to improve it more. – Jot Apr 14 at 19:40
  • it seems clear enough now .... the comments in the code are self explanatory – jsotola Apr 14 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.