Software Support > Automated Projects

PC Smart Switch

<< < (2/3) > >>

DaOld Man:
And heres the code I have so far, if I edit it I will post an update.
RPI Pico, using Circuit Python language.

Final UPDATE: (Hopefully anyway).
I changed the code to allow you to use either a maintained toggle like switch or a momentary pushbutton to start and shutdown.
You can use a jumper soldered to the pico to choose which mode without having to open the code.
But i figured why? Just set MODE flag True or False depending on if you use a switch or button.
Theres probably not a reason to have a hardwired jumper that I can think of, but if you want me to post code using the jumper, I can.
Or you can change the code below to use the jumper. (Instructions in the comments #)


This seems to be working great.



--- Code: ---#Using RPi PICO to start up and shutdown my arcade cabinet. KRR 04/08/2026
#Coded in CircuitPython

import time
import board
import digitalio
from digitalio import DigitalInOut, Direction, Pull

#board led
led = DigitalInOut(board.LED)
led.direction = Direction.OUTPUT
led.value = False

#inputs
#power switch On=LOW
SWITCH = digitalio.DigitalInOut(board.GP3)
SWITCH.direction= Direction.INPUT
SWITCH.pull = Pull.UP

#pc is on input (Low when on)
PCON = digitalio.DigitalInOut(board.GP5)
PCON.direction= Direction.INPUT
PCON.pull = Pull.UP

#outputs
#pc on button
PCSTART= digitalio.DigitalInOut(board.GP4)
PCSTART.direction= Direction.OUTPUT
PCSTART.value=False

#ACC relay
ACCON= digitalio.DigitalInOut(board.GP6)
ACCON.direction= Direction.OUTPUT
ACCON.value=False

#electrically press ESC button
ESCBUTT= digitalio.DigitalInOut(board.GP7)
ESCBUTT.direction= Direction.OUTPUT
ESCBUTT.value=False

#electrically Press front end exit button
FEEXIT= digitalio.DigitalInOut(board.GP8)
FEEXIT.direction= Direction.OUTPUT
FEEXIT.value=False

#if you use a onboard jumper to choose switch or button, Remove the #'s below
#and use GPIO number the jumper is placed on
#you will also need to change all instance of MODE to MODE.value

#jumper to select switch or button
#MODE=digitalio.DigitalInOut(board.GP13)
#MODE.direction= Direction.INPUT
#MODE.pull = Pull.UP

temp_flag=False

#if not using hardware jumper,
#If using momentary pushbutton, MODE=True
#If using maintained switch, MODE=False
MODE=False

time.sleep(0.25)
#MODE can be jumper pins on Pico (GP13 and Ground, Pins 17 & 18) uncomment the assignment above
#or MODE can made high or low here, if you dont use a real-time jumper. Example: MODE=False (or True)

while True:
    if MODE: #momentary pushbutton is used
        if not SWITCH.value: #switch is on
            if PCON.value: #PC is off
                if not temp_flag: #only scan once, in case button is held in
                    led.value=True
                    PCSTART.value=True
                    time.sleep(.25)
                    PCSTART.value=False
                    ACCON.value=True
                    temp_flag=True
            else: #PC is on, shut er down
                PCSTART.value=False
                ACCON.value=False
                led.value=False
                ESCBUTT.value= True
                time.sleep(0.25)
                ESCBUTT.value= False
                time.sleep(1.5)
                FEEXIT.value= True
                time.sleep(0.25)
                FEEXIT.value= False
        else: #switch is off       
            temp_flag=False #reset the flag
        time.sleep(.025)
    else:
        temp_flag=False   
        break

#Maintained switch used
while True:
    if SWITCH.value: break
    led.value=True   
    time.sleep(.25)
    led.value=False
    time.sleep(0.25)
   
while True:
    if not SWITCH.value:
        led.value=True
        ACCON.value=True
        if PCON.value:           
            if not temp_flag:
               PCSTART.value=True
               time.sleep(0.25)
               PCSTART.value=False     
               temp_flag=True       
    else:
        led.value=False       
        if  not PCON.value:
            if temp_flag:
                PCSTART.value=False
                ACCON.value=False
                ESCBUTT.value= True
                time.sleep(0.25)
                ESCBUTT.value= False
                time.sleep(1.5)
                FEEXIT.value= True
                time.sleep(0.25)
                FEEXIT.value= False
        temp_flag=False
    time.sleep(0.5)   

--- End code ---

DaOld Man:
Id also like to add, that I have the option to "shutdown PC when Mala exits" selected, in Mala Options.
If I want to do work on the PC I can plug in a mouse and exit Mala by right clicking then choosing exit.
This does not shut down the PC. Only drawback that unlike my first circuit, turning the switch off will not shut down PC, I will have to shut it down with windows start/shutdown button. The main switch will have to be turned to off then to on to restart the PC.

Also, not in the diagram, I added a jumper on the output leads of the Auxiliary relay opto. The power for this relay comes from PC 12 volt supply.

Auxiliaries are monitor power, marquee light, speaker amp, and the lighted pushbuttons and joystick servos. (Controlled by 12 VDC relay)
If the jumper is installed, all the auxiliaries stay on until the PC shuts down, if jumper is removed, the auxiliaries will go off as soon as the power switch is turned off.

Having this option gives it an original arcade cabinet feel, but allows you to keep everything on for troubleshooting while shutting down, if necessary.
I need to add this jumper to the schematic.
I power my coin door lights off the PC 12 volt supply, so if for whatever reason the PC doesnt shutdown I will know by the coin lights staying on.


DaOld Man:
Here is a pic of pico and interface board mounted and wired up to my cabinet.
I have modified the code to use a momentary button as press on/ press off in place of the maintained toggle switch.
Im thinking of adding a jumper on the pico to choose switch or button.
I havent tested the code yet but will update the post above when it tests good.
In the pic, I didnt label the switch connection to the interface board. Its the blue terminal strip at bottom of board.

Zebidee:
My original post was wrong because I missed something, so deleted it.  :dunno

DaOld Man:

--- Quote from: Zebidee on April 08, 2026, 07:42:30 am ---My original post was wrong because I missed something, so deleted it.  :dunno

--- End quote ---

No prob, want me to delete this post?

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version