I have a headless Pi running Raspbian with the Sense HAT attached on top. A portable micro USB charger is attached to the outside of the casing, in order to make the Pi handheld. My classes are currently trying to put together a game where, as one can see in the coding, random responses are given at each shake of the Pi. The main problem comes towards the end of the script, however, since it would be helpful to have the Pi shutdown after four minutes of inactivity.

This is the current script:

import random
import time
from sense_hat import SenseHat
from subprocess import call 

sh = SenseHat()

sh.show_message("Let's begin Act I...", text_colour=[0, 50, 220], scroll_speed=.055)

replies = ['Scene 1, Ln 12-3',
       'Scene 3, Ln 14-5',
       'High five Roman +1',
       'Scene 6, Ln 87-9',
       'Chicken Dance +2',]

while True: 
    x, y, z = sh.get_accelerometer_raw().values()

    x = abs(x)
    y = abs(y)
    z = abs(z)

    if x > 2 or y > 2 or z > 2 :
        sh.show_message(random.choice(replies), text_colour=[0, 50, 220], scroll_speed=.055) 
    elif x==0 and y==0 and z==0 :
        sh.show_message("Time to shutdown!", text_colour=[0, 50, 220], scroll_speed=.055)
        call("sudo nohup shutdown -h now", shell=True)

Python checks to see if the x, y, and z values for the accelerometer are equal to zero, at which point, the time delay should kick in. In browsing, it seems that perhaps instead of calling to the shutdown command, "os.system()" should be used instead in some way?

Thank you!

1 Answer 1


For the shutdown command, try:

os.system("sudo shutdown -h now")

The problem with the script as it stands now is that your time delay is just halting operation of the script for 240 seconds which is not what you need. Instead you need to mark the time when there's zero activity and store it in a variable and then compare that variable to actual time each time through the loop to see if the delta is >240 seconds.

  • thank you. I went back and did some reading about the time functions and variables. Commented Feb 9, 2016 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.