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

Lego NXT code problems
http://robotc.net/forums/viewtopic.php?f=1&t=10352
Page 1 of 1

Author:  jbjosh100 [ Sun Jan 25, 2015 5:38 pm ]
Post subject:  Lego NXT code problems

Hi All,

I'm relatively new to the mindstorms project, however i've been working on a robot, coding it using C. I've been trying to get my robot to move around on it's own, avoiding walls. The other thing i'm trying to get it to do is that: whenever the two light sensors encounter a light, the robot will act "timid" and back away, continuing on the regular routine of moving around avoiding obstacles. Currently when I start the code, the bot will literally go around in circles/not do anything at all. Here is the code (what's wrong with it):

#pragma config(Sensor, S1, Rightlight, sensorLightInactive)
#pragma config(Sensor, S2, Sonar, sensorSONAR)
#pragma config(Sensor, S4, Leftlight, sensorLightInactive)
#pragma config(Motor, motorA, Rightwheel, tmotorNormal, PIDControl, reversed, encoder)
#pragma config(Motor, motorC, Leftwheel, tmotorNormal, PIDControl, reversed, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

bool run=false;
//need to define program for "run", not sure how to embed that into program//

task main()
{
while(1==1)
{
if (SensorValue(sonar)<30)
{
motor[motorA]=0;
motor[motorC]=0;
wait1Msec(500);
motor[motorA]=-75;
motor[motorC]=-10;
wait1Msec(750);
}

}

while(run == false); /* set run=false and do 3a until either
sensor has a value>60 */
{
motor[rightwheel]=60-SensorValue(rightlight);
motor[leftwheel]=60-SensorValue(leftlight);
if (SensorValue(rightlight)>60){
run=true;}
if (SensorValue(leftlight)>60)
{run=true;}
}

while(run==true); /* when run is true (the light intensity was >60)
start 2a to move away from the light */
{
motor[rightwheel]=60-SensorValue(leftlight);
motor[leftwheel]=60-SensorValue(rightlight);
if (SensorValue(rightlight)<30)
{run=false;}
if(SensorValue(leftlight)<30)
{run=false;}
}
}
}

Thanks

Author:  BurningLights [ Tue Jan 27, 2015 8:41 pm ]
Post subject:  Re: Lego NXT code problems

One problem that I immediately spot in your code is that it doesn't start off with something to make it go straight. Here's what's happening in the first part of your code:
Code:
while(1==1) // This is an infinite loop. In other words, none of the code that comes after this will EVER run....
 {
// Now the robot does not move at all until something is within 30 centimeters of the sonar sensor. And when that happens....
 if (SensorValue(sonar)<30)
 {
 // ...it stops for half a second, and then starts driving in a circular fashion, and will continue to do so until something is once again
 // within 30 centimeters of the sonar sensor.
 motor[motorA]=0;
 motor[motorC]=0;
 wait1Msec(500);
 motor[motorA]=-75;
 motor[motorC]=-10;
 wait1Msec(750);
 }


And by the way, once you get those problems fixed, there's also a problem with your other two while loops. Namely, they should not have semicolons after them. So,
Code:
while(run == false);
should be
Code:
while(run == false)

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