richardhughes
Posts: 4
Joined: Thu Jun 04, 2015 7:47 pm

Code to check if the switch is pressed

Wed Jun 10, 2015 6:09 pm

I just need to know what the code is to check the central press of the joystick and if it is not pressed, then to loop back and check again, meaning it is constantly checking.
Thanks

gordon77
Posts: 5279
Joined: Sun Aug 05, 2012 3:12 pm

Re: Code to check if the switch is pressed

Wed Jun 10, 2015 8:58 pm

How is the joystick connected?

User avatar
ben_nuttall
Posts: 235
Joined: Sun Aug 19, 2012 11:19 am
Location: Cambridgeshire, UK
Contact: Website Twitter

Re: Code to check if the switch is pressed

Wed Jun 10, 2015 10:39 pm

richardhughes wrote:I just need to know what the code is to check the central press of the joystick and if it is not pressed, then to loop back and check again, meaning it is constantly checking.
Thanks
The Astro Pi / Sense HAT's joystick is mapped to the up/down/left/right/enter keys on the keyboard. To detect joystick use, use an event loop in PyGame:

Examples:

https://github.com/astro-pi/astro-pi-ha ... oystick.py
https://github.com/bennuttall/astro-pi- ... r_match.py
Former RPF staff. Author of gpiozero and creator of piwheels.

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

Re: Code to check if the switch is pressed

Mon Jun 15, 2015 9:31 pm

Hi. I want to know how to code my Pi so that when I move the Joystick one way, the 'page' which is being shown on the LEDs will move to a different page. Eg. One page shows the temperature, the joystick is moved right, the next page shows the humidity. I don't understand how to do this.

Thanks!

scotty101
Posts: 4104
Joined: Fri Jun 08, 2012 6:03 pm

Re: Code to check if the switch is pressed

Tue Jun 16, 2015 9:14 am

Post some code showing how you currently show each 'page' and how you are reading the joystick buttons and I can help fill in the blanks.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

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

Re: Code to check if the switch is pressed

Fri Jun 19, 2015 8:05 pm

Code: Select all

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'

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

Re: Code to check if the switch is pressed

Fri Jun 19, 2015 8:06 pm

ben_nuttall wrote:
richardhughes wrote:I just need to know what the code is to check the central press of the joystick and if it is not pressed, then to loop back and check again, meaning it is constantly checking.
Thanks
The Astro Pi / Sense HAT's joystick is mapped to the up/down/left/right/enter keys on the keyboard. To detect joystick use, use an event loop in PyGame:

Examples:

https://github.com/astro-pi/astro-pi-ha ... oystick.py
https://github.com/bennuttall/astro-pi- ... r_match.py
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'

User avatar
DougieLawson
Posts: 40227
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Code to check if the switch is pressed

Fri Jun 19, 2015 8:14 pm

If you're going to post code on the forum can you post it in [code]... your code goes here ... [/code] tags.

That makes the difference between

if event.key == K_DOWN and ap.set_pixels == 'twenty_nine':
ap.set_pixels == 'twenty_three'

and

Code: Select all

if event.key == K_DOWN and ap.set_pixels == 'twenty_nine':
    ap.set_pixels == 'twenty_three'
Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Return to “Astro Pi”