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

Line Following Backwards?
http://www.robotc.net/forums/viewtopic.php?f=1&t=178
Page 1 of 1

Author:  NXTProgrammer2851 [ Wed May 23, 2007 9:57 pm ]
Post subject:  Line Following Backwards?

Hello everyone, this is my first post! I need some help programming my NXT. And once I found out about RobotC, I was new to C programming, so I don't know much, and this might be easy but we'll see.

Ok so I did everything so it would follow the line(used the tutorial thing), but now I made it so that if something is within 15 centimeters it would basically do the line following but it would go backwords. So I wrote everything and when I ran it, I put my hand in front of the Ultrasonic Sensor, and it just stopped.

This is everything I wrote, please tell me what I did wrong or whatever.

task main()
{

while(SensorValue(ultrasonicSensor) > 15)
{

while(SensorValue(lightSensor) < 53)
{

motor[motorC] = 0;
motor[motorB] = 80;

}

while(SensorValue(lightSensor) > 53)

{

motor[motorC] = 80;
motor[motorB] = 0;

}

}

while(SensorValue(lightSensor) < 53)
{

motor[motorC] = 0;
motor[motorB] = -80;

}

while(SensorValue(lightSensor) > 53)
{

motor[motorC] = -80;
motor[motorB] = 0;



}
}






Thanks so much!

Author:  SuntzuMaster [ Thu May 24, 2007 2:41 pm ]
Post subject: 

why do you have twice the motorC ?

while(SensorValue(lightSensor) < 53)
{

motor[motorC] = 0;
motor[motorC] = -80;

}

Author:  NXTProgrammer2851 [ Thu May 24, 2007 3:50 pm ]
Post subject: 

Heh......I don't know, mistake. Anyways I fixed it and got the same results. So why is it not working?

Author:  JamesD [ Fri May 25, 2007 9:54 am ]
Post subject: 

I hope I've misread the tone of your last email. You seem a bit short for someone who is asking for help.

Anyway, it looks like your code is running through the second half of your code once then exiting the program (all happening within an instant) as soon as the ultrasonic reads less than 15cm. Try:

Code:
task main()
{
   while (true)
   {

      while(SensorValue(ultrasonicSensor) > 15)
      {

         while(SensorValue(lightSensor) < 53)
         {

            motor[motorC] = 0;
            motor[motorB] = 80;

         }

         while(SensorValue(lightSensor) > 53)

         {

            motor[motorC] = 80;
            motor[motorB] = 0;

         }

      }

      while(SensorValue(lightSensor) < 53)
      {

         motor[motorC] = 0;
         motor[motorB] = -80;

      }

      while(SensorValue(lightSensor) > 53)
      {

         motor[motorC] = -80;
         motor[motorB] = 0;


      }
   }
}

Author:  NXTProgrammer2851 [ Fri May 25, 2007 11:14 am ]
Post subject: 

Ok...what did you mean by "You seem a bit short for someone who is asking for help. " ? And so was I just missing the while(true)? If that was it, then io was wondering why it was moving forward normally. Anyways, I copied your code and it did weird stuff after my hand was in front of it. So thats when I realized I needed to put it so instead of one motor just going backwards instead of forwards, the other motor had to do backwards. You might be confused, but here is what I put, just had to change one thing that you put. Just look at where it goes backwards. And sorry forgot I needed to put it in a code box on my first post. This is what made it work, thanks for the help!

Edit: I also changed the distance to 25 because I couldn't keep my hand along with it, and if my hand got out of range and then back in range, it would mess up because it's not on the line and its weird. And if it gets to a turn, it doesn't take a turn it gets going backwards with no turn at all and then it keeps seeing the white on the test pad so it just keeps moving one motor. But oh well, it was just to see how well it would work.

Code:
task main()
{
   while (true)
   {

      while(SensorValue(ultrasonicSensor) > 25)
      {

         while(SensorValue(lightSensor) < 53)
         {

            motor[motorC] = 0;
            motor[motorB] = 80;

         }

         while(SensorValue(lightSensor) > 53)

         {

            motor[motorC] = 80;
            motor[motorB] = 0;

         }

      }

      while(SensorValue(lightSensor) < 53)
      {

         motor[motorC] = -80;
         motor[motorB] = 0;

      }

      while(SensorValue(lightSensor) > 53)
      {

         motor[motorC] = 0;
         motor[motorB] = -80;


      }
   }
}

Author:  AThomas [ Fri May 25, 2007 12:06 pm ]
Post subject: 

It worked correctly moving forward before because the while(SensorValue(ultrasonicSensor) > 15) was functioning like the usual while(true) statement at the beginning of any set of steps that are supposed to repeat forever. It would run until the test condition of the while statement became false. This means that your forward moving code would repeat until the condition was false, which meant that the robot would continue to go forward (looping through your code) until it had sensed an object within 15 centimeters. At that point nothing was there to make it loop through the backward code. That's why you needed to have the while(true) statement or a timer to make it continue to go backwards.

I hope this helps some. If not now, then in the future. It may be better to know why you do something than to just be told to do it.

-Mandy

Author:  NXTProgrammer2851 [ Fri May 25, 2007 5:37 pm ]
Post subject: 

Yes, it is better to understand why. Thanks for the help!

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