I’m in the early phase of learning to program robots, and am trying to figure out why the first program below works, while the second one does not.
Here’s the first one, which works. When an object gets close enough to the sensor, the motor turns on for a short while, then stops. There’s one motor in motor port 2 and one sensor, plugged into digital ports 1 and 11.
[FONT=“Comic Sans MS”]
#include “Main.h”
void main ( void )
{
unsigned int objProximity;
StartUltrasonic (1, 11);
while(1)
{
objProximity = GetUltrasonic( 1, 11);
if ( objProximity < 12 )
{
SetMotor (2, 127 );
Wait ( 2000 );
break;
}
}
SetMotor ( 2, 0 );
}
[/FONT]
Now, using the exact same equipment setup, the next piece of code does not work. It never detects a change in proximity, i.e. a motion towards or away from the sensor. I get no complaints from EasyC regarding syntax during build & download, so I am guessing it’s a problem with my program logic. Any suggestions would be much appreciated.
[FONT=“Comic Sans MS”]
#include “Main.h”
void main ( void )
{
int newValue;
int oldValue;
StartUltrasonic (1, 11);
Wait ( 100 );
newValue = GetUltrasonic ( 1, 11 );
while(1)
{
oldValue = newValue;
Wait ( 100 );
newValue = GetUltrasonic( 1, 11);
if ( newValue != oldValue )
{
SetMotor (2, 127 );
break;
}
}
Wait ( 500 );
SetMotor ( 2, 0 );
}
[/FONT]
Thanks!