PWRBTN is defined as output: you cannot run a digitalRead() function on that pin.
If that's true:
1. Why does it not cause a compile error?
2. Why does "pwrswitch = digitalRead(PWRBTN);" return the correct current state of the pin to "pwrswitch" as seen in the serial monitor window?
The problem of not seeing a change of state with the multimeter or LED was there
before I added the two digitalRead commands and Serial.println commands to confirm that the "digitalWrite(PWRBTN, LOW);" and "digitalWrite(PWRBTN, HIGH);" commands to press and release the button were changing the state of the pin.
- Once we figure out how to get pin 3 configured and working right, it will be easy to cut out the two "pwrswitch = digitalRead(PWRBTN);" lines and two "Serial.println(pwrswitch);" lines.
You could also use digitalRead() to monitor the presence of 5V (or better: the presence of a voltage higher than 2.5V circa) instead of analogRead() being that you have two states (ground or "voltage").
The analog part of the code works perfectly and it allows for more flexibility.
One nice thing about analogRead is that it allows you to set a threshold in case the LED pin voltage is floating when off.
I don't know for sure if the anode of the LED is always positive with the ground being switched (like an arcade LED controller)
or if the ground is always connected and the LED operating voltage is being switched.
The current code works for a switched operating voltage circuit.
- I wired a switch between 5v and Arduino analog pin A0 for testing.
Change the threshold value to 200 (?) and change the if comparison sign from ">" to "<" for a switched ground setup.
Scott