View unanswered posts | View active topics It is currently Sat Aug 02, 2014 4:36 am






Reply to topic  [ 7 posts ] 
Line Following Backwards? 
Author Message
Rookie

Joined: Wed May 23, 2007 9:40 pm
Posts: 6
Post 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!


Last edited by NXTProgrammer2851 on Thu May 24, 2007 3:51 pm, edited 1 time in total.



Wed May 23, 2007 9:57 pm
Profile
Rookie

Joined: Sun Apr 15, 2007 8:33 am
Posts: 40
Location: USA
Post 
why do you have twice the motorC ?

while(SensorValue(lightSensor) < 53)
{

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

}

_________________
SuntzuMaster- a French gentlemen in USA.


Thu May 24, 2007 2:41 pm
Profile
Rookie

Joined: Wed May 23, 2007 9:40 pm
Posts: 6
Post 
Heh......I don't know, mistake. Anyways I fixed it and got the same results. So why is it not working?


Thu May 24, 2007 3:50 pm
Profile
Novice

Joined: Sun Feb 04, 2007 12:48 am
Posts: 69
Location: Australia
Post 
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;


      }
   }
}


Fri May 25, 2007 9:54 am
Profile
Rookie

Joined: Wed May 23, 2007 9:40 pm
Posts: 6
Post 
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;


      }
   }
}


Fri May 25, 2007 11:14 am
Profile
Rookie

Joined: Mon May 21, 2007 11:48 am
Posts: 13
Location: Windows machine in a Linux lab
Post 
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


Fri May 25, 2007 12:06 pm
Profile
Rookie

Joined: Wed May 23, 2007 9:40 pm
Posts: 6
Post 
Yes, it is better to understand why. Thanks for the help!


Fri May 25, 2007 5:37 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 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.