import serial         # Import serial labrary
from visual import *   # Import all the Visual Library

MyScene = display(title='My Virtual World')
MyScene.width = 800
MyScene.height = 800
MyScene.autoscale = False
MyScene.range = (12,12,12)
target = box(length =.1, width = 10, height=5, pos=vector(-6,0,0))
myBoxEnd = box(length =.1, width = 10, height=5, pos=(-6,0,0))
myTube2 = cylinder(color=color.blue, pos=vector(-8.5,0,-2.5), radius=1.5, length=2.5)
myTube3 = cylinder(color=color.blue, pos=vector(-8.5,0,2.5), radius=1.5, length=2.5)
myBall = sphere(color=color.red, radius = .3)

sensorData = serial.Serial('/dev/cu.usbserial-A9071I4P',9600)

while (True):
    rate(25)
    while (sensorData.inWaiting()==0):  # while there is no data, do nothing
            pass
    texline = sensorData.readline()   # ... than read those waiting data to the variable named 'myData'
    dataNums = textline.split(',')    # split textline data into array with vour elements: dataNums(0), dataNums(1), ...
    red = float(dataNums(0))
    green= float(dataNums(1))
    blue = float(dataNums(2))
    dist = float(dataNums(3))
    
    print dist

       
   
                                         
