1

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

I added:

  [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

then

  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.

5
  • 1
    If you are running with Thonny it will be running as a Python3 program. Firstly check it runs from a command line with 'python3 /home/pi/lcd/demo_lcd.py'. It also looks like you need an infinite loop with a sleep to avoid the program exiting. – CoderMike Sep 10 '20 at 17:08
  • I get the same thing running it like that. I loads up the initial screen for Credits: 0 but then the button does nothing. – user3486773 Sep 10 '20 at 17:34
  • It would more to the point then if you edited this to remove all the extraneous stuff about it not working at boot. It doesn't work period, you need to solve that problem first. – goldilocks Sep 10 '20 at 18:12
  • So I think it actually is running however because I don't have an infinite loop it just appears the buttons do not work. But I'm not sure how to turn this into a loop. Every place I have added a "while True:" I get an error of conflicting edge detection already enabled for this GPIO channel. But I'm not sure how to get this to stay running, waiting for that button infinitely? – user3486773 Sep 10 '20 at 19:10
  • Just add while True: time.sleep(0.1) at the end of your code – CoderMike Sep 10 '20 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.