I have what seems like a strange problem. My NODEMCU is connected to switches that close to GND when activated. I am using internal pullups on D1 & D2. Both switches are open. The voltage on D1 and D2 measures 3.14V. When I test these pins they both show LOW. Relevant code below.
const int Open_Lim_Pin = D1; //D1 GPIO5 Low when door 102% open
const int Closed_Lim_Pin = D2; //D2 GPIO4 low when door closed
void setup() {
pinMode(Step_Pin, OUTPUT); // Sets the three pins as Outputs, two as inputs
pinMode(Dir_Pin, OUTPUT);
pinMode(Solenoid_Pin, OUTPUT);
pinMode(Open_Lim_Pin, INPUT_PULLUP);
pinMode(Closed_Lim_Pin, INPUT_PULLUP);
......
if (Closed_Lim_Pin == HIGH) {
Serial.println ("Closed limit pin = HIGH");
} else {
Serial.println ("Closed limit pin = LOW");
}
if (Open_Lim_Pin == HIGH) {
Serial.println ("Open limit pin = HIGH");
} else {
Serial.println ("Open limit pin = LOW");
}
The serial monitor prints the low result in both cases.
I must be missing something here.
Thank you. You and a few other folks pointed out that I needed to use digitalRead() to get the value. That solved my problem. I did not realize this was an int variable rather than boolean. I did not post the entire code since it is quite lengthy, and those who correctly pointed out the code would not compile were correct.
Open_Lim_Pin = digitalRead(Open_Lim_Pin);
which would mean you assign either
Open_Lim_Pin = 1; // if IO-pin is HIGH
or
Open_Lim_Pin = 0; // if IO-pin is low
and the next time the digitalRead i no longer a
digitalRead(4);
but
a digitalRead(1)
or
a digitalRead(0)
which both read the wrong IO-pin
The difference between your wrong code and the one that works is
if (Closed_Lim_Pin == HIGH) {
which is
if (4 == HIGH)
and
if (digitalRead(Closed_Lim_Pin) == HIGH) {
which is
if (digitalRead(4) == 1) {
where the result of digitalRead(4) can be either 1 or 0
which boils down to
if (1 == 1) { // ==> condition true
or
if (0 == 1) { // ==> condition false
no booleans as values they are 1 or 0
but boolean expressions
1 == 1
0 == 1