CSC 386: MAKING A TRAFFIC LIGHT SIMULATION WITH A RASPBERRY PI 3 – PART 2: Results and conclution

This post is about the result and the conclusion that have made in my Traffic Light Simulation Project but before going into detail, I want to take the first part of this post about what going on my life.

My life

I admit, I have been slacking on posting on my blog for like forever. For me, that is hard to say because I want to become better and I want to surpass my own expectations. It is hard, for the long thing and still, now. I feel like I am fighting an uphill battle against my expectation and my situation. This brings the feeling of been trapped in an ongoing endless cycle where there is on end in sight. It has taken me a while but I have realized with the help of others that happiness and this endless cycle is a product of the self. I am the one who controls my future, my happiness, my success, not someone else, me! This enlightenment has taken a while to realize come from my background but I am starting to feel more successful in understanding myself. I am my own enemy and ally in life. I have realized and are taken steps to find a balance with myself that suits me. After reflecting upon myself, I saw that I spend most of my day thinking about others and their happiness. I can not remember that the late time, I sat down and thought to myself what bring me happiness in my life. So, I started the thing and I still I am but I found that I am happier just being mindful rather than so focus.

Recently, I have had a hard time with myself and my next steps that I need to take. So, I felt that I need to put this message out there let people know that I changing myself to suit not the job market but myself. Plus, to tell people the knowledge that I have found within myself and the support of others.

Thank you to the people in life and the ones who will come. You, my human family, are the reason that I am strong and the reason I am here in the first place. It is as interest relationship of giving and taking. We live to die and die to live, we are one and we are many. So thank you for being human there is no one else as powerful as you.

Project:

I just had to get that off my chest. For the Traffic Light Situation project, I made a state machine, below is its diagram:

Rasp Pi TL SM Digram.png

The has to total of eight states not counting start and end. These states show how the Traffic Light will progress from the start of the program to the end. The circle named after color symbolize the light’s color at giving moment. The arrows show the direction of the cause leading with next state being the programs affect. These arrows also show the time is that it take to reach one state to another.

The Design:

The project called for three colors for the light. The starts with the light continually updating green until a button was pushed. After the button push, the project will wait for three seconds then change the light to yellow. Then after three seconds the light would change to red. After that, the program waits for five seconds then changes green and then waits for ten seconds then come back to the origin state and with for a button press.

For this project, we were also tasked with making the program that would not sleep. I found this method of waiting using a loop and time method.

Then, I used my instructor’s code that he provided for this project. I combined my knowledge of his program, the time method along with my state machine to create the code below:

import RPi.GPIO as GPIO
import curses, \
time

# Authors: John Hellrung and Shadia Prater
# Contributors: Matt Jadud and Dan Gibbs at http://www.dangibbs.co.uk/journal/python-wait-function-alternative-to-sleep-for-gtk

# Configuration variables.

# A dictionary mapping colors to correct pin numbers
pin_dictionary = {“GREEN”:4,”YELLOW”:17,”RED”:27}

def get_key (win):
key = None
try:
key = win.getkey()
except:
pass
return key

def wait(time_lapse):
# Function obtained by Dan Gibbs –> check contributors for link to code
“””
Description: Takes the amount of time wanted (time_lapse) and passes until the amount of time has come
Parameter(s): time_lapse is interger of time wanted in seconds
“””
time_start = time.time()
time_end= (time_start + time_lapse)
while time_end > time.time():
pass

def LED_blink(color,pin_dictionary,duration):
“””
Description: Blinks the color LEDs for a duration
Parameter(s): color takes a string,
pin_dictionary take a dirctionary,
duration take a interger
“””
for i in range(duration): #Loops for as many time as you want it
print “LED ” + color + ” ON\r” #Log that LED is ON
GPIO.output(pin_dictionary[color], GPIO.HIGH) #Set voltage to 5 for the LED
wait(1) #Waits a second
print “LED ” + color + ” OFF\r” #Log that LED is OFF
GPIO.output(pin_dictionary[color], GPIO.LOW) #Set voltage to 0 for the LED
wait(1) #Watis a second

# Das Blinkenlights references
# google://blink led python raspberry-pi
# https://projects.drogon.net/raspberry-pi/gpio-examples/tux-crossing/gpio-examples-1-a-single-led/
# https://thepihut.com/blogs/raspberry-pi-tutorials/27968772-turning-on-an-led-with-your-raspberry-pis-gpio-pins

def loop (win):
k = get_key(win)
if k == “q”:
return -1
elif k == “g”: # When the g key is hit this return 1
return 1
else:
LED_blink(“GREEN”,pin_dictionary,1)
return 0

def color_changes():
“””
Description: Color goes through Green to Yellow to Red and back to Green for the correct amount
of seconds when triggered by a button press”””
LED_blink(“GREEN”,pin_dictionary,3)
LED_blink(“YELLOW”,pin_dictionary,3)
LED_blink(“RED”,pin_dictionary,5)
LED_blink(“GREEN”,pin_dictionary,10)

def setup (win):
RUNNING = True
win.nodelay(True)
win.clear()

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(pin_dictionary[“GREEN”], GPIO.OUT)
GPIO.setup(pin_dictionary[“YELLOW”], GPIO.OUT)
GPIO.setup(pin_dictionary[“RED”], GPIO.OUT)
# When we are done setting up, loop.
while RUNNING:
# Our loop function happens over and over.
# It isn’t a loop so much as the “body” of
# the
exit_code = loop(win)
# If we get a return value less than zero,
# We should stop looping. Set RUNNING to False.
if exit_code < 0:
RUNNING = False
if exit_code > 0: # Check the value for greaterthan 0
color_changes() # Then run the color_changes funcation

# For curses references…
# google://non-blocking keyboard read python
# https://ubuntuforums.org/showthread.php?t=1514035
if __name__ == “__main__”:
curses.wrapper(setup)

 

As you can see, have a workpiece of code that does what it needs from my understanding. Thank you, Shadia and everyone else who help with project.

 

 

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