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

Trouble programming NXT Robot
http://robotc.net/forums/viewtopic.php?f=1&t=7732
Page 1 of 1

Author:  IndianCow [ Sat Jan 18, 2014 7:00 pm ]
Post subject:  Trouble programming NXT Robot

I am having trouble making my robot controlling my Mindstorms NXT robot with RobotC. I want my robot to be able to move forward on a table and when reaching the end, the ultrasonic sensor, which is facing down, will determine if it is on the edge by seeing how far the ground is. When the ultrasonic sensor finds that it is on the edge, the robot will move back from the edge, turn around, and go the other way.

Here is my code:

#pragma config(Sensor, S1, , sensorSONAR)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

task main()

{

int Ultrasonic;
Ultrasonic = SensorValue[S1];

while (true)
{
if (Ultrasonic >10)
{
motor[motorB] = -100;
motor[motorC] = -100;
wait1Msec(2000);

motor[motorB] = 100;
wait1Msec(2000);
}
if (Ultrasonic <= 10)
{
motor[motorB] = 50;
motor[motorC] = 50;
wait1Msec(5000);
}
}
}

Author:  NeXT-Generation [ Sat Jan 18, 2014 7:07 pm ]
Post subject:  Re: Trouble programming NXT Robot

Might I suggest:

Code:
#pragma config(Sensor, S1, , sensorSONAR)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

task main()
{
   int Ultrasonic;
   while (true)
   {
      Ultrasonic = SensorValue[S1];
      if (Ultrasonic >10)
      {
         motor[motorB] = -100;
         motor[motorC] = -100;
         wait1Msec(2000);
      }
      else if (Ultrasonic <= 10)
      {
         motor[motorB] = 50;
         motor[motorC] = 50;
      }
   }
}


Assigning the value of the Ultrasonic sensor is a good idea if you're going to be calling on it multiple times like that in the same loop. However, since you declared the variable and assigned the value outside of your loop, the variable value would never have changed. Although I can see how you'd think that, Ultrasonic = SensorValue[S1]; does not continually update the Ultrasonic value with the sensor value, only when you execute that command does it update the value. I also changed your motor control code a bit - change it back if you like, but this makes more sense to me with what you're trying to do.

Author:  IndianCow [ Sat Jan 18, 2014 7:11 pm ]
Post subject:  Re: Trouble programming NXT Robot

Thank you so much @NeXT-Generation! I just started programming in RobotC because I want to learn it before I go to high school so I can be on the robotics team.

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