Joseph Burrell
Posts: 7
Joined: Mon May 25, 2015 6:23 pm

Joystick Help!

Fri Jun 19, 2015 10:35 pm

Hi. I am trying to make it so that when Tim turns on the Pi. It comes up with the temperature. This is one page. When he moves the joystick right, it turns to the other page which shows him the humidity. He can press it again which goes to another page to show whatever. Please help! I am completely stuck and have been for days and I will not be able to finish it in time! Thanks. Here is the code.

Code: Select all

[code]from astro_pi import AstroPi # Imports the Astro Pi
ap = AstroPi() # Changing Astro Pi into 'ap'

import pygame

from pygame.locals import *
from astro_pi import AstroPi

import time # Imports the time
t = time

ap.set_rotation(270)
#ap.show_message("Welcome, Tim!", scroll_speed=0.05, text_colour=[255,0,0]) # Shows the message: Welcome, Tim!

X = [255, 255, 255] # Displays the text in white.
O = [0, 0, 0] # No colour

tem = ap.get_temperature() # This gets the temperature from the surroundings
temp = int(round(float(tem))) # Turns the temperature into an integer
t.sleep(3)

# --------------------------------------------------------2--

twenty = [
X, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, X, X, X, O, X, X, X,
O, O, O, X, O, X, O, X,
O, O, X, O, O, X, O, X,
O, X, O, O, O, X, O, X,
O, X, X, X, O, X, X, X
]

if temp == 20:
    ap.set_pixels(twenty)

# ----------------------------------------------------------

twenty_one = [
X, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, X, X, X, O, X, X, O,
O, O, O, X, O, O, X, O,
O, O, X, O, O, O, X, O,
O, X, O, O, O, O, X, O,
O, X, X, X, O, X, X, X
]

if temp == 21:
    ap.set_pixels(twenty_one)

# ----------------------------------------------------------

twenty_two = [
X, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, X, X, X, O, X, X, X,
O, O, O, X, O, O, O, X,
O, O, X, O, O, O, X, O,
O, X, O, O, O, X, O, O,
O, X, X, X, O, X, X, X
]

if temp == 22:
    ap.set_pixels(twenty_two)

# ----------------------------------------------------------

twenty_two = [
X, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, X, X, X, O, X, X, X,
O, O, O, X, O, O, O, X,
O, O, X, O, O, O, X, O,
O, X, O, O, O, X, O, O,
O, X, X, X, O, X, X, X
]

if temp == 22:
    ap.set_pixels(twenty_two)

# ----------------------------------------------------------

twenty_three = [
X, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, X, X, X, O, X, X, X,
O, O, O, X, O, O, O, X,
O, O, X, O, O, O, X, X,
O, X, O, O, O, O, O, X,
O, X, X, X, O, X, X, X
]

if temp == 23:
    ap.set_pixels(twenty_three)

# ----------------------------------------------------------

twenty_four = [
X, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, X, X, X, O, X, O, O,
O, O, O, X, O, X, O, O,
O, O, X, O, O, X, X, O,
O, X, O, O, O, X, X, X,
O, X, X, X, O, O, X, O
]

if temp == 24:
    ap.set_pixels(twenty_four)

# ----------------------------------------------------------

twenty_five = [
X, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, X, X, X, O, X, X, X,
O, O, O, X, O, X, O, O,
O, O, X, O, O, X, X, X,
O, X, O, O, O, O, O, X,
O, X, X, X, O, X, X, X
]

if temp == 25:
    ap.set_pixels(twenty_five)

# ----------------------------------------------------------
    
twenty_six = [
X, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, X, X, X, O, X, X, X,
O, O, O, X, O, X, O, O,
O, O, X, O, O, X, X, X,
O, X, O, O, O, O, O, X,
O, X, X, X, O, X, X, X
]

if temp == 26:
    ap.set_pixels(twenty_six)

# ----------------------------------------------------------

twenty_seven = [
X, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, X, X, X, O, X, X, X,
O, O, O, X, O, O, O, X,
O, O, X, O, O, O, O, X,
O, X, O, O, O, O, O, X,
O, X, X, X, O, O, O, X
]

if temp == 27:
    ap.set_pixels(twenty_seven)

# ----------------------------------------------------------
    
twenty_eight = [
X, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, X, X, X, O, X, X, X,
O, O, O, X, O, X, O, X,
O, O, X, O, O, X, X, X,
O, X, O, O, O, X, O, X,
O, X, X, X, O, X, X, X
]

if temp == 28:
    ap.set_pixels(twenty_eight)

# ----------------------------------------------------------

twenty_nine = [
X, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, X, X, X, O, X, X, X,
O, O, O, X, O, X, O, X,
O, O, X, O, O, X, X, X,
O, X, O, O, O, O, O, X,
O, X, X, X, O, O, O, X
]

if temp == 29:
    ap.set_pixels(twenty_nine)

#----------------------------------------------------------
    
thirty = [
X, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, X, X, X, O, X, X, X,
O, O, O, X, O, X, O, X,
O, O, X, X, O, X, O, X,
O, O, O, X, O, X, O, X,
O, X, X, X, O, X, X, X
]

if temp == 30:
    ap.set_pixels(thirty)

# ----------------------------------------------------------

thirty_one = [
X, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, X, X, X, O, X, X, O,
O, O, O, X, O, O, X, O,
O, O, X, X, O, O, X, O,
O, O, O, X, O, O, X, O,
O, X, X, X, O, X, X, X
]

if temp == 31:
    ap.set_pixels(thirty_one)
    t.sleep
# ----------------------------------------------------------

thirty_two = [
X, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, X, X, X, O, X, X, X,
O, O, O, X, O, O, O, X,
O, O, X, X, O, O, X, O,
O, O, O, X, O, X, O, O,
O, X, X, X, O, X, X, X
]

if temp == 31:
    ap.set_pixels(thirty_one)
    t.sleep
# ----------------------------------------------------------



# ----------------------------------------------------------

hum = ap.get_humidity() # This gets the humidity from the surroundings
humi = int(round(float(hum))) # This turns it into an integer

# ----------------------------------------------------------

fourty = [
X, X, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, X, O, O, O, X, X, X,
O, X, O, O, O, X, O, X,
O, X, X, O, O, X, O, X,
O, X, X, X, O, X, O, X,
O, O, X, O, O, X, X, X
]

if humi == 40:
    ap.set_pixels(fourty)

# ----------------------------------------------------------
pygame.init()
pygame.display.set_mode((640, 480))

ap = AstroPi()
ap.clear()


if event.key == K_DOWN and ap.set_pixels == 'twenty_nine':
    ap.set_pixels == 'twenty_three'
[/code]

User avatar
Davespice
Forum Moderator
Forum Moderator
Posts: 1662
Joined: Fri Oct 14, 2011 8:06 pm
Location: The Netherlands
Contact: Twitter

Re: Joystick Help!

Sat Jun 20, 2015 9:51 am

Hi Joseph
This is not as hard as you think. If you look at the example code here:
https://www.raspberrypi.org/learning/as ... oystick.md

In the program you have an x and y variable and you update this when the joystick is pressed in a particular direction.
You just need to do the same thing but only in the x axis. Then test the position of the x variable before you show your page.

For instance:

Code: Select all

if x == 0:
    ap.show_message("Page 1")
elif x == 1:
    ap.show_message("Page 2")
etc

Joseph Burrell
Posts: 7
Joined: Mon May 25, 2015 6:23 pm

Re: Joystick Help!

Fri Jun 26, 2015 4:25 pm

I'm sorry, but I genuinely do not understand. I have been spending hours trying to do it, but it isn't working! I have done very little and the deadline is in two weeks so I doubt that I will be able to enter.

I don't know what to do!

User avatar
Davespice
Forum Moderator
Forum Moderator
Posts: 1662
Joined: Fri Oct 14, 2011 8:06 pm
Location: The Netherlands
Contact: Twitter

Re: Joystick Help!

Sat Jun 27, 2015 7:43 pm

Try this:

Code: Select all

import pygame
import time

from pygame.locals import *
from astro_pi import AstroPi

pygame.init()
pygame.display.set_mode((640, 480))

ap = AstroPi()
ap.clear()

running = True

x = 0
y = 0

ap.set_pixel(x, y, [255, 255, 255])

while running:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            ap.set_pixel(x, y, [0, 0, 0])

            if event.key == K_RIGHT and x < 7:
                x = x + 1
            elif event.key == K_LEFT and x > 0:
                x = x - 1
            elif event.key == K_ESCAPE:
                running = False
                break

            ap.set_pixel(x, y, [255, 255, 255])
            time.sleep(.1)
		
            if x == 0:
                ap.show_message("Page 1")
            elif x == 1:
                ap.show_message("Page 2")
            elif x == 2:
                ap.show_message("Page 3")
            elif x == 3:
                ap.show_message("Page 4")
            elif x == 4:
                ap.show_message("Page 5")
            elif x == 5:
                ap.show_message("Page 6")
            elif x == 6:
                ap.show_message("Page 7")
            elif x == 7:
                ap.show_message("Page 8")
      if event.type == QUIT:
          running = False
Use the Escape key to quit, or you can click the little X in the top right corner if running under the desktop.
P.S. Don't be a copy and paste programmer, read the code and try to understand it before using it.

Return to “Astro Pi”