I am trying to do something I thought would be simple.... All I want to do is register a change (High OR low, doesn't matter to me) on the press of external button.
I was able to get an IFTTT event working on startup, but this approach required resetting the board, and reconnecting to Wifi every time, which took too long. So all I want to do is run it with an IF statement in my loop instead.
After I could not get a button to work, I tested my sanity (and my code) by chucking an analogue pot on there and said if below x value, then do a thing, else don't do the thing - and Yep, this was fine.... But with the button on a digital pin.... I just cant seem to get any setup to work!
I have been through countless forum posts, I have tried every pin, I tried pullup with no resister approach, the 10k ohm resister approach, different buttons... I just cant NOT do this simple thing...
I am using Lolin Node Mcu v3 (but will need to do this on a D1 mini when it arrives in the mail)
I want to run the board from USB power
Happy to go with a resister if needed, or not...
Can someone please help me with this most simple of wiring set ups? Because apparently, I'm stupid!
My apologies, I have tried so many different approaches.. I didn't know what to post... but this is the most recent attempt...
int pin = 0;
void setup()
{
pinMode(pin, INPUT);
digitalWrite(pin, HIGH);
Serial.begin(9600);
}
int buttonStatus = 0;
void loop() //if deep sleep is working, this code will never run.
{
// print out the value
Serial.println(digitalRead(pin));
delay(500);
}
Generally don't use pins 0 & 1... they are used for Serial.
EDIT. Ignore that I see you are using "Lolin Node Mcu v3"... but GPIO may still have another special use on that uC.
Better to use INPUT_PULLUP then look for LOW when the button is pressed. Then you don't need an external resistor.
const int buttonPin = 5; // the number of the pushbutton pin
const int ledPin = 2; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
Serial.println("high");
// turn LED on:
digitalWrite(ledPin, HIGH);
delay(500);
} else {
// turn LED off:
Serial.println("low");
digitalWrite(ledPin, LOW);
delay(500);
}
}
So, I am now going Pin 5 switch and switch to GND
with this code:
const int buttonPin = 5; // the number of the pushbutton pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT_PULLUP);
digitalWrite(buttonPin, HIGH);
Serial.begin(9600);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
Serial.println("high");
delay(500);
} else {
Serial.println("low");
delay(500);
}
}
And the serial output is always High...
I was able to confirm with my multimeter that on a particular setup, the 2 pins on the same side showed 0 when pressed but greater than 0 when not pressed...
I did also try, as per previous setup, bypassing the switch and connecting blue to orange directly... and the serial out, still does not change.
Im sure the board is OK, as I have done other tutorials successfully. I must be doing something wrong still....