t = 0 side = 4.0 thk = 0.3 s2 = 2*side - thk s3 = 2*side + thk board = Arduino(port="COM14") #Create an object for the Serial port. Adjust 'com11' to whatever port your arduino is sending to. #measuringRod = cylinder( radius= .1, length=6, pos=vector(-3,-2,0)) #lengthLabel = label(pos=vector(0,3,0), text='Target Distance is: ', box=False, height=40) #target=box(pos=vector(0,-.5,0), length=.2, width=3, height=3, color=color.blue) box(length=.100, width=15,height=1, pos=vector(-9.5,0,0),color=color.blue) box (pos=vector(-14.5, 0, 4), size=vector(s3, thk, s3), color = color.red) sphere(pos=vector(-12.5, 0, -4),size=vector(s3, thk, s3),color=color.yellow, radius=2.2) box(pos=vector(-9,0,9), size=vector(3, 5, -3),color=color.yellow) target=box(length=.1, width=10,height=5, pos=vector(-6,0,0),color=color.blue) #Create the object that will represent your target (which is a colored card for our project) myBoxEnd=box(length=.1, width=10,height=5, pos=vector(-8.5,0,0),color=color.blue) #This object is the little square that is the back of the ultrasonic sensor myTube2=cylinder(pos=vector(-8.5,0,-2.5), radius=1.5,length=2.5 ) #One of the 'tubes' in the front of the ultrasonic sensor myTube3=cylinder(pos=vector(-8.5,0,2.5), radius=1.5,length=2.5 ) #Other tube myBall=sphere(color=color.yellow,size=vector(s3, thk, s3), radius=.4) while True : #Create a loop that continues to read and display the data rate(3/dt) #Tell vpython to run this loop 20 times a second myData = str(board.Distance_Sense(13,11)) #Check to see if a data point #print (myData) #Print the measurement to confirm things are working distance = float(myData) #convert reading to a floating point number print (distance) #Print the measurement to confirm things are working target.pos=vector(-3+distance,-.5,0) #myLabel= 'Target Distance is: ' + myData #Create label by appending string myData to string #lengthLabel.text = myLabel #display updated myLabel on your graphic #target.pos=vector(-6 + distance,0,0) t = t+dt
from Arduino import Arduino import time '''distance sensor control led brightness ''' def my_map(x, in_min, in_max, out_min, out_max): return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min) board = Arduino(port="COM14") #com口根据实际情况修改 while True: value = board.Distance_Sense(13, 11) #Distance_Sense(trig_pin,echo_pin) #time.sleep(0.6) #Delay for some time value = value print(value) #time.sleep(0.06) #Delay for some time
#box (pos=vector(-14.5, 0, 4), size=vector(s3, thk, s3), color = color.red) #sphere(pos=vector(-12.5, 0, -4),size=vector(s3, thk, s3),color=color.yellow, radius=2.2) #box(pos=vector(-9,0,9), size=vector(3, 5, -3),color=color.yellow) dt = 0.0002 t = 0 board = Arduino( port="COM14" ) #Create sensorData 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( 30 ) #We need to tell Vpython how fast to go through the loop. 25 times a second works pretty well #rate(3/dt) textline = str( board.Distance_Sense(13, 11) ) #textline= sensorData.readline() # read the entire line of text #print(textline) # 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 float(textline) #last number in the list is the distance print(textline) #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 + textline, 0, 0 ) #Adjust the position of the target object to match the distance of the real target from the real sensor #t = t+dt