Newbie with a LCD Widget question

First off let me say Blynk is a amazing product! I literally had my Android phone controlling a Arduino Mega 2560 in just minutes. Considering my knowledge of writing Arduino code is very limited I found this software extremely easy to use. By trade I am a Sr. Technician and spend my day automating industrial molding machines. I’m proficient in programming Allen Bradley & Modicon processors but not Arduino (not yet anyways). Since I do not understand Arduino complex code I use the PLC to monitor various sensors and control lights, door locks, and other items. In my quest for complete Home Automation I have added code to control and monitor my garage door.

My equipment - Arduino Mega 2560, 8 relay board, Allen Bradley MicroLogix 1400 PLC, and a home made input board with pull down resisters.
My Project - Complete Home Automation
My Question: I’d like to add a LCD Widget to the Blynk app that displays a status of my garage door and I’d like to do this without causing a flood error. I’m sorry that I cannot provide a copy of my Arduino project as I’m currently at work and have no access to it. I’m providing 3 inputs to the Mega from the PLC (Door open, door closed, and door moving). What I’m trying to do is have the LCD display “Garage Dr Open” when the DoorOpenPin goes high. Display “Garage Dr Closed” when DoorClosedPin goes high, Display “Garage Dr Moving” when DoorMovePin goes high, and for the LCD to display “Door Jammed” when none of the digital pins are high (or all Dpins are low). I’ve played with the code for a few days trying to apply my needs to available sketches. I have been unsuccessful in displaying anything on the LCD Widget other than the original “Hello World”. I have also noticed since I have been playing with lcd,print my app’s response time has greatly increased. Sometime it takes 20-40 seconds to complete a request when it was almost instant before. I suspect I have created a flood error because I get a message on the app that I have lost communication with the server which I have never seen before. If you could provide a sample code for making the LCD print my messages I’d really appreciate it.

When I have used the LCD widget I have always used ADVANCED rather than SIMPLE mode.
I NEVER use a frequency other than PUSH with any of the widgets. To be honest I think Blynk should perhaps consider setting PUSH as the default frequency for all widgets rather than 1 sec.

Other than that it is simply lcd.print(x, y, “message”); in the appropriate places in your sketch.

Remembering y is just row 0 or row 1 and x is from 0 to 15.

I am using Advanced mode. I followed the LCD Advanced sketch and can send general messages to my phone like “Hello World”. I just need to figure out the code to display a message when one of three digital pins are high (or when all are low). As far as a avoiding a flood condition I tried to implement a timer but I may have done it incorrectly. I realize its hard to get help with this without displaying my work - I will try to add my sketch when I get home tonight. I guess because I work with ladder logic all day I’m having a hard time translating to Arduino code and I’m getting lost. I spent almost an hour troubleshooting the other day because I forgot a simple “}”. Maybe I need some real basics…

difficult to advise further without your sketch but if you have 3 digital pins (2, 4 and 5) then perhaps something like this to check if they are all HIGH:

  if((digitalRead(2) == 1) && (digitalRead(4) == 1) && (digitalRead(5) == 1)){
    lcd.print(0, 0, "all pins are high");
    // do something  
  }

You should use SimpleTimer and call the above in a function at say 500ms intervals.

Okay so I spent the weekend playing with the code. Good news by reading some examples I found in the community I built a sketch that is almost there. It is displaying door open and door closed but that is it (and no more flood issues). I would really like to have it also display “door moving & door stopped” also. I’ve tried to copy my sketch to this board for advice but I cannot figure out how to get the format correct. I searched all over and do not see the best way to copy and paste it - are there any instructions available to due this? I’d like to copy from Arduino 1.6.12 to this board.

Add three backticks ` before and after your code. That’ll format the code very pretty here :slight_smile:

For full formatting:

three back ticks then cpp
paste code starting on line below.
on the line after the code three back ticks

@Pavlo has posted a nice video of the process on several occassions

Okay I hope this works… What I’m trying to do is add messages to the LCD when the door is moving and if it has stopped (jammed) before fully opening or closing. I understand the current and previous statement and how it works to stop a flood warning. But I do not know how to make it use more than one D-pin to compare. In the PLC world I’d create a bit for each switch (EI: open=0, Closed=1, moving=2, stopped=3) then move that bit to a compare statement. In CPP I’m lost…

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "MySecretAuthCode";

WidgetLCD lcd(V1);


void setup()
{

  Serial.begin(9600);
  Blynk.begin(auth);

  pinMode(22, INPUT); // Garage door is open (O:1/0 from PLC "Open switch is high")
  pinMode(23, INPUT); // Garage Door is closed (O:1/1 from PLC "Closed switch is high")
  pinMode(24, INPUT); // Garage door is moving (O:1/2 from PLC "Both open and closed switches are low for 20 seconds or less")
  // Note: D-Pins 22, 23, & 24 all low = Garage Door is Jammed ("Both open and closed switches are low for more than 20 seconds")

 
 
  while (Blynk.connect() == false) {  // Wait until connected
  }
}

bool current = 0; //Logical placeholder - current status of garage door
bool previous = 1; //Logical placeholder - previous status of garage door  

void doorClosed() 
{
  lcd.clear ();
  lcd.print(1,0, "The Garage Door");
  lcd.print(3,1, "is closed.");
}

void doorOpen()
{ 
  lcd.clear ();
  lcd.print(1,0, "The Garage Door");
  lcd.print(4,1, "is Open!");
}

void doorMoving()
{
  lcd.clear ();
  lcd.print(1,0, "The Garage Door");
  lcd.print(2,1, "is Moving!");
}

void doorStopped ()
{
  lcd.clear ();
  lcd.print(1,0, "The Garage Door");
  lcd.print(2,1, "has Stopped!");
}

void garageDoorSensor()
{
  current = digitalRead(23); // Status of the Garage door closed switch
  if (current != previous){ // Only run if there is a status change from previous state
    previous = current;  // reinitialize
    if (current == HIGH)
    {
      doorClosed();
    }
    else
    {
      doorOpen();
    }
    }
  }

void loop ()
{  
    
    Blynk.run();
    garageDoorSensor();
}

Got the paste to work!

You need SimpleTimer for this, Arduino Playground - HomePage and it is required for almost all Blynk sketches so Blynk include the library as one of the 5 libraries you downloaded when you began Blynking.

Actually the timing is all set up in my PLC. I just used the remarks to explain what makes that D-Pin high. Sorry I didn’t mean for those statements to be confusing. So what I need is if a Pin 22 goes high the LCD displays “The Garage Door is Open!”, if Pin 23 goes high the LCD displays “The Garage Door is Closed.”, if pin 24 goes high the LCD displays, “The Garage Door is moving!”, and if non of the pins are high (22, 23, & 24 are low) the LCD displays, “The Garaged Door has Stopped!”. I believe I have the code correct to display each of those statements as the way it is written right now the Blynk app currently displays The garage door is open when it is actually open and The garage door is closed when it is actually closed.

The part of code that I’d like to change is here

void garageDoorSensor()
{
current = digitalRead(23); // Status of the Garage door closed switch
if (current != previous){ // Only run if there is a status change from previous state
previous = current; // reinitialize
if (current == HIGH)
{
doorClosed();
}
else
{
doorOpen();
}
}
}

How do I get it to look for a change between the current and previous condition between any of the pins 22, 23, & 24. Also then lcd print the correct message for which pin has changed - 22 (door open), 23 (door closed), 24 (door moving), or all pins low (door stopped).

Thanks for helping with this. I’m really trying to learn this language and all of these examples are very informative. I just picked up a C++ for dummies book I’ve got some reading to do.

Without looking in detail at your code I still think it requires SimpleTimer.

Read x now and again in y seconds, if high / low do z etc.

Does that sound about right?

So what you’re saying is not to use a compare but a simple timer instead?

From what you have said about a door jam / moving then yes SimpleTimer. If it is still moving x seconds have you last checked the pin status then the door has jammed.

My Allen Bradley PLC processes the door status using a single proxy sensor on my garage door drive chain. It sends a high signal to one of three inputs on the Arduino - 22 the door is closed, 23 the door is open, 24 - the door is moving, and if all three are low then the door has stopped. I do not need the Arduino to process that information I just want it to display the message that is associated with each pin (or lack off all three).

Ok I have looked at your code and the problem I see is as follows:

garageDoorSensor(); not advisable to use this in loop() even though you sensibly check for a pin status change within the function. Just call the same function with SimpleTimer as short intervals if you think it is important, so perhaps 10, 50 or 100ms.

Are you familiar with else if?

Would something like this work for you with some tweaks based on previous / current readings and at timed intervals:

  if(digitalRead(22)){
    lcd.print(0, 0, "Door closed");
  }
  else if(digitalRead(23)){
    lcd.print(0, 0, "Door open");  
  }
  else if(digitalRead(24)){
    lcd.print(0, 0, "Door moving");  
  }
  else{
      lcd.print(0, 0, "Door stopped");
  }

I did not know about “Else if” that does look like it will do what I need. Thanks for the information about “else if” and the link for SimpleTimer. I will play with the code again tonight and see what I come up with. I’m also going to incorporate a second proxy on my door. I think I can make my PLC register which way the garage door is actually moving. So depending on how it works I may remove the “door is moving message” and replace it with “door is opening” & “door is closing”. I realize this is getting way more complicated than it needs to be for a simple garage door opener but it will have a large “Cool” factor when its completed. My ultimate goal is to have a system to open and close the garage door without human intervention. Basically when I drive away on my Harley the door will close and when I return it will open automagically. I believe I can achieve this by combining a app called Tasker with Blynk.

Again thanks for the direction, it is greatly appreciated!

1 Like

recent changes to Blynk mean you shouldn’t need Tasker i.e. Blynk now has a GPS widget.