|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
I have a problem with my autonomous code. It involves the use of a lot of while() loops that the compiler says will not end. I heard you might have to put an extra condition in the loop such as "If the motor is not on..."(In code). Here's my code:
while (rightteeth < 4 || leftteeth < 4)/*if robot has not turned four gear tooth counts... */ { pwm13 = 0; /*rev left motor */ pwm15 = 255; /*forward right motor, hopefully will turn robot about ten deg's */ } The variables rightteeth and leftteeth are to count inputs from the hall effect sensors. Please someone help me! |
|
#2
|
||||
|
||||
|
Re: Program does not end
There is nothing actually wrong with the code you've shown so far. The most likely reason that the compiler is complaining is that before you enter the loop you set one or other of the variables to something less than 4. I didn't think that this compiler was that smart, but maybe it realized that you never alter the variables in the loop. However, if you are using interrupts, then this code should be fine. If you could post all your code, that would be extremely helpful.
It may also just be that the compiler is confused. If your code works (have you tested it?), then I'd just ignore the warning. |
|
#3
|
||||
|
||||
|
Re: Program does not end
Quote:
You are incorrectly using the while loop. Since you never increment rightteeth or leftteeth inside the loop, it will loop forever This will have the side effect of having your robot run at whatever speed it was running before the loop was hit. This is because the outputs are set at the bottom of the main loop, which will never get hit in your case. What you probably want to do is use an IF case. The main loop will take care of the iteration. |
|
#4
|
||||
|
||||
|
Re: Program does not end
While loops are tricky in robotics programming because only the code section within the loop runs until it is broken. Since the sensors are read outside of the loop itself, the loop is infinite.
Another implementation might be like this: 'pseudocode' if (l teeth < 4 || r teeth < 4) { run motors; } else { motors = 127; } The question I ask is if the code is meant to turn the robot, why would the left and right sides both count similarly? Are they on the same gear as a redundancy check? The real question I ask is are the sensors on the robot so one is on each side of the robot, or are they positioned differently? |
|
#5
|
|||
|
|||
|
Re: Program does not end
Quote:
The "if" version in the pseudocode is more in line with what you need. You'll probably also need a static variable to keep track of which state you are in - starting to turn, working on completing the turn, turn completed, starting to drive, driving, etc. BTW - The compiler is complaining specifically because you are using a loop whose control statement uses variables whose values aren't being changed in the loop. It is not aware that you might be changing them in an interrupt routine. (That is where they get updated, right?) |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| 2-week FIRST summer program @ WPI | ColleenShaver | General Forum | 20 | 31-05-2009 03:02 |
| Mulit-postioning with pnuematics this year? | KenWittlief | Pneumatics | 101 | 01-02-2005 21:54 |
| how to add steps in the provided encoder program for the edu | Zaramel2002 | Programming | 3 | 11-02-2004 08:35 |
| Fried program slots? | Jeff Waegelin | Programming | 18 | 19-03-2003 18:08 |
| Ahh! Program trick confusing! | archiver | 2001 | 9 | 24-06-2002 02:26 |