#include <Arduino.h>
#define ENABLE_DEBUG
#ifdef ENABLE_DEBUG
#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG
#endif
enum LedStatus
{
LedOff = 0,
LedOn = 1
};
enum LedStatus curentLedStatus = LedOff;
#define BAUD_RATE 9600
// Pins of push buttons and led
#define pinPButtonLedOn 16
#define pinPButtonLedOff 5
#define pinLed 4
void setup()
{
Serial.begin(BAUD_RATE);
// put your setup code here, to run once:
pinMode(pinPButtonLedOn, INPUT_PULLUP); // set arduino pin to input pull-up mode
pinMode(pinPButtonLedOff, INPUT_PULLUP); // set arduino pin to input pull-up mode
pinMode(pinLed, OUTPUT); // set arduino pin to output mode
}
void loop()
{
// put your main code here, to run repeatedly:
Serial.printf("\n \n \n On button state :");
Serial.print(digitalRead(pinPButtonLedOn));
Serial.printf("\n \n \n Off button state :");
Serial.print(digitalRead(pinPButtonLedOff));
if (digitalRead(pinPButtonLedOn) == LOW && curentLedStatus == LedOff)
{
digitalWrite(pinLed, HIGH);
curentLedStatus = LedOn;
Serial.printf("\n \n \n Led turned On by Push Button");
}
if (digitalRead(pinPButtonLedOff) == LOW && curentLedStatus == LedOn)
{
digitalWrite(pinLed, LOW);
curentLedStatus = LedOff;
Serial.printf("\n \n \n Led turned Off by Push Button");
}
}
if ( (digitalRead(pinPButtonLedOn) == LOW) && (curentLedStatus == LedOff) )
enum LedStatus curentLedStatus = LedOff;
int LedStatus curentLedStatus = LedOff;
Hi,
try:
Code:if ( (digitalRead(pinPButtonLedOn) == LOW) && (curentLedStatus == LedOff) )
change the other "IF..." line the same way.
Klaus
***
added:
also check if this is correct:
Code:enum LedStatus curentLedStatus = LedOff;
maybe change it to:
Code:int LedStatus curentLedStatus = LedOff;
// put your main code here, to run repeatedly:
Serial.printf("\n \n \n On button state :");
Serial.print(digitalRead(pinPButtonLedOn));
Serial.printf("\n \n \n Off button state :");
Serial.print(digitalRead(pinPButtonLedOff));
#define pinPButtonLedOn 16
#define pinPButtonLedOff 5
#define pinLed 4
#define pinPButtonLedOn D0
#define pinPButtonLedOff D1
#define pinLed D2
No, that does not make any differenceYou are seemingly assuming that can use GPIO indexes to refer to I/O's, which is not allways true in the Arduino world.
There is an intrinsic mapping, so instead of using this...
Code:#define pinPButtonLedOn 16 #define pinPButtonLedOff 5 #define pinLed 4
You should consider using that
Code:#define pinPButtonLedOn D0 #define pinPButtonLedOff D1 #define pinLed D2
You are right, thanks a lot. I am new here, this is my first project of NodeMCUPersonally, I've stopped referring to the different types of board and use the 'generic' description instead. Partly that's because after using something like a NodeMCU for development, the final product only uses the actual ESP module without the rest of the board.
Aside from the pin number/name issue and the LED and enumerating strangeness, I can't see why there is any need to record the present LED state when it never changes unless a button is pressed. Simply 'if on is pressed turn it on' and 'if off is pressed turn it off' should work equally well. Saving the state is only necessary if it is being toggled with a single switch and the previously recorded condition has to be changed.
Brian.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?