Displaying reed switch status via LED widget

Still working on my first proper project and I’m stuck. Really sorry I wanted to figure this out for myself, I’ve spent 3 days going through this and I just can’t work out what I’m doing wrong, I’ve even spent 4hrs watching a C++ tutorial on Youtube which was very useful in what I’ve learned but hasn’t got me any further with this issue.

It’s a sketch to allow opening and of closing of garage doors, and that bit is working perfectly thanks to @PeteKnight, but now I’m trying to determine the open/close status of each door and display it via a virtual LED widget that changes colour. I’ve taken the Virtual LED example sketch and amalgamated it into my sketch, and added subroutines to determine the status and display it - except I haven’t. I’m obviously doing something fundamentally wrong because I can see from the serial monitor that these subroutines aren’t even being run. This is what I’ve got so far

 /* Comment this out to disable prints and save space */
 #define BLYNK_PRINT Serial
 #include <ESP8266WiFi.h>
 #include <BlynkSimpleEsp8266.h>
 
  
 char auth[] = "do";
 // Your WiFi credentials.
 char ssid[] = "Vodaf";
 char pass[] = "5il9v6";
 
 WidgetLED led1(V3);
 WidgetLED led2(V4);
 
 BlynkTimer timer;
 
 // constants won't change. They're used here to set pin numbers:
 const int door1 = 4; // D2
 const int door2 = 5; // D1
 const int door1Status = 13; // D7
 const int door2Status = 12; // D6
 
 #define BLYNK_GREEN     "#23C48E"
 #define BLYNK_RED       "#D3435C"
 
  
 void setup()
 {
   
  pinMode(door1, OUTPUT);
  pinMode(door2, OUTPUT);
  pinMode(door1Status, INPUT);
  pinMode(door2Status, INPUT);
  
 
  digitalWrite(door1,LOW); // ensure relays not activated on power-on
  digitalWrite(door2,LOW);
 
  
  Serial.begin(74880);
  
  led1.on(); // Turn LED widgets on 
  led2.on(); 
  timer.setInterval(1000L, LED1Widget); 
  timer.setInterval(1000L, LED2Widget);
  Blynk.begin(auth, ssid, pass);  //connects to the blynk server using the credentals from above.
 
  }
 
 // Door 1 LED Widget
 void LED1Widget()
 {
   if (door1Status) {
     led1.setColor(BLYNK_GREEN);
     Serial.println("LED on V3: green");
   } else {
     led1.setColor(BLYNK_RED);
     Serial.println("LED on V3: red");
   }
   
 }
 
 // Door 2 LED Widget
 void LED2Widget()
 {
   if (door2Status) {
     led2.setColor(BLYNK_GREEN);
     Serial.println("LED on V4: green");
   } else {
     led2.setColor(BLYNK_RED);
     Serial.println("LED on V4: red");
   }
 
 }
  
 BLYNK_WRITE(V1)
 {        
   
   if (param.asInt() == 1) // DOOR 1 MODULE
   {            
     digitalWrite(door1, HIGH); //Close the relay
     Serial.println("Relay 1 Activated");
     delay(500);
     digitalWrite(door1, LOW); //Release the Relay
     Serial.println("Relay 1 Deactivated");
     Serial.println("");
   }
   
 }
 
 BLYNK_WRITE(V2)
 {   
       
   if (param.asInt() == 1) // DOOR 2 MODULE
   {            
     digitalWrite(door2, HIGH); //Close the relay
     Serial.println("Relay 2 Activated");
     delay(500);
     digitalWrite(door2, LOW
     ); //Release the Relay
     Serial.println("Relay 2 Deactivated");
     Serial.println("");
   }
   
 }
 
 void loop() 
 {
   Blynk.run();  
 }

It makes life much more difficult for us if you don;t format your code correctly, with triple backticks at the beginning and end.

You have a variable that’s called door1Status which is assigned the value of 13 at startup, as this is the GPIO pin that your reed switch for door 1 is attached to.

You then set this as LOW in void setup (not sure about the logic of this).

What you should then be doing is reading the state of this pin 13 on a regular basi to find out if the door is opened or not. You should be doing this with a result1=digitalRead(13) or preferably result1=digitalRead(door1Status) type of command.

You aren’t doing this, and instead you’re saying:

which in C++ is same as testing if door1Status ==1 or door1Status == true.
As door1Status (which is the GPIO pin number, not it’s HIGH/LOW reading) is equal to 13, this statement will never evaluate as true - so it’s the ‘else’ option that is executed.

What you actually need to be doing is something like this:

 // Door 1 LED Widget
void LED1Widget()
{
  int result1 = digitalRead(door1Status); // Get the reed switch status
  if (result1) // if it's HIGH then do this...
  {
    led1.setColor(BLYNK_GREEN);
    Serial.println("LED on V3: green");
  }
  else // if it's LOW then do this instead....
  {
    led1.setColor(BLYNK_RED);
    Serial.println("LED on V3: red");
  }
}

It would help enormously if you gave more meaningful names to your variables.
door1Status should really be door1SensorPin, which would allow the variable that I’ve called result1 to be called door1Status - which is what it actually represents.

It would also be better to make the result1 variable global, by declaring it at the top of your code, but I’ve made it local in the example for simplicity.

When you’re struggling to debug code like this then simplify add-in some serial print statements. If you’d have done this with your original code:

// Door 1 LED Widget
void LED1Widget()
{
 
 Serial.print("door1Status = ");
 Serial.println(door1Status);

 if (door1Status) {
   led1.setColor(BLYNK_GREEN);
   Serial.println("LED on V3: green");
 } else {
   led1.setColor(BLYNK_RED);
   Serial.println("LED on V3: red");
 }
}

Then you would have immediately seen that the value of door1Status was 13, and you’d have understood your error.

Pete.

Sorry I couldn’t work out which button formatted the post correctly and had a few attempts before I grasped it.

Thanks, that what I couldn’t get my head around, I thought if I interrogated that pin it would give me a “true” if it was high and a “false” if it was low but I see what you mean now. I’ll take on board about the use of variables and keep watching C++ videos.

I haven’t set door1Status in setup to to low though, that’s door1 to stop the relays activating on power up. I take your point about the confusing use of variables, I should have called that one door1relay.

Thanks again for your help

Andy

Apologies, I misread that when I was searching through the code, but better variable naming would certainly help :slightly_smiling_face:

Does this amended code work?

Pete.

I haven’t had chance to rewrite it yet, I run a retail business and am currently being deluged with employee enquiries following Boris’s speech! I’m going to turn my phone off for the night soon and then I’ll have another crack.

Andy

It would have been so much better if he’d said “return to work in Wednesday” or even the following Monday.
Just glad that we both retired early so don’t have the hassles of managing business and staff in this chaos!

Pete.

I was so close, less than 2 yrs away from early retirement. Goodness knows what all this will do those plans though.

Ok this is the revised sketch, and although your suggestions make it much clearer for me to see what’s going on I’ve still got something wrong as I can see from the serial monitor that my LEDWidget subroutines are not being called at all. Still only the relay responses that are displayed.
Is it to do with how I’m using the timer? I wasn’t sure if that should all be in setup but it produced errors elsewhere.

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

 
char auth[] = "do";
// Your WiFi credentials.
char ssid[] = "Vodafone";
char pass[] = "5i";

WidgetLED led1(V3);
WidgetLED led2(V4);

BlynkTimer timer;

// constants won't change. They're used here to set pin numbers:
const int door1RelayPin = 4; // D2
const int door2RelayPin = 5; // D1
const int door1SensorPin = 13; // D7
const int door2SensorPin = 12; // D6
int door1Status;
int door2Status;

#define BLYNK_GREEN     "#23C48E"
#define BLYNK_RED       "#D3435C"

 
void setup()
{
  
 pinMode(door1RelayPin, OUTPUT);
 pinMode(door2RelayPin, OUTPUT);
 pinMode(door1SensorPin, INPUT);
 pinMode(door2SensorPin, INPUT);
 

 digitalWrite(door1RelayPin,LOW); // ensure relays not activated on power-on
 digitalWrite(door2RelayPin,LOW);

 
 Serial.begin(74880);
 
 led1.on(); // Turn LED widgets on 
 led2.on(); 
 timer.setInterval(1000L, LED1Widget); 
 timer.setInterval(1000L, LED2Widget);
 Blynk.begin(auth, ssid, pass);  //connects to the blynk server using the credentals from above.

 }


 // Door 1 LED Widget
void LED1Widget()
{
  door1Status = digitalRead(door1RelayPin); // Get the reed switch status
  if (door1Status) // if it's HIGH then do this...
  {
    Serial.println(door1Status);
    led1.setColor(BLYNK_GREEN);
    Serial.println("LED on V3: green");
  }
  else // if it's LOW then do this instead....
  {
    Serial.println(door1Status);
    led1.setColor(BLYNK_RED);
    Serial.println("LED on V3: red");
  }
}

// Door 2 LED Widget
void LED2Widget()
{
  door2Status = digitalRead(door2RelayPin); // Get the reed switch status
  if (door2Status) // if it's HIGH then do this...
  {
    Serial.println(door2Status);
    led2.setColor(BLYNK_GREEN);
    Serial.println("LED on V4: green");
  }
  else // if it's LOW then do this instead....
  {
    Serial.println(door2Status);
    led2.setColor(BLYNK_RED);
    Serial.println("LED on V4: red");
  }
}


 
BLYNK_WRITE(V1)
{        
  
  if (param.asInt() == 1) // DOOR 1 MODULE
  {            
    digitalWrite(door1RelayPin, HIGH); //Close the relay
    Serial.println("Relay 1 Activated");
    delay(500);
    digitalWrite(door1RelayPin, LOW); //Release the Relay
    Serial.println("Relay 1 Deactivated");
    Serial.println("");
  }
  
}

BLYNK_WRITE(V2)
{   
      
  if (param.asInt() == 1) // DOOR 2 MODULE
  {            
    digitalWrite(door2RelayPin, HIGH); //Close the relay
    Serial.println("Relay 2 Activated");
    delay(500);
    digitalWrite(door2RelayPin, LOW
    ); //Release the Relay
    Serial.println("Relay 2 Deactivated");
    Serial.println("");
  }
  
}

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

You’re missing timer.run(); from your void loop :rofl:

Pete.