• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

ESP32 programming in Arduino IDE

Q&A Forum › Category: ESP32 › ESP32 programming in Arduino IDE
0 Vote Up Vote Down
Shailesh Dakhara asked 4 years ago

When Door Closed, there are following two serial print happen at the same time
Door Open
Door Close
This means I want to when I open the door then the serial print door open & door close then the serial print door close only 1 time (loop should be closed when one event (Door Open or Door Close) Done.

Arduino Code

const int SensorInput = 2;
byte lastSensorState = LOW;

void setup() {
Serial.begin(115200);
pinMode(SensorInput, INPUT);
}

void loop() {

byte SensorState = digitalRead(SensorInput);

if (SensorState != lastSensorState)
{
lastSensorState = SensorState;
Serial.println(“Door Open”);

if (SensorState == LOW)

{
Serial.println(“Door Close”);

}
}
}

https://imgur.com/a/CrreXpr

7 Answers
0 Vote Up Vote Down
Best Answer
Steve Mercer answered 4 years ago

It’s not a false reading. Your code is doing exactly what you told it to do. You need to work though your code to see what happens.

If we start at the beginning you set lastSensorState to LOW. Now, through the loop. Let’s start with SensorState HIGH. the first if statement is true so it prints “Door Open” and sets lastSensorState to HIGH. SensorState is HIGH so the second if statement is false and we return to the beginning of the loop. Nothing happens until SensorState goes LOW. Then the first If statement is now true. (lastSensorState == HIGH and SensorState == LOW). So, it again prints “Door Open” and sets lastSensorState to LOW. The second if statement is now true (SensorState == LOW) so it prints “Door Close”. This will repeat over and over as you see.

You’ll need to fix your logic. What you want to do is to add another if statement inside the beginning if statement. So, if SensorState == LOW it prints “Door Close” OR if SensorState == HIGH it prints “Door Open”.

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Hi.
You need to add a debounce code to prevent false readings.
Do you know how to do that?
 
There’s a basic example on the Arduino IDE. Go to File > Examples > Digital > Debounce.
 
Additionally, you can use debounce + interrupts. We have the following example with the ESP32:

  • https://randomnerdtutorials.com/esp32-door-status-telegram/

It sends notifications when the door sensor changes state, but you can ignore the sections that use Telegram.
Here’s the code:

  • https://raw.githubusercontent.com/RuiSantosdotme/Random-Nerd-Tutorials/master/Projects/ESP32/ESP32_Telegram_Door.ino

Let me know if you need further help.
 
Regards,
Sara
 
 
 

0 Vote Up Vote Down
Shailesh Dakhara answered 4 years ago

Your guidance is so helpful.
Your support is gratifying. Thank you for your support

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Great!
Then, let me know if you need anything else.
Regards,
Sara

0 Vote Up Vote Down
Shailesh Dakhara answered 4 years ago

As per your Suggestion, My Application is working…….
Thank you so much, Steve

0 Vote Up Vote Down
Shailesh Dakhara answered 4 years ago

Now my query has been resolved,

Thanks, Sara & Steve

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Great!
If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.