Author |
Message |
vexmche123
Rookie
Joined: Sun Jul 10, 2011 3:34 pm Posts: 30
|
 programming an ultrasonic sensor to be a motion detector
For a project, I wanted to make a motion detector with my VEX robot using an Ultrasonic sensor. The robot would sit still and when an object passes by it, the values will jump, causing a motor to move.
The way I have gone about doing this is trying many ways to have the sensor read a value while sitting still, then store that value. I then added in an if statement, trying to say if the initial value doesn’t equal the current reading (while something is passing by), then move the motor. My programming skills aren’t too advanced, but I understand a lot of c programming. So I’m not sure if this is even possible, but if anyone can show me at least how to store an initial reading of the sensor, that would be great. Thanks.
|
Sun Mar 25, 2012 4:16 pm |
|
 |
magicode
Moderator
Joined: Tue Sep 14, 2010 9:19 pm Posts: 496
|
 Re: programming an ultrasonic sensor to be a motion detector
You can just use a variable to store a sensor reading. Is that what you're asking?
_________________ sudo rm -rf /
|
Sun Mar 25, 2012 4:31 pm |
|
 |
vexmche123
Rookie
Joined: Sun Jul 10, 2011 3:34 pm Posts: 30
|
 Re: programming an ultrasonic sensor to be a motion detector
Yes, thank you it does help, but how can I say (in code), that if the value changes, then to move a motor? Basically what I am trying to do is have a robot sit still in a room and when something moves in front of it, meaning the ultra sonic had a jump or drop in distance reading, to do something. The distance it is reading is unknown every time, allowing me to put it anywhere in a room. Here's the code I came up with:
#pragma config(Sensor, in1, ultra, sensorSONAR, int1) int change; task main() { change=SensorValue(ultra); if(SensorValue(ultra)!=change) { motor[port5]=40; wait1Msec(5000); }//end if }//end main
I know that its not working because I set the ultrasonic sensor value equal to change and the if statement would never be true because of this, but what I was hoping to do was to say that if the SensorValue(ultra) changed because a different distance was being read, that the motor would move. Does this make more sense with what I am trying to do? This seems like it should be pretty simple.
|
Sun Mar 25, 2012 9:04 pm |
|
 |
magicode
Moderator
Joined: Tue Sep 14, 2010 9:19 pm Posts: 496
|
 Re: programming an ultrasonic sensor to be a motion detector
What you want is a while loop.
_________________ sudo rm -rf /
|
Sun Mar 25, 2012 9:20 pm |
|
 |
vexmche123
Rookie
Joined: Sun Jul 10, 2011 3:34 pm Posts: 30
|
 Re: programming an ultrasonic sensor to be a motion detector
I have tried to get this to work several times. I can't get it to work. To test it, I changed the threshold value above and below the distance I knew it was reading, and I couldn’t get it to work. I think I understand the logic in the program, but it's just not working for me. Is there something else I can do? Or another method?
|
Thu Mar 29, 2012 2:03 pm |
|
 |
magicode
Moderator
Joined: Tue Sep 14, 2010 9:19 pm Posts: 496
|
 Re: programming an ultrasonic sensor to be a motion detector
What do you mean that it doesn't work? What exactly happens? Have you tried looking in the debugger's sensor window to see what values you're getting at what parts in the program?
_________________ sudo rm -rf /
|
Thu Mar 29, 2012 2:11 pm |
|
 |
vexmche123
Rookie
Joined: Sun Jul 10, 2011 3:34 pm Posts: 30
|
 Re: programming an ultrasonic sensor to be a motion detector
The robot is moving regardless of the threshold value and the sensor value reading. If the reading is larger or smaller than the threshold, it doesn’t matter, because after looking at it for a while, I noticed that when you say. (SensorValue[Ultrasonic] - firstVal), it will always equal zero. So regardless of the threshold value, the statement will always be true. Any ideas?
|
Mon Apr 02, 2012 1:41 pm |
|
 |
magicode
Moderator
Joined: Tue Sep 14, 2010 9:19 pm Posts: 496
|
 Re: programming an ultrasonic sensor to be a motion detector
That doesn't seem possible. What happens when you try this:
_________________ sudo rm -rf /
|
Mon Apr 02, 2012 2:03 pm |
|
 |
vexmche123
Rookie
Joined: Sun Jul 10, 2011 3:34 pm Posts: 30
|
 Re: programming an ultrasonic sensor to be a motion detector
Ok, so I finally got it to work. I had to do some modifications to your code because I couldn't understand some of it, but here's the code that does work...
#pragma config(Sensor, in1, ultra, sensorSONAR, int1)
task main() {
while(true) {
int firstVal=SensorValue(ultra);//stores the first value
if(SensorValue(ultra) < firstVal || SensorValue(ultra) > firstVal) //move forward for one second { motor[port5]=20; wait1Msec(1000); }//end if if(SensorValue(ultra) == firstVal) { motor[port5]=0; }//end if }//end while
}//end main
Thanks for your help!
|
Sat Apr 07, 2012 5:03 pm |
|
 |
magicode
Moderator
Joined: Tue Sep 14, 2010 9:19 pm Posts: 496
|
 Re: programming an ultrasonic sensor to be a motion detector
You're welcome. I'm really surprised that code works though. The sensor value has to change exactly in between the two lines where you assign firstVal and check it. Are you sure that this is the code that you're running?
_________________ sudo rm -rf /
|
Sat Apr 07, 2012 7:37 pm |
|
 |
vexmche123
Rookie
Joined: Sun Jul 10, 2011 3:34 pm Posts: 30
|
 Re: programming an ultrasonic sensor to be a motion detector
It was a little picky and didn't work all the time, because the values jump a little if it is not reading a whole number, but I just needed to get the basic program working.
I'm not sure exactly what you mean when you say you're surprised the program works. It checks if the value gets bigger or smaller than what it was reading, to move. Why are you led to believe it doesn't work? That too may be why its only works sometimes.
|
Mon Apr 09, 2012 12:04 pm |
|
 |
magicode
Moderator
Joined: Tue Sep 14, 2010 9:19 pm Posts: 496
|
 Re: programming an ultrasonic sensor to be a motion detector
I'm surprised because the only way I see the program working is if the value changes between two specific lines, which seems improbable. Perhaps it works sometimes if the program is small.
_________________ sudo rm -rf /
|
Mon Apr 09, 2012 1:42 pm |
|
|