The Code

How it Works

The code starts by trying to connect to the Arduino in all the different locations it could be on a windows machine.  Once it finds the proper COM port it sets the baud rate to 9600 for the serial communication between python and the Arduino.

The GUI for python was made using the “easygui” module, available at this link: http://easygui.sourceforge.net/. This module allows the programmer to make a very simple graphical user interface consisting of message boxes, boxes with buttons, boxes with a list of choices, and boxes with fields to enter information. The programmer can also add pictures to some of the types of boxes, as well as titles. The standard msgbox can accommodate a message, a title, a picture, and and buttons by simply writing eg.msgbox(message = “message”, title = “title”, choices = [“A”, “B”, “C”], image = image.gif)

To add in the pictures that are shown when a player answers a question, one must place a .gif version of that image in the same folder as the .py program is in.

The gui is the only part of the program that is used as the player goes through the game. Button boxes are used for the questions, and message boxes are used for the correct or wrong answer screens, as well as the final screen.

Once the user clicks “Go!” the code will send a command to the Arduino in the form of a string.  The Arduino is constantly checking for a signal from python to be sent to it.  When it receives the signal it processes it and then picks the correct profile from the three choices for graphs 1, 2, or 3.  The signal is created by incrementing a variable ‘i’ to change the duty cycle of the PWM signal.  To switch the polarity of the signal to the track a high signal was sent to the relay, this allowed us to make the car travel forwards and backwards around the track using only one power circuit.

The Python Code

# Too Fast, Too Curious 
# Ben Clemence, Austin Pietrobono, Rosalie Isis Shaw
# PDI Studio V, Ron Eglash, Fall 2012

# The program is a GUI that will control a slot car track with an Arduino

# import GUI library
import easygui as eg

# import serial communication library for communication with the Arduino
import serial
import pygtk

# Possible COM ports that the Arduino could be connected to
ports=[‘COM1′,’COM2′,’COM4′,’COM5′,’COM6′,’COM7′,’COM8′,’COM9′,’COM10′,’COM11′,’COM12′,’COM13′,’COM14′,’COM15’]    
 
# try to find the Arduino  
for device in ports:  
    try:  
        print “Trying…”,device  
        arduino = serial.Serial(device, 9600)  
        break  
    except:  
        print “Failed to connect on”,device  

######################################Graph3###################################################
def graph3():
    eg.buttonbox(msg = “Watch it Happen!”, title = “Graph 3”, image = “Graph3-0.gif”,choices = [“GO”])
    arduino.write(“4”)
    main()


def demo3_4():
    message = “Does the car move further from points A to D or points F to H?”
    image = “Graph3.gif”
    title = “Question 4”
    choices = [“A to D”, “F to H”, “No difference”]
    B = eg.buttonbox(msg=message, title=title, choices=choices, image=image)
    if B == “F to H” or B == “No difference” : #Wrong Answers
<span style=”font-family: ‘Helvetica Neue’, Helvetica, Ar