Checkpoint 4:  Graphically display the data collected from your light sensor.

 

Goals:

·        Understand the difference between an Arduino Sketch and a VPython program.

·        Get the Arduino to send data to a VPython program.

·        Learn enough VPython to be able to create a visual display to representation of collected data

 

In this checkpoint, we are going to use VPython to graphically display data collected from your light sensor.  This is going to be a difficult check point to get through so make sure that you ask for help when you are not fully understanding what is happening.

 

First, a brief description of VPython:  VPython is a programming language based upon the well-known and widely used Python programming language.  The chief advantage of VPython is its quick learning curve and the relative ease in making graphs and 3D objects on the screen.

 

VPython is an open-source language and can be downloaded at http://www.vpython.org.

 

Instructions for downloading and installing VPython.

1)      Go to the appropriate download section of http//www.vpython.org and carefully follow the directions for downloading version 2.7.

2)      Note that you are first to download Python-2.7.2 and only after installing Python you are next supposed to download VPython2.7.

3)      Only after both Python and VPython have been downloaded, installed and tested, you next need to install PySerial.  An automatic Windows installer for PySerial can be found at: http://sourceforge.net/projects/pyserial/files/pyserial/2.5/ . Another option is to go to http://pypi.python.org/pypi/pyserial to download and install Pyserial.  Ultimately, a folder named “serial” needs to end up in a directory that probably looks something like c:\Python27\Lib\site-packages.

For mac’s you might try http://randomproblem.wordpress.com/2012/01/14/installing-pyserial-2-5-on-mac-os-x-to-python-2-x-and-3-x/ .

 

 

Next, use the program from Checkpoint 3 that causes the Arduino to get a light sensor reading and then send that information to the computer.

 

The following VPython program will then give us a visual representation of the light sensor reading.

 

One quick note:  Make sure that you turn off the ‘serial monitor’ on the computer before you open and run VPython.

 

 

 

 

 

### The below VPython program opens up a serial connection with the Arduino

### and graphically displays light sensor result.

### It is important that the Arduino Serial Monitor is NOT open on your computer when

### you run this program.

 

import serial

from visual import *

import time

 

wire = serial.Serial("COM6", 9600) #Opens serial port.  You might also need to change the COM port

wire.timeout = 30 # length of time before checking the port again

strn = ""

value = 0

 

window = display(title='Photo Sensor Display',  width=600, height=600, 

center=(0,0,0), range = (500, 500, 500))

 

bulb = sphere(pos=(0,0,0), color = color.green, radius = 100,

              opacity = 1)# make a sphere to represent the LED.

 

while True: #Makes a continuous loop to read values from Arduino

   strn = wire.readline()# reads the value from the serial port

   print(strn)

   value = int(strn)# converts the read string to an integer

   print (value)

   bulb.radius = value/2# assigns the radius of the sphere to value/2

   bulb.opacity = value/255.0# changes the opacity

 

 

 

If time permits:

1)      Modify the VPython program so that, instead of a sphere, you use a cylinder with a height that represents the brightness of light detected by light sensor.

2)      You can also create graphs relatively easily in VPython.  To do this go to ___________ and try making a graph that puts