View unanswered posts | View active topics It is currently Fri Jul 01, 2016 7:51 am

 Page 1 of 1 [ 7 posts ]
 Print view Previous topic | Next topic
Line Following Backwards?
Author Message
Rookie

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

{

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
Rookie

Joined: Sun Apr 15, 2007 8:33 am
Posts: 40
Location: USA

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
Rookie

Joined: Wed May 23, 2007 9:40 pm
Posts: 6

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
Novice

Joined: Sun Feb 04, 2007 12:48 am
Posts: 69
Location: Australia

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
Rookie

Joined: Wed May 23, 2007 9:40 pm
Posts: 6

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
Rookie

Joined: Mon May 21, 2007 11:48 am
Posts: 13
Location: Windows machine in a Linux lab

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
Rookie

Joined: Wed May 23, 2007 9:40 pm
Posts: 6

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

Fri May 25, 2007 5:37 pm
Display posts from previous:  Sort by
 Page 1 of 1 [ 7 posts ]

#### Who is online

Users browsing this forum: No registered users and 1 guest

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

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning