Code Behavior Gone Wild

BlitzMax Forums/BlitzMax Beginners Area/Code Behavior Gone Wild

Emmett(Posted 2008) [#1]
Background: Wheel on screen - starts turning with mouse click - automatic increase in speed during rotation.

The Action Code: r:+b

r is the variable representing degrees of circle
b is the variable used to speed up rotation of the wheel

The first mouse click on screen begins the wheel turning (b=.1). Everything is automated after that.
During rotation (clockwise) b is incremented .01 approximately 180 times so at the end of the first 360 degrees of rotation b equals about 1.8.
This goes on and on but when b equals about 61 the incrementing stops or at least the readout of b stops. The wheel keeps turning at the high rate of speed reached but appears almost to stand still but you know its still rotating by the slight wobble and the readout of r and turns (the variable to keep track of how many total revolutions).

It's quite a hoot to watch but when spin reaches 3minutes:15seconds variable b absolutely locks up at 61.0017624 even though the wheel keeps spinning as though nothing happened.

Can anyone shed some light on this code behavior?


tonyg(Posted 2008) [#2]
Without any code it's VERY difficult to have an idea.
A complete guess would be you're checking for the size of b in the range n.n0 to n.n9 when it doesn't work like that for floats.


Emmett(Posted 2008) [#3]
The only check for the size of b is: DrawText "Value b: " + b, 800,250
The start value of b is: b:+.1 all the automatic increments are b:+.01

Here are all the files to look at this program. The code is 91 lines including spaces between sections.
The program is not finished but it functions completely except for glitch mentioned above.
Don't laugh too hard at the amatureish code structure! Hey! it works! :}
Updated these files to reflect glitch repair as noted in below post!
(the images) http://localocate.biz/blitz/magimages.zip
(the bmx) http://localocate.biz/blitz/magmotor.bmx.zip
(the debug) http://localocate.biz/blitz/magmotor.debug.app.zip


Brucey(Posted 2008) [#4]
Hey! it works!

... apart from the glitch? :-)


Brucey(Posted 2008) [#5]
Very cool :-)

The reason it "locks up" is because you have gaps in your "if r> and r<" code... once it reaches the magic 60 degrees (and a bit) and it hits one of those gaps, it will simply rotate around the gap forever.
Since b is only updated when the the rotation falls outside of the gap, b isn't updated any more.

If click to the right enough times ones it "locks", it should eventually have enough degree change to flick back into working...

:o)


Brucey(Posted 2008) [#6]
around 66 is the value where it picks up again...


Jesse(Posted 2008) [#7]
I wonder what will happen when b reaches above 360? :)


Emmett(Posted 2008) [#8]
Brucey - I ran it for 32 minutes but the b variable remained at 61.0017624. How many minutes till yours restarted?
When I leave for work I will start the program and see what happens after several hours. ;)

Jesse - Good question - but the fun is in watching the rotation speed up from zero. When you can no longer see rotation - then whats the point. My reason for making this little program is to test a theory I have about creating a self powered motor. The red blinking ovals represent "battery power" turned on or off. The starter battery is one "D" cell. The shaft drives a minature generator which cranks out 12 watts at 6 volts. (bicycle light generator). When rotation reaches a certain rpm - the generator takes over supplying power and bypasses the "D" cell.

I still would like to know what makes the b variable stop in the first place?


Brucey(Posted 2008) [#9]
I ran it for 32 minutes but the b variable remained at 61.0017624.

And it would remain at that value forever if you left it.

As I explained earlier, once it falls into the "gap" that you've programmed into it, you are pretty much stuck... (you need to click to increase b until it reaches 66 or so.)

Why 66?

Because 5 * 66 = 330
If r>329 And r<361

330 just makes it into the 6th segment of your circle. After which time it will slowly increase again by itself

Anything between 61 and 66 will not be enough to get outside of the gap you've coded into lights().

Remember, 6 * 61 >= 360, which resets r back to 0.
r :+ 61 + 61 + 61 etc... >= 360, which resets r back to 0.

r :+ 61
If r>29 And r<61 .... FALSE (61)
r :+ 61
If r>89 And r<121 ... FALSE (122)
r :+ 61
If r>149 And r<181 ... FALSE (183)

etc...

:-)

Back to the drawing board, Emmett, TheBeginner.


Emmett(Posted 2008) [#10]
sorry - was a double post


Emmett(Posted 2008) [#11]
Brucey -
Thanks so much for the explanation. Add the bolded line where shown to fix it. It will now run non-stop to 360.
r:+b
If r+b=>360 b:+.01
If r=>360 reset
SetRotation(r)
DrawImage rotor, 506, 384
info
Flip
Now if someone would be so kind and explain how I could capture and display RPM?
I tried the following:
rpm = turns/(seconds) Mod 60 * 60
SetColor 255,0,0
DrawText "RPM: " + rpm,800,280
SetColor 255,255,255
But - it don't work. It coughs out an RPM but only at 60,120,180 etc.


xlsior(Posted 2008) [#12]
It coughs out an RPM but only at 60,120,180 etc.


I assume your turns and seconds are stored in integers -- if you divide these with each other, the results will also be in integers, and you'll end up with a lot of rounding errors. You then multiple these by 60 later on, hence the steps of 60 in the final result.

To fix, all you need to do is convert the variables to floats or doubles while doing your math, like this:

rpm:Int = double(turns)/(double(seconds)) Mod 60 * 60 
Print rpm


or alternate with the same result:

rpm:Int = Float(turns)/(Float(seconds)/60)
Print rpm