Latest Raspbian on a pi zero w...
I'm newish and still learning. I have a simple python program that connects to i2c lcd display that will control credits on a home arcade machine. So a momentary switch that when pushed the display adds a credit. When the credits are an even number, activate a relay.
This works fine on the pi when ran on Thonny. The button when pressed changes the number on the display. But I need it to start on boot. I tried rc.local and it would load the program and set the default display to "Credits: 0", but when I pushed the button nothing would happen.
I have tried systemd and it appears that I'm successful in setting it up (following instructions found on various sites) but nothing seems to ever load.
Here is the code:
import RPi.GPIO as GPIO import lcddriver GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(18, GPIO.OUT) display = lcddriver.lcd() x=0 display.lcd_display_string("Credits: 0", 1) # Main body of code def button_callback(channel): global x try: x=x+1 display.lcd_display_string("Credits: "+str(x), 1) if (x % 2)==0: GPIO.output(18, GPIO.HIGH) time.sleep(.25) GPIO.output(18, GPIO.LOW) except KeyboardInterrupt: # If there is a KeyboardInterrupt (when you press ctrl+c), exit the program and cleanup print("Cleaning up!") display.lcd_clear() GPIO.add_event_detect(10, GPIO.RISING, callback=button_callback)
And here is how I setup the systemd:
sudo nano /lib/systemd/system/myscript.service
[UNIT] Description=Hollywood Skill Crane After=multi-user.target [Service] Type=idle ExecStart=/usr/bin/python /home/pi/lcd/demo_lcd.py [Install] WantedBy=multi-user.target
sudo chmod 644 /lib/systemd/system/myscript.service sudo systemctl daemon-reload sudo systemctl enable myscript.service
When I reboot, nothing. The LCD is blank.
When I tried rc.local, it least it loaded up the python and tried to run it (it would display the initial screen), but it was like it happened before the system was able to let it recognize the GPIO maybe because the buttons would do anything.
I also tried crontab but that didn't produce any results either.
@reboot python /home/pi/lcd/demo_lcd.py &
Any help is greatly appreciated. This project is far from over but I'm dead in the water if I can't get it to start up automatically once powered on.