tonygo2
Posts: 86
Joined: Sun Aug 28, 2011 2:50 pm
Location: Leicester, UK

Joystick control

Sat Sep 26, 2015 9:33 am

I could not get the joystick example in
https://www.raspberrypi.org/learning/as ... oystick.md
to work. It crashed at
if event.key == K_DOWN and y < 7:
with the following error:
AttributeError: ‘Event’ object has no attribute ‘key’

I’ve not used pygame before, only the GPIO pins, so thought I would investigate and learn about pygame.

There appeared to be two problems:
1) Each instance of “if event.key ==” needs “pygame.” before the K_name.
2) The whole “if event.key” block of code needs further indentation.

I’ve added quitting via the centre joystick button, turned off the LEDs on quitting and drastically reduced the size the pygame.display black window, which kept getting in the way.

The modified code is below.

Code: Select all

# Modified from Astro Pi Guide for Teachers
#      raspberrypi.org/resources
# Tony Goodhew 26 Sept 2015
import pygame

from pygame.locals import *
from sense_hat import SenseHat

pygame.init()
pygame.display.set_mode((90,5))#Big enough

sense = SenseHat()
sense.clear()

print("Steer the BLOB with the joystick")
print("  * Up, Down, Left or Right *\n")
print("Quit with Joystick Centre Button, RETURN or ESC\n")

running = True

x = 0
y = 0
sense.set_pixel(x, y, 255, 255, 255)

while running:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            sense.set_pixel(x, y, 0, 0, 0)  # Black 0,0,0 means OFF

            if event.key == pygame.K_DOWN and y < 7:
                y = y + 1
            elif event.key == pygame.K_UP and y > 0:
                y = y - 1
            elif event.key == pygame.K_RIGHT and x < 7:
                x = x + 1
            elif event.key == pygame.K_LEFT and x > 0:
                x = x - 1
            elif event.key == pygame.K_RETURN:
                running = False
                sense.clear()
                print("** BYE from Joystick **")
            if running: sense.set_pixel(x, y, 255, 255, 255)
            
        if event.type == QUIT:
            running = False
            sense.clear()
            print(" ++ BYE from mouse QUIT ++")
It works on my sense HAT driven by a Pi2B and I now know more than I did about pygame.

User avatar
dliloch
Posts: 168
Joined: Wed Jun 27, 2012 6:28 pm
Location: cleveland, ohio usa

Re: Joystick control

Fri Oct 09, 2015 9:13 pm

Hello,
I have a sensehat and I've implemented the center button to shutdown.. I was doing this so that I can run it headless and turn it off so that I don't mess up the sd card.. My problem now is that it works fine when I run it from a terminal but when I run it from my use crontab it quits. There seems to be something with the pygame module because my other sensehat routines run fine from crontab. I am using the crontab associated with pi NOT root. any ideas? have you tried it from crontab? I actually do a shutdown when the center button is pressed..Is it also possible to read GPIO 13? Does that pin go on when the joystick is pressed down?

thanks,
-don

Return to “Astro Pi”