## This program reads data over the serial port ## from that arduino. You have to read an entire line of data ## and then you have to parse it into different number values ## Then those R, G, B numbers are used to make the color of ## a visual object in python change. from visual import * #import vPython library # start the color sensor MyScene=display(title='Virtual World with distance sensor') #Create your scene and give it a title. MyScene.width=800 #We can set the dimension of your visual box. 800X800 pixels works well on my screen MyScene.height= 800 MyScene.autoscale=False #We want to set the range of the scene manually for better control. Turn autoscale off MyScene.range = (12,12,12) #Set range of your scene to be 12 inches by 12 inches by 12 inches. target=box(length=.1, width=10,height=5, pos=(-2,0,0)) #Create the object that will represent your target (which is a colored card for our project) myBoxEnd=box(color=color.blue,length=.1, width=10,height=5, pos=(-8.5,0,0)) #This object is the little square that is the back of the ultrasonic sensor myBoxLED=box(color=color.blue,length=.1, width=5,height=5, pos=(-8.5,0,-12)) myTubeLed1=cylinder(color=color.gray(0.2),pos=(-8.5,-1.8,-13.5), radius=0.5,length=1.0 ) myTubeLed2=cylinder(color=color.gray(0.2),pos=(-8.5,1.8,-13.5), radius=0.5,length=1.0 ) myTubeLed3=cylinder(color=color.gray(0.2),pos=(-8.5,-1.8,-10.5), radius=0.5,length=1.0 ) myTubeLed4=cylinder(color=color.gray(0.2),pos=(-8.5,1.8,-10.5), radius=0.5,length=1.0 ) myTubeLed5=cylinder(color=color.gray(0.1),pos=(-8.5,0,-12), radius=1.2,length=0.5 ) myTube2=cylinder(pos=(-8.5,0,-2.5), radius=1.5,length=2.0 ) #One of the 'tubes' in the front of the ultrasonic sensor myTube3=cylinder(pos=(-8.5,0,2.5), radius=1.5,length=2.0 ) #Other tube myTube4=cylinder(color=color.black, pos=(-6.6,0,2.5), radius=1.2,length=0.2 ) myTube5=cylinder(color=color.black, pos=(-6.6,0,-2.5), radius=1.2,length=0.2 ) myBoxQuartz=box(color=color.white,length=0.5, width=2, height=0.5, pos=(-8.2,2,0)) myBall=sphere(color=color.red, radius=.3) ##sensorData= serial.Serial('com11',115200) # Create senorData object to read serial port data coming from arduino ## ##while True: #This is a while loop that will loop forever, since True is always True. ## ## rate(25) #We need to tell Vpython how fast to go through the loop. 25 times a second works pretty well ## while(sensorData.inWaiting()==0): # Wait here untill there is data on the Serial Port ## pass # Do nothing, just loop until data arrives ## textline = sensorData.readline() # read the entire line of text ## dataNums=textline.split(',') # Remember to split the line of text into an array at the commas ## red=float(dataNums[0]) # Make variables for Red, Blue, Green. Remember ## green=float(dataNums[1]) # the array was read as text, so must be converted ## blue=float(dataNums[2]) # to numbers with float command ## dist=float(dataNums[3]) # last number in the list is the distance ## ## blue=blue*.7 #On my sensor, blue is always a little too strong, so I tone it down a little ## if (dist>=1.5 and dist<=2.25): #only change color or target if target is between 1.5 and 2.25 inches from sensor ## target.color=(red/255., green/255., blue/255.) #This keeps color from flickering. ## ## target.pos=vector(-6 + dist,0,0) #Adjust the position of the target object to match the distance of the real target from the real sensor