CSC 386: Making a Traffic Light Simulation with a Raspberry Pi 3 – Part 1: Plans and Ideas

TL: DR

For your first project, our profess is having us develop a traffic light simulation using our raspberry pi 3s. This project calls for a fives other pieces of hardware, a breadboard, three color LEDs (Red, Yellow, and Green) and momentary switches (Buttons). We are going to write the program to run this twice. Once in python and another in C. This way we can get a costumed to learning both languages for our goals later in the class. Below, I will going through, my idea and plans of this project.

Before starting, I will quick go over the requirements of the assignment.

  • We have to make a program that simulate a traffic light. The program must display the color green using text until the user push a button and then the display yellow text and then red text. After that it then must go back into the programs original cycle of displaying green.
  • The one exception of the program is that we can’t sleep function that comes pre-installed with python because sleep forces that program to go quiet and rest until the next command.
  • The time requirements for each light must be 3 seconds to switch from green to yellows, 5 seconds from yellow to red, immediately change from red to green and then the program should let the light stay green for 10 second before checking again if the user has push button.

These are the requirement of the program and they will require an led to change for the traffic light.

My thoughts are that this is a bit of difficult assignment but I believe I will be able to pull it off. Thanks to elections and 3 years of working with python but I can’t image the how the other are feeling right, now.

Anyway, so, here is what I found, when I was researching alternate methods of waiting functions.

I found two different methods:

The Timer Method involves:

Essentially you have to create a globe integer variable that holder the time and increases  time as your program processes.

The Threading method involves:

In the simplest of terms it means to multi-task. Using threading one can run two functions at once rather then one after the other.  Below is the example code used to show how threading in python works. For more information look here.

import threading
import time

def worker():
    print threading.currentThread().getName(), 'Starting'
    time.sleep(2)
    print threading.currentThread().getName(), 'Exiting'

def my_service():
    print threading.currentThread().getName(), 'Starting'
    time.sleep(3)
    print threading.currentThread().getName(), 'Exiting'

t = threading.Thread(name='my_service', target=my_service)
w = threading.Thread(name='worker', target=worker)
w2 = threading.Thread(target=worker) # use default name

w.start()
w2.start()
t.start()



Run the file and the results is below:

$ python -u threading_names.py

worker Thread-1 Starting
my_service Starting
Starting
Thread-1worker Exiting
 Exiting
my_service Exiting

As you can see the w or “working” started followed by w2 or “my_service” and then finally with t stating witch is not named. Then w, t and the finally w2 exit.

They say, I write print(“green”) 5 times in the worker thread you would see that now worker would print and end last bur the other threads would exit.

Rasp Pi TL Assignment.png

Above is my idea of the circuit that I will need. As you can see the raspberry pi run the program and uses io pins. Connected to the io pins is the led with has four inputs (GND, Green, Red, Blue) and the button with four pins but we are only going to be using three. I forgot to draw a pull up configuration for the button but other the other hand this is not the final design. I did included the resistor for the led.

So here is the plan, I going to use this treading and timer trick together in order to reach this assignment requirements. In my next post, I will go through detail on my program work with a state machine and going this different examples of the code. Thank you for reading.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s