2

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.set_rotation(90)
sh.show_message("Let's begin Act I...", text_colour=[0, 50, 220], scroll_speed=.055)
time.sleep(4) 

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 :
        time.delay(240)
        sh.show_message("Time to shutdown!", text_colour=[0, 50, 220], scroll_speed=.055)
        time.delay(1)
        call("sudo nohup shutdown -h now", shell=True)
    else:
        sh.clear()

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!

3

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.

1
  • thank you. I went back and did some reading about the time functions and variables. Feb 9 '16 at 20:54

Your Answer

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

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