Main > Raspberry Pi & Dev Board
Arduino code
PL1:
--- Quote from: baritonomarchetto on October 27, 2022, 01:15:31 am ---PWRBTN is defined as output: you cannot run a digitalRead() function on that pin.
--- End quote ---
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.
--- Quote from: baritonomarchetto on October 27, 2022, 01:15:31 am ---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").
--- End quote ---
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. :dunno
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
lilshawn:
thanks guys, this has been really helpful.
i'll probably play around a bit more with this this afternoon.
PL1:
Figured it out. :lol
Helps when your multimeter and LED are connected to the pin you defined in the code instead of the one next to it. :embarassed: :banghead:
Here's an update for the code that should work for you. Adjust the time variables as desired.
- Connect the LED power to Arduino pin A0.
- Connect the computer power button to Arduino pin 2.
--- Code: ---// Computer power check and switch press for always-on.
// Connect the LED power pin to pin A0
// Connect the PC power button to pin 2
// Define variables
int id = 3000; // Initial delay period -- how long to wait before starting the main program loop (in miliseconds)
int cd = 2000; // Check delay period -- how long to wait between power checks (in miliseconds)
int pd = 500; // Pulse duration period -- how long to press the button (in miliseconds)
int PWRPIN = 0; // Power pin on pin A0
int pwrstate = 1024; // Initialize power state condition
int pwrthreshold = 819; // Power on threshold (range is 0-1024) -- greater than this value = power LED on
int PWRBTN = 2; // Power button on pin 2
boolean pwrswitch = HIGH; // Initialize power switch condition -- HIGH = not pressed, LOW = pressed
// Define setup
void setup() {
pinMode(PWRBTN, OUTPUT); // Set the power button pin as an output
digitalWrite(PWRBTN, HIGH); // Initialize the power button pin as not pressed
delay(id); // Initial delay before starting main power loop
}
// Main program loop
void loop() {
delay(cd); // Delay between power checks
pwrstate = analogRead(PWRPIN); // Read the analog input pin
if(pwrstate > pwrthreshold) { // Do the following if the power LED voltage is >(power threshold/1024)*5v (on)
Serial.println("Power ON "); // Print power ON and carriage return
Serial.println(pwrstate); // Print pwrstate and carriage return
} else { // Do the following if the power LED is LOW (off)
Serial.println("Power OFF "); // Print power OFF and carriage return
Serial.println(pwrstate); // Print pwrstate and carriage return
digitalWrite(PWRBTN, LOW); // Press the power button
Serial.print("Press power button "); // Print press power button
pwrswitch = digitalRead(PWRBTN); // Read the power button pin state
Serial.println(pwrswitch); // Print power switch state and carriage return
delay(pd); // Hold the power button for "pd" miliseconds
digitalWrite(PWRBTN, HIGH); // Release the power button
Serial.print("Release power button "); // Print release power button
pwrswitch = digitalRead(PWRBTN); // Read the power button pin state
Serial.println(pwrswitch); // Print power switch state and carriage return
}
} // End main program loop
--- End code ---
Scott
lilshawn:
thanks for the help guys. the timings needed a little tweak and i had to change the pin 2 code to basically switch the pin from an input to output. (due to the outputs being tied with a resistor when switched and not left floating... the switch doesn't trigger properly. NBD though.. too bad the code didn't have a digitalWrite(2, FLOAT); command to leave an output floating.)
--- Code: ---// digitalWrite(PWRBTN, LOW); // Press the power button
pinMode(PWRBTN, OUTPUT);
Serial.print("Press power button "); // Print press power button
pwrswitch = digitalRead(PWRBTN); // Read the power button pin state
Serial.println(pwrswitch); // Print power switch state and carriage return
delay(pd); // Hold the power button for "pd" miliseconds
//digitalWrite(PWRBTN, HIGH); // Release the power button
pinMode(PWRBTN, INPUT);
--- End code ---
PL1:
Glad you got it working. ;D
What timings worked for you?
Better yet, would you mind posting your tested and fully operational sketch for the next guy?
----------------------------------
I get that "pinMode(PWRBTN, INPUT);" leaves the power button pin floating.
Based on the code snippet you posted, what I can't wrap my head around is how/why "pinMode(PWRBTN, OUTPUT);" takes the pin to logic LOW (button pressed) without a digitalWrite. :dunno
- Did you change "boolean pwrswitch = HIGH;" to "boolean pwrswitch = LOW;" in the variables section?
- Did you change the setup loop where it sets the pinMode to OUTPUT then does a digitalWrite to set it HIGH?
- Is LOW the default state when you set pinMode to OUTPUT?
Scott
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version