|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||||
|
|||||
|
Crazy Loops
Hey!
I'm trying to create a simple program that spins my bot for 20 seconds and sees how many tenths of degrees my gyro picks up. I've been programming for a long time, and I can't figure this one out. I'm using easyC because the rest of our team started coding with that, and I guess I'm stuck with it for this year.... Any way, here is some sudo code. Can't copy paste (It's not on this machine, but this is basically what it does) Operator control { int gyro; int timer = 0; setgyrotype(4,125) initgyro(4) startgyro(4) starttimer(1) set_pwm(1,63) set_pwm(2,63) set_pwm(3,63) set_pwm(4,63) while(timer<20000) { gyro = getgyro(4) timer = GetTimer(1) PrintToScreen(gyro) PrintToScreen(timer) } set_pwm(1,127) set_pwm(2,127) set_pwm(3,127) set_pwm(4,127) PrintToScreen("done") } Yeah, Yeah... Syntax. Semicolons are overrated. So are proper parameters. It's SUDO CODE.... Anyway... Heres where it breaks down. It never leaves the while loop. The PrintToScreen SHOWS that the variable counts up to 20000, but continues to go around the loop after that. HOW DOES IT RUN THE LOOP WHEN THE CONDITION ISN'T MET??? Ive also tried an if statement inside the loop that checks the time and makes a variable 'done' 1, and I can show that at 20000, it becomes 1, but it still runs the while loop. So what gives? I just updated easyC to 3.0.1.1 I haven't tried this in the old version. Of course the same task in MPLab worked, and took half the time then clicking and dragging If statements.... Jacob |
|
#2
|
||||
|
||||
|
Re: Crazy Loops
well this might be wrong but isnt the operator control function called from one big while loop in main?
you might want to put all of that in a function then in opcon do a int i = 0; while(1) { if i ==0 function(); i++; } |
|
#3
|
||||
|
||||
|
Re: Crazy Loops
GetTimer returns an unsigned long. Your timer variable is an int.
Does that make a difference? |
|
#4
|
||||
|
||||
|
Re: Crazy Loops
While I have never worked in Easy C my best guess would be that it needs some sort of end while statement. While it doesn't seem likely cause C doesn't have a end while command I know it is in other languages so it's worth a shot.
Good luck getting that working. |
|
#5
|
|||||
|
|||||
|
Re: Crazy Loops
Whats weird, is that using the Wait block works. Just replacing the while loop with this block works. This is inconvenient though, because if you want to do anything with this time, you can't.
Perhaps it is the enclosing loop... I'll try this. Thanks, Jacob |
|
#6
|
|||||
|
|||||
|
Re: Crazy Loops
Do while 1, and have an if statement in your while loop, something like
if(!(timer < 20000)) { break; } to break out of the loop |
|
#7
|
|||||
|
|||||
|
Re: Crazy Loops
Quote:
Jacob |
|
#8
|
||||
|
||||
|
Re: Crazy Loops
If your 'done' variable was changing by itself, then perhaps the gyro call is assigning a variable of the wrong type ... if that variable is immediately adjacent in memory to the 'done' variable, the overflow would affect it.
This normally wouldn't be possible with a compiler like EasyC, which doesn't use pointers (well, it doesn't let US use pointers). Please post the exact code - C code preferrably. Mike BTW - PSEUDO-CODE, not SUDO (but probably you know that) |
|
#9
|
|||||
|
|||||
|
Re: Crazy Loops
Quote:
lol yeah... Sudo is sudo for pseudo... hence sudo (my aim at a joke... perhaps a few hours of sleep would be more humorous to my team members...) OK that was pretty lame. I'll go back to using pseudo. |
|
#10
|
||||
|
||||
|
Re: Crazy Loops
Looking only a pseodo-code can lead one to erroneous results.
I once worked on a project where every module was coded, and had to have COMPLETE pseudo-code in the file header as well. Endless hours were spent making sure the two lined up. Of course the programmers only looked at the code, knowing the pseudo-code would sometimes be wrong. Recently I had a variable that mysteriously changed values, in a loop like yours. This was using MPLAB. Turned out there were two variables with exactly the same name, one in local scope and one in global scope. The compiler never hinted of the conflict (well, that's my story and I'm sticking to it!). Of course the global one was being updated (by an external .C routine) and the local one was read by the printf ... since it was on the stack and there were interrupts in the system, it always contained garbage. |
|
#11
|
|||
|
|||
|
Re: Crazy Loops
The operator control routine in EasyC is run and run and run. If you exit, it will just call it again for you. To stop that behavior add something like the following to the end after the print done.
Code:
while (1) ; |
|
#12
|
||||
|
||||
|
Re: Crazy Loops
i dunno much about easyC, but in MPlab, while loops are never a good thing. They have a tendency to cause the motors not to get data. If that's not the case in EasyC, then ignore me. Otherwise, use if statements
|
|
#13
|
|||
|
|||
|
Re: Crazy Loops
Quote:
|
|
#14
|
|||||
|
|||||
|
Re: Crazy Loops
Mmmmm.... About the unsigned long...
It was correct in my real code... I remember changing that. Sorry my sudo wasn't updated. Whats weird is that it wasn't the timer. I used the timer to change another variable from a 0 to a 1, which was a change you could see with a print statement. The while loop simply said while(done ==0) { yadda yadda } and you would see done change from 0 to 1 without leaving the loop.... It's just bizzare. Jacob |
|
#15
|
|||
|
|||
|
Re: Crazy Loops
Just so you know, it's pseudo code.
![]() |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| While loops | Guy_E | VEX | 6 | 01-11-2006 22:04 |
| POLL: control loops | Rickertsen2 | Programming | 2 | 28-10-2004 20:32 |
| loops inside of subs??? | manodrum | Programming | 4 | 19-02-2003 20:05 |
| Solution to Timing Loops | Steven Carmain | Programming | 39 | 10-02-2003 13:33 |
| Programming Loops | Mike o. | Programming | 5 | 26-03-2002 11:24 |