Preamble
It is always fascinating when the virtual reality starts to compete with real world, particularly when one can track user’s locomotion, head movement, gestures, EEG and vital statistics etc. Here is just a little teaser project about virtual reality.
Parts needed
- Breadboard
- Arduino Uno or other board
- Hc-sr04 Ultrasonic Distance Measuring Sensor
- connecting wires
The fritzing schematic
sketch code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
int trigPin=13; //Sensor Trig pin connected to Arduino pin 13 int echoPin=11; //Sensor Echo pin connected to Arduino pin 11 float pingTime; //time for ping to travel from sensor to target and return float targetDistance; //Distance to Target in inches float speedOfSound=776.5; //Speed of sound in miles per hour when temp is 77 degrees. void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { // put your main code here, to run repeatedly: digitalWrite(trigPin, LOW); //Set trigger pin low delayMicroseconds(2000); //Let signal settle digitalWrite(trigPin, HIGH); //Set trigPin high delayMicroseconds(15); //Delay in high state digitalWrite(trigPin, LOW); //ping has now been sent delayMicroseconds(10); //Delay in low state pingTime = pulseIn(echoPin, HIGH); //pingTime is presented in microceconds pingTime=pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second) pingTime=pingTime/3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour) targetDistance= speedOfSound * pingTime; //This will be in miles, since speed of sound was miles per hour targetDistance=targetDistance/2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance. targetDistance= targetDistance*63360; //Convert miles to inches by multipling by 63360 (inches per mile) Serial.println(targetDistance); delay(100); //delay tenth of a second to slow things down a little. } |
python code with vpython
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import serial #Import Serial Library for python 2 only from visual import * #Import all the vPython library arduinoSerialData = serial.Serial("/dev/ttyACM1", 9600) #Create an object for the Serial port. Adjust 'com11' to whatever port your arduino is sending to. MyScene=display(title='My Virtual World') #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=true #We want to set the range of the scene manually for better control. Turn autoscale off MyScene.range = (13,13,13) #Set range of your scene to be 12 inches by 12 inches by 12 inches. measuringRod = cylinder( radius= .1,length=13, color=color.yellow, pos=(-5,0,0)) lengthLabel = label(pos=(0,5,0), text='Target Distance is: ', box=false, height=30) target=box(length=.1, width=10,height=5, pos=(-3,0,0)) #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=(-8.5,0,0)) #This object is the little square that is the back of the ultrasonic sensor myTube2=cylinder(color=color.blue, pos=(-8.5,0,-2.5), radius=1.5,length=2.5 ) #One of the 'tubes' in the front of the ultrasonic sensor myTube3=cylinder(color=color.blue, pos=(-8.5,0,2.5), radius=1.5,length=2.5 ) #Other tube #myBall=sphere(color=color.red, radius=.3) while (1==1): #Create a loop that continues to read and display the data rate(20)#Tell vpython to run this loop 20 times a second if (arduinoSerialData.inWaiting()>0): #Check to see if a data point is available on the serial port myData = arduinoSerialData.readline() #Read the distance measure as a string try: print myData #Print the measurement to confirm things are working #float(element) distance = float(myData) #convert reading to a floating point number measuringRod.length=distance #Change the length of your measuring rod to your last measurement target.pos=(-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 except ValueError: pass |
A picture means a thousand of words.
If you look carefully, you will notice that ruler measurement matches the reading from virtual world.
24
JUL
JUL
About the Author:
Beyond 8 hours - Computer, Sports, Family...