View unanswered posts | View active topics It is currently Mon Dec 29, 2014 2:08 am






Reply to topic  [ 3 posts ] 
Trouble programming NXT Robot 
Author Message
Rookie

Joined: Sat Jan 18, 2014 6:59 pm
Posts: 2
Post 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);
}
}
}


Sat Jan 18, 2014 7:00 pm
Profile
Senior Roboticist
User avatar

Joined: Wed Sep 28, 2011 10:13 pm
Posts: 630
Location: If I told you, I'd have to kill you.
Post 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.

_________________
A.K.A. inxt-generation
Self-proclaimed genius, and future world dominator.
My Brickshelf Folder
"Don't they teach recreational mathematics anymore?" - The Tenth Doctor
Bow down to Nikola Tesla, King of the Geek Gods.


Sat Jan 18, 2014 7:07 pm
Profile WWW
Rookie

Joined: Sat Jan 18, 2014 6:59 pm
Posts: 2
Post 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.


Sat Jan 18, 2014 7:11 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


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.