Main > Raspberry Pi & Dev Board
Arduino code
PL1:
Thanks for the corrected time values, lilshawn. :cheers:
Took a while, but here's my cleaned-up version. ;D
- Fully commented.
- Excess code removed.
- Initialize the power button pin as an INPUT during setup.
- Removed the power button digitalRead commands because they didn't clearly show a difference between OUTPUT mode (logic LOW) and INPUT mode. (floating logic level that usually reads as LOW on my test setup)
- Added a countdown timer (optional) so you have time to open the Serial Monitor for troubleshooting before the board initializes.
- Added delays (optional) so your multimeter has time to stabilize if you need it for troubleshooting.
--- 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
// When pin 2 mode is set to OUTPUT, the default logic level is LOW
// LOW = button pressed
// When pin 2 mode is set to INPUT, the logic level is left floating
// Floating logic level (not HIGH or LOW) = button not pressed
// This prevents a problem with the computer's power button input and the internal Arduino resistor in OUTPUT mode
// If you want to slow the program down to verify what's happening with your multimeter, remove the "//" (comment) in front of the delay and Serial.println commands on lines 32-42, 74, and 82
// Comment those lines out when you are done testing so the program will run at normal speed
// Define variables
int id = 10000; // Initial delay period -- how long to wait before starting the main program loop (in miliseconds)
int cd = 3000; // 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
// End define variables
void setup() { // Start setup -- this section only runs once at start-up
////////////////////////////////////////// Countdown delay so you have time to open the Serial Monitor for testing
// Serial.println("25 seconds until initialization . . .");
// delay(5000);
// Serial.println("20 seconds until initialization . . .");
// delay(5000);
// Serial.println("15 seconds until initialization . . .");
// delay(5000);
// Serial.println("10 seconds until initialization . . .");
// delay(5000);
// Serial.println("5 seconds until initialization . . .");
// delay(5000);
// Serial.println("Initialize");
////////////////////////////////////////// End countdown delay for testing
pinMode(PWRBTN, INPUT); // Set the power button pin as an input which leaves the pin floating = power button not pressed
delay(id); // Initial delay before starting main power loop
} // End setup
void loop() { // Main program 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 steps 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
pinMode(PWRBTN, OUTPUT); // Press the power button, setting pin to OUTPUT ==> default logic level LOW
Serial.println("Press power button"); // Print press power button
delay(pd); // Hold the power button for "pd" miliseconds
////////////////////////////////////////// Delay for testing
// delay(6000); // Delay 6000 miliseconds for multimeter to stabilize during testing
////////////////////////////////////////// End delay for testing
pinMode(PWRBTN, INPUT); // Release the power button, setting the pin to INPUT leaves it floating (not HIGH or LOW) = not pressed
Serial.println("Release power button"); // Print release power button
////////////////////////////////////////// Delay for testing
// delay(6000); // Delay 6000 miliseconds for multimeter to stabilize during testing
////////////////////////////////////////// End delay for testing
} // End power LED is LOW (off) steps
} // End main program loop
--- End code ---
Are there any other changes or features you would like added?
LMK if there are any mistakes or unclear explainations in the comments.
Scott
lilshawn:
--- Quote from: PL1 on November 11, 2022, 03:10:05 am ---Are there any other changes or features you would like added?
LMK if there are any mistakes or unclear explainations in the comments.
Scott
--- End quote ---
no, actually everything worked out perfect. the timings i adjusted where to ensure the board had
1: indeed received power.
2: indeed done the initial power on-off.
3: indeed did not power on of it own accord.
ive tested the now dubbed "DEFIB board" in the machine about 60 times since implementation, having it both fail powering on (and the Arduino kicking it on) and succeed properly in powering on (requiring the arduino to do... well, nothing) and it has so far been working flawlessly.
lilshawn:
for prosperity's sake...
--- 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 = 10000; // Initial delay period -- how long to wait before starting the main program loop (in miliseconds)
int cd = 3000; // 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
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);
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 ---
Navigation
[0] Message Index
[*] Previous page
Go to full version