Linux Format

Taking pictures

- LXF

We start by creating a temp variable which will contain the current temperatur­e. We use the Sense Hat temp sensor to take a reading, which is rather precise, and round the reading to two decimal places. We fix the resolution of the camera to 800x600 pixels and the framerate to 24 frames per second and open the preview window for five seconds allowing time to frame the shot before the preview window is closed. Next, we annotate the image to include the current temperatur­e, before sleeping for 0.1 seconds, and taking the picture and saving the image to home with the current time and date as the file name. Our final function handles the flash. def flash(toggle): print(toggle) if toggle == ‘on':

sense.clear(255,255,255) elif toggle == ‘off':

sense.clear() We touched on this function in the takepic() function and we call this function from inside of takepic() . The flash function has one argument and this controls whether the flash is on or off. If the value of toggle is ‘on’ then all of the LEDs are set to full brightness. If the value of toggle is ‘off’ the LED matrix is turned off. We now move to the main code ( see http://bit.ly/LXF205Astr­oCam): try: timer = 0 while True: for event in pygame.event.get():

if event.type == KEYDOWN: We start by using a try…except test which will enable our code to exit gracefully, if needed. In there we set the value of the timer to 0 ( timer = 0 ) and use a loop to check for any user input on the joystick. If the joystick is pressed then a key press is detected. if event.key == pygame.K_UP: print('Adding time') timer = timer + 5 sense.show_message(str(timer), text_ colour=[255,0,0])

If the key press returns that up has been pressed on the joystick then five seconds is added to the timer and reported to the user via the LED matrix. Likewise, if down is pressed on the joystick then five seconds is removed from the timer. elif event.key == pygame.K_LEFT: print('Add flash') sense.show_message('Flash ready’, text_ colour=[255,0,0], scroll_speed=0.05)

toggle = ‘on’ Here we use pressing left on the joystick to turn on the flash by changing the value stored in the variable toggle to ‘on’ . Pressing right on the joystick will turn off the flash. Our final condition triggers taking a picture. elif event.key == pygame.K_RETURN: print('Takepic') takepic(timer,toggle) flash('off') Pressing the joystick in is the same as pressing Enter or Return and triggers the takepic() function and passes the values of the timer and the toggle variables as arguments to the function. A picture is taken and then the flash is turned off. Our final section of code is the except part of the try… except test. except KeyboardIn­terrupt: pygame.display.quit() pygame.quit()

If the user pressed CTRL+c the project will cleanly close any open pygame windows and exit. With all this code complete, save you work and click on Run > Run Module to start taking pictures with your AstroCam!

 ??  ?? The Sense Hat is a remarkable platform for running scientific experiment­s, and thanks to a robust Python library we can integrate it into lots of different projects.
The Sense Hat is a remarkable platform for running scientific experiment­s, and thanks to a robust Python library we can integrate it into lots of different projects.

Newspapers in English

Newspapers from Australia