Possible program flow glitch, program crashes randomly

moorea21

Senior Member
The attached program is a 'violin synthesiser', that uses 1 adc and 4 output pins (one for each 'string'.) The program cycles through each output to test for adc values that are only > 0 when a 'string' contacts a fret (ie a point along the resistor network in attached pic.) That adc value drives a calculation to find which eeprom address to look up, the lcd then displays the note value. It will eventually play the note via pwm rather than display it through lcd.

It works great for an indeterminate ammount of time, usually seconds, and then crashes, without any pattern to what behaviour preceded the crash.

I'm not sure the 'returns' and gotos' are the right way to control program flow here, I think they may be the cause of the crashes?
Apologies for not putting this small program into the body of the message; I havent worked out how to do that.
 

Attachments

lbenson

Senior Member
Haven't looked thoroughly, but this is a potential problem.
Code:
		do while b1 <255
			If b0 < b1 then lookUpData	'if theres a match, leave this loop
			b1 = b1 + 8
		loop
When you have gone through the loop 31 times, b1 is equal to 248. In real life, if you add 8 to that you get 256, but in PICAXE math, or any byte-limited math, 256 wraps back around to zero, so your loop never exits (unless your other condition is satisfied). PICAXE "b#" variables can only hold values between 0 and 255 and incrementing by eights starting from zero can never reach 255.

There are several ways to insert code in a box in a forum message. I'm stuck on the original way--copy it within the tags "[ code]" and "[ /code]", but without the spaces I have inserted after "[" so that that text wouldn't be interpreted as a tag in this post.
 

moorea21

Senior Member
Good point. The maximum value that is supposed to be seen will be 252, as b1 starts from 12.

So this:
Code:
do
	If b0 < b1 then lookUpData
	b1 = b1 + 8
loop while b1 <252
should solve that, although I can't think of any circumstance in which the adc would find itself with a value over 252, as the resistor network just doesn't have anywhere such a value could appear.

Oddly, having just run it, it seems to no longer crash. I wonder if there was just something electrical that I hadn't coded for, that allowed b1 to go from 252 to 4 etc.
 

lbenson

Senior Member
Hope it continues to work. Note that if 252 is a valid value to be assessed, your loop will exit when that value is reached after the increment by 8, without the "if" statement being executed.

If you change to "b1 <= 252", then you still have the problem of wrapping back around to a byte value, so the "while" condition will not be satisfied.

Maybe something like this:
Code:
do
	If b0 < b1 then lookUpData
        if b1 > 244 then exit           ' exit before overflow (if 252 is highest valid value)
	b1 = b1 + 8
loop
 

moorea21

Senior Member
The highest value that the adc can receive using the existing transistor network is 250, so, that being < 252, the if statemnt should always execute. If it crashes again at any point, I'll look into again though; I havent exactly tested it exhaustively yet...
 

lbenson

Senior Member
The highest value that the adc can receive using the existing transistor network is 250, so, that being < 252, the if statemnt should always execute.
But if b1 = 252, the do loop will exit, and the test for the b0 value of 250 will not occur for the condition of b1=252.
 

moorea21

Senior Member
Right again! I'm not the best coder in the world...
What it does do though is take the current value of b1 (252) and use it in 'lookUpData', so the code still works, although not quite the way I intended it to.
I prefer your way though, it works just as well (no crashing) so I'll use that instead.
Thanks
 
Top