A simple digital read on ESP32

Hi all
I'm trying to have a Soft_AP server start up on different modes based on the initial switch condition.
And I have boiled my code down to this one simple concept which I just cant wrap my head around .
The AnalogToDigital Example in arduino IDE ... Working as expected :

int pushButton = 12;
int buttonState = 5;// or any random number 

void setup() {
  Serial.begin(9600); 
  pinMode(pushButton, INPUT);
  }

void loop() {
  buttonState = digitalRead(pushButton); 
  Serial.println(buttonState);//returns correct number based on switch 
  }

But if I just move the digitalRead command into the initial setup fuction :

int pushButton = 12;
int buttonState = 5;

void setup() {  
  Serial.begin(9600); 
  pinMode(pushButton, INPUT);
  buttonState = digitalRead(pushButton);
  Serial.println(buttonState);// Always prints 0 regardless
}

void loop() {}

It wont even let me save it into another global variable and carry it for later functions.
why is it forbidding me to do a digital read in setup ?
Thank you.

Welcome to the forum

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

Have you tried putting a delay() in setup() before you do the digitalRead() ?

How is the input wired ?
Consider using INPUT_PULLUP in pinMode() to turn on the built in pullup resistor

The software pullup didnt help, but changing from

Serial.begin(9600); 
delay(1);
pinMode(pushButton, INPUT);
buttonState = digitalRead(pushButton);

to

Serial.begin(9600); 
pinMode(pushButton, INPUT);
delay(1);
buttonState = digitalRead(pushButton);

Worked like a charm.
P.S I cant delete my own full post/thread here , can I ?

Why would you want to ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.