ROBOTC.net forums
http://robotc.net/forums/

programming an ultrasonic sensor to be a motion detector
http://robotc.net/forums/viewtopic.php?f=11&t=4397
Page 1 of 1

Author:  vexmche123 [ Sun Mar 25, 2012 4:16 pm ]
Post subject:  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.

Author:  magicode [ Sun Mar 25, 2012 4:31 pm ]
Post subject:  Re: programming an ultrasonic sensor to be a motion detector

You can just use a variable to store a sensor reading.
Code:
int USVal = SensorValue[Ultrasonic];
Is that what you're asking?

Author:  vexmche123 [ Sun Mar 25, 2012 9:04 pm ]
Post subject:  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.

Author:  magicode [ Sun Mar 25, 2012 9:20 pm ]
Post subject:  Re: programming an ultrasonic sensor to be a motion detector

What you want is a while loop.
Code:
task main(){
  wait1Msec(1000); //The ultrasonic sensor needs some time to start reading a valid value. I'm not sure how much time, so I'll put 1 second to be safe.
  int firstVal = SensorValue[Ultrasonic];//stores the first value
  int threshold = 5; //It's normal to have some variation, so you should include a threshold that the value has to go past.

  while(abs(SensorValue[Ultrasonic] - firstVal) < threshold);
  //While loop that waits until the sensor value is more than 'threshold' away from the first value;

  motor[port5] = 40;
  wait1Msec(5000);
}

Author:  vexmche123 [ Thu Mar 29, 2012 2:03 pm ]
Post subject:  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?

Author:  magicode [ Thu Mar 29, 2012 2:11 pm ]
Post subject:  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?

Author:  vexmche123 [ Mon Apr 02, 2012 1:41 pm ]
Post subject:  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?

Author:  magicode [ Mon Apr 02, 2012 2:03 pm ]
Post subject:  Re: programming an ultrasonic sensor to be a motion detector

That doesn't seem possible. What happens when you try this:
Code:
task main(){
  wait1Msec(1000); //The ultrasonic sensor needs some time to start reading a valid value. I'm not sure how much time, so I'll put 1 second to be safe.
  int firstVal = SensorValue[Ultrasonic];//stores the first value
  int threshold = 5; //It's normal to have some variation, so you should include a threshold that the value has to go past.
  while(abs(SensorValue[Ultrasonic] - firstVal) < threshold){
    int deltaSensor = abs(SensorValue[Ultrasonic] - firstVal);
  }
  //While loop that waits until the sensor value is more than 'threshold' away from the first value;

  motor[port5] = 40;
  wait1Msec(5000);
}
what values are you getting for deltaSensor, and how does it compare to the threshold?

Author:  vexmche123 [ Sat Apr 07, 2012 5:03 pm ]
Post subject:  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!

Author:  magicode [ Sat Apr 07, 2012 7:37 pm ]
Post subject:  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?

Author:  vexmche123 [ Mon Apr 09, 2012 12:04 pm ]
Post subject:  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.

Author:  magicode [ Mon Apr 09, 2012 1:42 pm ]
Post subject:  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.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/