Main Restorations Software Audio/Jukebox/MP3 Everything Else Buy/Sell/Trade
Project Announcements Monitor/Video GroovyMAME Merit/JVL Touchscreen Meet Up Retail Vendors
Driving & Racing Woodworking Software Support Forums Consoles Project Arcade Reviews
Automated Projects Artwork Frontend Support Forums Pinball Forum Discussion Old Boards
Raspberry Pi & Dev Board controls.dat Linux Miscellaneous Arcade Wiki Discussion Old Archives
Lightguns Arcade1Up Try the site in https mode Site News

Unread posts | New Replies | Recent posts | Rules | Chatroom | Wiki | File Repository | RSS | Submit news

  

Author Topic: Arduino code  (Read 4951 times)

0 Members and 1 Guest are viewing this topic.

lilshawn

  • Trade Count: (+3)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 7439
  • Last login:July 20, 2024, 11:44:22 am
  • I break stuff...then fix it...sometimes
Arduino code
« on: October 20, 2022, 02:41:02 pm »
I'm having issue with a machine's computer, whereby it is supposed to power on at startup, but despite the bios set to "on AC restore" being on, it sometimes does not. otherwise, the computer works just fine.

i would like to create some Arduino code that basically monitors the negative side power LED pin header for ground (IE a turned on power led) every say..10 seconds... and if it sees it's not "on"... then to turn on the computer.

i did create a program to just wait and then "power on" the computer by "pressing the button" which does work...except when the computer decides to actually work and power on... in which case...it helpfully turns off the computer at this time.

so, semantics aside, i would like to implement monitoring/check of the computer's power LED control line so if the computer IS on, it doesnt try and turn it off.

i'm old and tired and don't have much head to wrap around this...any help would be wonderful. i've kinda thrown some stuff together here... it's out of order and obviously would never work...i just sort of started typing ignore my comments, i'm a self-depreciating programmer... would eventually move things around to where they belong. i need a then do this somewhere... at least let me know if i'm on the right track anyway. I could do this in basic. please,thanks!
Code: [Select]
void setup() {
   int start = 2;
  int power = 3;
  int powerState = 0;
  int lastPowerState = 0;
  pinMode(start, INPUT);
  pinMode(power, INPUT);
 


void loop() {}
//CHECK THE POWER LED LINE TO SEE IF ITS ON OR NOT


//STARTING THE ---smurfette---
  pinMode (start, OUTPUT);
  delay(150);
  pinMode(start, INPUT);
  delay(150);
  //END STARTING THE ---smurfette---
   
 
}

powerState = digitalRead (power);

// compare the powerState to its previous state
  if (powerState != lastPowerState) {
    if (powerState == LOW) {
      // if the current state is LOW then the computer went on:
         
    } else {
     
    delay(50);
  }
  // save the current state as the last state, for next time through the loop do i need to actually do this or nah probably only need a one shot anyway so no need to loop idiot but if so then here we go
  lastPowerState = PowerState;



baritonomarchetto

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 815
  • Last login:Today at 02:50:05 am
Re: Arduino code
« Reply #1 on: October 21, 2022, 12:37:20 am »
So, will the Arduino always be powered by an external PSU H24? If yes, isn't this going to be a waste of energy? Wouldn't it be more convenient to wire a button to the power on the motherboard pinheader and press it in those rare cases the PC doesn't turn on by itself?

One approach for this could be monitoring for 5V at any unused PC power connector. Arduino should be powered by an external 5V source and in turn connected to the same switch of the PC. This way arduino is notnpowered H24 and is not going to continuously switch the PC on. An ATtiny85 micro should be more than enought for the task.
AnalogReadSerial example could be a good starting point
« Last Edit: October 23, 2022, 11:24:30 am by baritonomarchetto »

lilshawn

  • Trade Count: (+3)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 7439
  • Last login:July 20, 2024, 11:44:22 am
  • I break stuff...then fix it...sometimes
Re: Arduino code
« Reply #2 on: October 23, 2022, 04:46:09 pm »
yes it powered by the offending computer's USB port with the port set to be powered by +5SB, so it's always on if the power supply is plugged in and has AC voltage.

this computer's state will be 99% on. I need this for the random part of 1% that it decides not to come on.

this board is also an intel board so when it gets power it does that stupid little thing where it powers on for 3 seconds then turns off when it gets power so the old "capacitor on the power button header" thing doesn't work.

normally i wouldn't mess around with this, but the computer is locked in a cabinet...and if it powers off due to power interruption, I NEED it to come back on all the time. I can't be driving 6 hours to go press a power button.

nitrogen_widget

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 1746
  • Last login:March 30, 2024, 09:10:09 am
  • I want to build my own arcade controls!
Re: Arduino code
« Reply #3 on: October 23, 2022, 06:55:45 pm »
did you look for a bios upgrade?
might be a known problem with the board that is fixable.

lilshawn

  • Trade Count: (+3)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 7439
  • Last login:July 20, 2024, 11:44:22 am
  • I break stuff...then fix it...sometimes
Re: Arduino code
« Reply #4 on: October 23, 2022, 09:19:27 pm »
did you look for a bios upgrade?
might be a known problem with the board that is fixable.

yeah, i tried messaging the maker but did not get a response. they make embedded boards for crap and literally provide no support because their contract states they leave it to the OEM they made it for to provide "support" IE nothing... since the OEM didn't make the BIOS they have nothing.

I also have 12 other computers that operate correctly and they have the same firmware version. I even pulled a bios chip from one of the good computers, dumped the contents...and then wrote it to a brand new chip...and it still does not behave properly sometimes.

it's weird, because this is a common issue with this board (have had 3 machines do this now)... and company did not do anything about it except swap the boards for a different one. now those replacement boards are NLA (ebay puts them between 600 and 1200 ea USED) and the whole hardware kit has hit hard EOL and the alternative is stupid and involves about 3 grand and all new everything except the important user bits.

not to mention that means shoehorning a 16:9 image on a 19" 4:3 screen.  :banghead: yes, we like using a magnifying glass to view images on 5" of screen.

baritonomarchetto

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 815
  • Last login:Today at 02:50:05 am
Re: Arduino code
« Reply #5 on: October 24, 2022, 02:34:01 am »
When the "PC on on power loss" bios option was not available (old days) there was a method we used to authomatically power on a mamecab PC: a small capacitor (1uF IIRW) directly connected to the PC power on button contacts. This emulates a key press during the charging ramp.

Not sure if it's still a viable option with modern motherboards, but could be worth a try
« Last Edit: October 24, 2022, 02:36:35 am by baritonomarchetto »

Zebidee

  • Trade Count: (+9)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 3295
  • Last login:Today at 06:51:20 am
Re: Arduino code
« Reply #6 on: October 24, 2022, 04:28:46 am »
It is still an option, but there is no need with BIOS options.

I use a similar trick to get stubborn TVs to automatically come on when power is applied, but with a slightly larger cap 10-100uF) to allow the control circuit time to wake up.
Check out my completed projects!


lilshawn

  • Trade Count: (+3)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 7439
  • Last login:July 20, 2024, 11:44:22 am
  • I break stuff...then fix it...sometimes
Re: Arduino code
« Reply #7 on: October 24, 2022, 09:38:21 pm »
When the "PC on on power loss" bios option was not available (old days) there was a method we used to authomatically power on a mamecab PC: a small capacitor (1uF IIRW) directly connected to the PC power on button contacts. This emulates a key press during the charging ramp.

Not sure if it's still a viable option with modern motherboards, but could be worth a try

this is one of those fabulous intel boards that powers on for a few seconds to... i dunno... test for stuff??? then shuts off before you can boot it. so now the cap is no longer a "switch" cause it's charged.

it's stupid in that way as well.

so it powers on.... then your cap gets filled and then shuts off cause now its ready for your button press.


believe me, if i didnt have to program an arduino to do this, i wouldn't

baritonomarchetto

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 815
  • Last login:Today at 02:50:05 am
Re: Arduino code
« Reply #8 on: October 25, 2022, 12:34:37 am »
Ok, arduino then :)
Did you gave a look at the sketch I suggested as starting point?

nitrogen_widget

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 1746
  • Last login:March 30, 2024, 09:10:09 am
  • I want to build my own arcade controls!
Re: Arduino code
« Reply #9 on: October 25, 2022, 11:14:14 am »
When the "PC on on power loss" bios option was not available (old days) there was a method we used to authomatically power on a mamecab PC: a small capacitor (1uF IIRW) directly connected to the PC power on button contacts. This emulates a key press during the charging ramp.

Not sure if it's still a viable option with modern motherboards, but could be worth a try

this is one of those fabulous intel boards that powers on for a few seconds to... i dunno... test for stuff??? then shuts off before you can boot it. so now the cap is no longer a "switch" cause it's charged.

it's stupid in that way as well.

so it powers on.... then your cap gets filled and then shuts off cause now its ready for your button press.


believe me, if i didnt have to program an arduino to do this, i wouldn't

oh, it's one of those boards.

maybe this will help?

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

Zebidee

  • Trade Count: (+9)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 3295
  • Last login:Today at 06:51:20 am
Re: Arduino code
« Reply #10 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
« Last Edit: October 25, 2022, 12:15:13 pm by Zebidee »
Check out my completed projects!


lilshawn

  • Trade Count: (+3)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 7439
  • Last login:July 20, 2024, 11:44:22 am
  • I break stuff...then fix it...sometimes
Re: Arduino code
« Reply #11 on: October 25, 2022, 12:48:50 pm »

oh, it's one of those boards.

maybe this will help?

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


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"

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

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

PL1

  • Global Moderator
  • Trade Count: (+1)
  • Full Member
  • *****
  • Offline Offline
  • Posts: 9456
  • Last login:Today at 02:28:33 am
  • Designated spam hunter
Re: Arduino code
« Reply #12 on: October 25, 2022, 04:11:02 pm »
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: [Select]
// 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 ");
 
}

- 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: [Select]
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");

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

  • Global Moderator
  • Trade Count: (+1)
  • Full Member
  • *****
  • Offline Offline
  • Posts: 9456
  • Last login:Today at 02:28:33 am
  • Designated spam hunter
Re: Arduino code
« Reply #13 on: October 26, 2022, 05:02:41 pm »
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

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: [Select]
// 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


Scott
« Last Edit: October 26, 2022, 05:08:58 pm by PL1 »

baritonomarchetto

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 815
  • Last login:Today at 02:50:05 am
Re: Arduino code
« Reply #14 on: October 27, 2022, 01:15:31 am »
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").
« Last Edit: October 27, 2022, 01:18:45 am by baritonomarchetto »

PL1

  • Global Moderator
  • Trade Count: (+1)
  • Full Member
  • *****
  • Offline Offline
  • Posts: 9456
  • Last login:Today at 02:28:33 am
  • Designated spam hunter
Re: Arduino code
« Reply #15 on: October 27, 2022, 03:36:42 am »
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.   :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

  • Trade Count: (+3)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 7439
  • Last login:July 20, 2024, 11:44:22 am
  • I break stuff...then fix it...sometimes
Re: Arduino code
« Reply #16 on: October 27, 2022, 11:03:36 am »
thanks guys, this has been really helpful.

i'll probably play around a bit more with this this afternoon.

PL1

  • Global Moderator
  • Trade Count: (+1)
  • Full Member
  • *****
  • Offline Offline
  • Posts: 9456
  • Last login:Today at 02:28:33 am
  • Designated spam hunter
Re: Arduino code
« Reply #17 on: October 27, 2022, 01:23:24 pm »
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: [Select]
// 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


Scott

lilshawn

  • Trade Count: (+3)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 7439
  • Last login:July 20, 2024, 11:44:22 am
  • I break stuff...then fix it...sometimes
Re: Arduino code
« Reply #18 on: November 01, 2022, 10:54:21 am »
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: [Select]
// 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);

PL1

  • Global Moderator
  • Trade Count: (+1)
  • Full Member
  • *****
  • Offline Offline
  • Posts: 9456
  • Last login:Today at 02:28:33 am
  • Designated spam hunter
Re: Arduino code
« Reply #19 on: November 04, 2022, 05:19:01 am »
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

lilshawn

  • Trade Count: (+3)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 7439
  • Last login:July 20, 2024, 11:44:22 am
  • I break stuff...then fix it...sometimes
Re: Arduino code
« Reply #20 on: November 04, 2022, 09:29:38 pm »
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


as far as i can tell, when the pin is set as an output pinMode(##,OUTPUT); there is apparently an internal resistor that gets applied to the pin that forces the pin to a LOW state on initialization so that when you issue a digitalWrite there is a decisive logic level switch to tie it high or low. apparently there is a way to disable it, but i've never had it work.

so the main problem is... this resistor prevents us from using it to drive an input that already has some kind of biasing (such as the computer power switch or an arcade button) as these inputs are tied HIGH and checked by the device to see if they are pulled LOW but are left floating. therefore the only way i have been able to "switch" an input like this, is to change the pinMode from an input (which has no internal bias resistor) to an output (which does) effectively simulating a button press to ground.

i will post the code next week, as it's currently on my work computer, and i'm not going back to the office till tuesday. i'm sure it can be optimised better than the dirty dirty demon code i did... but in the end, it does work. and i've been testing the computer most of the last week here.

PL1

  • Global Moderator
  • Trade Count: (+1)
  • Full Member
  • *****
  • Offline Offline
  • Posts: 9456
  • Last login:Today at 02:28:33 am
  • Designated spam hunter
Re: Arduino code
« Reply #21 on: November 04, 2022, 11:11:35 pm »
I'll try to do some more testing this weekend on what's going on during initialization and input/output switching.
- Maybe a big initialization delay to ensure there's time to open the serial monitor window, add more serial print messages to show what's going on, and slow the process down enough to stabilize the multimeter/read it/analyze it at each step.

Once we know what's actually happening we can clean up, speed up, and accurately document the code.   :cheers:


Scott

lilshawn

  • Trade Count: (+3)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 7439
  • Last login:July 20, 2024, 11:44:22 am
  • I break stuff...then fix it...sometimes
Re: Arduino code
« Reply #22 on: November 04, 2022, 11:51:01 pm »
i did notice that the serial window did not display any info despite there being info that was supposed to be passed onto the terminal window... i thought it was just my computer setup, as i just threw on the software willy-nilly slammed some code in there and promptly got onto the next thing.

wasn't a big deal, i was able to follow along in the code and figure out where to make changes and whatnot...and the //'s really helped  :laugh:

you should see my code i wrote for a device i made that is used to auto program 60-in-1 board settings in all the games using the service mode and button presses.

it's a LITERAL sea of

pinMode(up, OUTPUT);
delay 200;
pinMode(up, INPUT)
delay 200;
pinMode(right, OUTPUT);
delay 200;
pinMode(right, INPUT)
delay 200;

it's dirty as all EFF and im ashamed of anybody that would need to change/fix/replicate it, but...it works..and at the end of the day... that's what matters.

PL1

  • Global Moderator
  • Trade Count: (+1)
  • Full Member
  • *****
  • Offline Offline
  • Posts: 9456
  • Last login:Today at 02:28:33 am
  • Designated spam hunter
Re: Arduino code
« Reply #23 on: November 05, 2022, 03:44:17 am »
i did notice that the serial window did not display any info despite there being info that was supposed to be passed onto the terminal window... i thought it was just my computer setup, as i just threw on the software willy-nilly slammed some code in there and promptly got onto the next thing.
1. You need to select a port other than COM1.

Sometimes I have to unplug/replug this board a few times to get COM5 to show up under the "Tools - Port" menu.
- Once it shows up, click on COM5 and you should get the selection checkmark.   ;D

2. Close the Serial Monitor window if you are uploading, resetting, or unplugging/replugging the board.  These are things that break the serial connection between the board and window.
- Open the Serial Monitor after you get the confirmation that the upload was successful or after the board boots up.  This ensures that the board is ready for the Serial Monitor to establish the serial connection.
- If you're not seeing the serial message stream, close and re-open the Serial Monitor to re-establish the connection.

wasn't a big deal, i was able to follow along in the code and figure out where to make changes and whatnot...and the //'s really helped  :laugh:
Glad the comments were useful.   :cheers:


Scott

lilshawn

  • Trade Count: (+3)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 7439
  • Last login:July 20, 2024, 11:44:22 am
  • I break stuff...then fix it...sometimes
Re: Arduino code
« Reply #24 on: November 08, 2022, 11:21:12 am »
Code: [Select]
// 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

PL1

  • Global Moderator
  • Trade Count: (+1)
  • Full Member
  • *****
  • Offline Offline
  • Posts: 9456
  • Last login:Today at 02:28:33 am
  • Designated spam hunter
Re: Arduino code
« Reply #25 on: November 11, 2022, 03:10:05 am »
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: [Select]
// 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

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

  • Trade Count: (+3)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 7439
  • Last login:July 20, 2024, 11:44:22 am
  • I break stuff...then fix it...sometimes
Re: Arduino code
« Reply #26 on: November 14, 2022, 07:51:23 pm »
Are there any other changes or features you would like added?

LMK if there are any mistakes or unclear explainations in the comments.


Scott

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

  • Trade Count: (+3)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 7439
  • Last login:July 20, 2024, 11:44:22 am
  • I break stuff...then fix it...sometimes
Re: Arduino code
« Reply #27 on: March 15, 2023, 05:31:52 pm »
for prosperity's sake...

Code: [Select]
// 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