Hi all, I am making an electronic dice project which uses a tilt sensor to trigger random numbers on a 3d printed dice with LED's. The program works pretty well, but when I tilt the dice, it is supposed to initiate a counted loop form 1 to 20 and display 20 random numbers before it settles on a number. The problem, however, is that the FOR...NEXT loop never completes the 20 iteration cycle, and stops after random amounts of iterations. Here is the code. Any ideas why it is doing this would be appreciated.
Code:
setfreq m16 'set clock speed to 16MHz
hintsetup %00100010 'configure hardware interrupt on pin B.0
dirsC=%10111111 'set direction of portC pins (1=output, 0=input)
symbol switch_val=pinC.6 'tilt switch input
symbol rand_num=w0 'word variable to store random number (0-65536)
symbol dice_roll=w1 'word variable to store random number from 1 to 6
symbol pattern1=%00000001 'pattern1 'pattern2 'pattern3 'pattern4
symbol pattern2=%00000110 '######### ######### ######### #########
symbol pattern3=%00011000 '# # # # # # # # # #
symbol pattern4=%10100000 '# # # # # # # # # # #
symbol noLED=%00000000 '# # # # # # # # # #
'######### ######### ######### #########
settimer t1s_16 'activate internal timer
main:
do while pinc.6=0 'start a loop which iterates while switch is open (0)
timer=0 'reset timer to 0
for b20 = 1 to 20
random rand_num 'generate a random number from 1-65536 (2^16)
dice_roll= rand_num // 7 'scale rand_num value to a number between 1 and 6
gosub LED
pause 100
sertxd(#b20,cr,lf)
next b20
loop
'sertxd(#timer,cr,lf) 'debug to output timer value to terminal
if timer > 5 then 'if timer exceeds 5 seconds... (change this value to desired timout period in seconds)
pinsC=noLED 'turn of all LEDs
sleep 0 'put microcontroller into SLEEP mode to conserve power
end if
goto main 'return to beginning of main loop
LED: 'sub-procedure to check rand_num and turn on corresponding LEDs
if dice_roll=0 then
goto main
elseif dice_roll=1 then
pinsC=pattern1
elseif dice_roll=2 then
pinsC=pattern2
elseif dice_roll=3 then
pinsC=pattern1 OR pattern2
elseif dice_roll=4 then
pinsC=pattern2 OR pattern3
elseif dice_roll=5 then
pinsC=pattern1 OR pattern2 OR pattern3
elseif dice_roll=6 then
pinsC=pattern2 OR pattern3 OR pattern4
end if
return 'return to main loop and continue program execution after calling statement
PS: patterns in this post are not displaying correctly