View unanswered posts | View active topics It is currently Sat Mar 23, 2019 8:45 pm






Reply to topic  [ 2 posts ] 
Lego NXT code problems 
Author Message
Rookie

Joined: Sun Jan 25, 2015 5:32 pm
Posts: 1
Post 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


Sun Jan 25, 2015 5:38 pm
Profile
Professor
User avatar

Joined: Sat Aug 31, 2013 9:15 am
Posts: 256
Post 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)

_________________
FTC Team 6100 Chariots of Fire - Programmer (2012-2013)
FTC Team 7468 Blue Chariots of Fire - Programmer (2013-2014)
FTC Team 7468 Blue Chariots of Fire - Mentor (2014-2015)
Check out our team website at http://bluechariotsoffire.com/.


Tue Jan 27, 2015 8:41 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.