Main > Raspberry Pi & Dev Board

Arduino code

<< < (3/6) > >>

Zebidee:
If you really wanted to avoid using an arduino, you could try combining a resistor with your capacitor to extend the time delay (the capacitor takes longer to "get filled").

For example, at 5v a 10k resistor with a 100uF capacitor gives you a "tau" of 10 seconds (until capacitor is 63% charged).

Being more practical, use a pot (maybe 10k/20k/50k) in series with your capacitor (100uF is just a suggestion) to tweak the time delay to suit your needs. Then, once tweaked and if desired, you can replace with a fixed resistor.

Search on "RC delay" + circuit or calculator

lilshawn:

--- Quote from: nitrogen_widget on October 25, 2022, 11:14:14 am ---
oh, it's one of those boards.

maybe this will help?

https://forum.arduino.cc/t/turn-on-computers-after-power-outage/528546/4


--- End quote ---

yeah initially that's what i did... delay 10 seconds, "switch" the line to power on the computer. done.

problem was, sometimes the motherboard would just decide to on AC loss... power on... and my arduino would then shut the computer OFF  :banghead:

this is why i'm looking at timing out, then checking the power to see if its on or not.. before we "push the button"


--- Quote from: Zebidee on October 25, 2022, 12:12:52 pm ---If you really wanted to avoid using an arduino, you could try combining a resistor with your capacitor to extend the time delay (the capacitor takes longer to "get filled").

For example, at 5v a 10k resistor with a 100uF capacitor gives you a "tau" of 10 seconds (until capacitor is 63% charged).

Being more practical, use a pot (maybe 10k/20k/50k) in series with your capacitor (100uF is just a suggestion) to tweak the time delay to suit your needs. Then, once tweaked and if desired, you can replace with a fixed resistor.

Search on "RC delay" + circuit or calculator

--- End quote ---

interesting. maybe i'll give this a try too as well.

PL1:
If Zebidee's RC mod doesn't work for you, we might be able to re-purpose the code from this post.

If I understand correctly, your application only needs one input (the computer power LED pin, on = HIGH) and one output (the computer power button pin, pressed = LOW) so it wasn't too hard to adapt that stepper code to this rough state.
- I haven't had time to load and test this code yet.


--- Code: ---// Computer power check and switch press for always-on.

// Adapted from https://dronebotworkshop.com/stepper-motor-hall-effect/

// Define connections
#define PWRPIN        2   // Arduino pin that senses power LED
#define PWRBTN       8   // Arduino pin that connects to power button
 
// Variables
int pd = 500;                       // Pulse duration period -- how long to press the button (in miliseconds)
int cd = 2000;                     // Check delay period -- how long to wait between power checks (in miliseconds)
boolean pwrswitch = HIGH; // Initialize power switch condition -- HIGH = not pressed, LOW = pressed
 
// Interrupt Handler

void pwrmon (){ // This section defines what happens when the power monitor interrupt is triggered.
 
// Press the power button
pwrswitch = LOW;

// Print button press and carriage return
Serial.println("Press power button");

// Hold the power button for "pd" miliseconds.
delay(pd);
 
// Release the power button
pwrswitch = HIGH;

// Print button release and carriage return
Serial.println("Release power button");

}
 
 
void setup() {
 
  // Setup the stepper controller pins as Output
  pinMode(PWRBTN,OUTPUT);
 
  // Setup the power monitor pin as Input
  pinMode(PWRPIN, INPUT);
 
  // Attach interrupt pin to handler
  attachInterrupt(digitalPinToInterrupt(PWRPIN), pwrmon, FALLING);
   
}
 
// This main program loop is what is done until the interrupt is triggered.
void loop() {

// Delay between power checks.
delay(cd);
 
// Print power OK and carriage return
Serial.println("Power OK ");
 
}

--- End code ---

- Not sure if the interrupt code will work as desired with the LED pin (response is triggered by a HIGH-to-LOW falling change of state) or if the main program code loop should do a digital read of the LED pin's state to a variable like "pwrstate" and react using an "if" statement like this instead of using an interrupt.

--- Code: ---if(pwrstate == 0) {
// Press the power button
pwrswitch = LOW;

// Print button press and carriage return
Serial.println("Press power button");

// Hold the power button for "pd" miliseconds.
delay(pd);
 
// Release the power button
pwrswitch = HIGH;

// Print button release and carriage return
Serial.println("Release power button");

}
--- End code ---
- Not sure if all of the logic levels and times are right.
- The good news is that you should be able to troubleshoot the code and verify operation by opening a serial window to monitor the printed output messages.


Scott

PL1:
Updated code using baritonomarchetto's suggestion of analogRead.
- The analogRead function is working perfectly for determining if 5v is present on analog pin A0.
- Setting the power threshold variable to 819 means that a voltage of 4v or greater = ON.  (819/1024)*5v = 4v

The output of the serial monitor window shows the power state followed by the raw power reading after each check.
- If power is OFF, it also shows button press, pin state low (0), button release, and pin state high (1).

--- Quote ---Power ON
1023
Power ON
1023
Power OFF
812
Press power button 0
Release power button 1
Power OFF
442
Press power button 0
Release power button 1
Power OFF
288
Press power button 0
Release power button 1
Power ON
1023

--- End quote ---

The only remaining problem is that I'm not seeing the expected voltage change with a multimeter between high and low on the designated power button switch pin (Arduino pin 3) when the sketch "presses and releases" the button.
- Also tried to use an LED to view the pin state like I've done before  (pin high = LED lit), but no luck.
- This could just be a bad connection with the multimeter probes or LED.   :dunno


--- 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 3


// Define variables
int pd = 500;             // Pulse duration period -- how long to press the button (in miliseconds)
int cd = 2000;            // Check delay period -- how long to wait between power checks (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 = 3;           // Power button on pin 3
boolean pwrswitch = HIGH; // Initialize power switch condition -- HIGH = not pressed, LOW = pressed


// Define setup
void setup() {
pinMode(PWRBTN, OUTPUT); // Setup the power button pin as an output
}


// 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

baritonomarchetto:
PWRBTN is defined as output: you cannot run a digitalRead() function on that pin.
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").

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version