Jump to content
 
  • entries
    7
  • comments
    6
  • views
    11,213

4. Simple 2-aspect signal control


DavidB-AU

1,066 views

We use the same electronic set up as the previous tutorial, but now pretend it is a simple 2-aspect colour light signal. Instead of LEDs on the breadboard, there could be leads out to a real signal on the layout.

blogentry-6959-0-44285600-1359211343.png

 

We want to be able to control this "signal" remotely by pressing the appropriate keys on the keyboard. Unfortunately this requires importing some more packages and adding more code to detect when a key is pressed. (You don't necessarily need to understand how this bit works.)

 

Create a new file called signal.py with the following:

import os, sys, termios         # This imports packages needed to detect key pressesimport RPi.GPIO as GPIO         # This imports the GPIO package# Set up pins 18 (GPIO24) and 22 (GPIO25) as outputGPIO.setup(18, GPIO.OUT)GPIO.setup(22, GPIO.OUT)# This sets the default state with the "signal" set to RED.GPIO.output(18, False)          # Turns red LED onGPIO.output(22, True)           # Turns green LED off# Set up detection of keypress.def getKey():  fd = sys.stdin.fileno()  old = termios.tcgetattr(fd)  new = termios.tcgetattr(fd)  new[3] = new[3] & ~termios.ICANON & ~termios.ECHO  new[6][termios.VMIN] = 1  new[6][termios.VTIME] = 0  termios.tcsetattr(fd, termios.TCSANOW, new)  key = None  try:    key = os.read(fd, 3)  finally:    termios.tcsetattr(fd, termios.TCSAFLUSH, old)  return key   while 1:  x = str(getKey())         # This detects a key being pressed and saves the value into a variable called x  if x == "r":              # If key pressed is equal to r...    GPIO.output(18, False)  # ...turn the red LED on    GPIO.output(22, True)   # ...and turn the green LED off  if x == "g":              # If key pressed is equal to g...    GPIO.output(18, True)   # ...turn the red LED of    GPIO.output(22, False)  # ...and turn the green LED on  if x == "q":              # If keypress is equal to q...    GPIO.output(18, False)  # ...turn the red LED on (safety first on our railway!)    GPIO.output(22, True)   # ...and turn the green LED off    break                   # ...then quit the program  else:                     # In all other cases...    pass                    # ...do nothing

 

Now run the program as root.

pi@raspberrypi ~$ sudo python signal.py

 

Note that the program is set up to detect lower case letters to make sure Caps Lock is off. When you press keys on the keyboard things will start to happen to the "signal".

 

If you press g, the signal turns green.

If you press r, the signal turns red.

If you press q, the signal returns to red (safety first!) and the program quits.

If you press other keys, the program ignores them.

 

blogentry-6959-0-56446200-1359212591.pngblogentry-6959-0-62339400-1359212713.png

 

This is a very basic demonstration of how to control a "signal". In this case the signal is controlled by pressing keys on the keyboard, but it's not the only way. The signal could be triggered by other events such as a train passing (and resetting the signal to danger) or by detecting some other event or trigger. We'll come to inputs next.

 

blogentry-6959-0-33640800-1359212213.png

  • Like 1

0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...