tofubob
Posts: 4
Joined: Mon Oct 27, 2014 1:11 am

How can I code a key press to turn on an LED?

Mon Oct 27, 2014 1:19 am

I've managed to get an LED to blink via the GPIO pins. I've done this via WiringPi and also separately in Scratch GPIO.

My end goal is to be able to press a key on the keyboard and have it activate a GPIO pin that has an LED attached. This is for a halloween costume. I want to code a small program that will autostart when the RasPi boots. It's sole function is detect keyboard presses and trigger the appropriate pin.

In Scratch I can emulate this behavior. I can autolaunch ScratchGPIO but can't autostart an individual project(i think). The costume won't have a screen which is why i want it to autostart.

How can I code a script in C or Python that will detect the appropriate keypress and turn a GPIO pin on?

Thanks!

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How can I code a key press to turn on an LED?

Mon Oct 27, 2014 8:14 am

tofubob wrote: In Scratch I can emulate this behavior. I can autolaunch ScratchGPIO but can't autostart an individual project(i think).
I think the --document option might be what you need -

Code: Select all

scratch --document "/home/pi/Documents/Scratch Projects/my_scratch_project.sb"

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

Re: How can I code a key press to turn on an LED?

Mon Oct 27, 2014 8:20 am

To do it in python start here: http://raspi.tv/2013/rpi-gpio-basics-7- ... -tutorials you also need to read the six earlier articles and they will give you a complete tutorial including the wiring of LEDs, switches and resistors.

Scroll down that page to "Links to all the RPi.GPIO Basics series" to get started.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

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

Return to “General programming discussion”