;FORTUNE TELLER LEDS....... YES, NO, MAYBE, TIGGERED BY A SWITCH.... Basic standard code example for students to improve on ; Generates Random number of LED flashes in a continuous direction, ending on different colour LEDs ;Change Code ;Change StartUpRoutine ;Change FLASHTIME the length of time the LED flashes ;Change MAXIMUM_FLASHES ;Change CLOCKWISE Routine to go different order of LEDs at different speeds ;******Set Variables and Constants*************************************** symbol flashtime = 50 ;constant. length of flashtime in milliseconds symbol maximum_Flashes = 17 ;constant, maximum number of flashes symbol flashcount = b9 ;Variable, How many flashes before it stops? read 1,w1 ; read the latest value of W1 from last time the program was run and the power switched off ;*********************************************************************** StartUpRoutine: pulsout 0,20 ;Pulse the LED on pin0 for 20 milliseconds to show device has power ;***************MAIN LOOP******************************************************* main: low 0 ; turn LED on pin0 off to show fortune teller has made a decision moi: ; moi is a Latch Loop waiting for switch to be pressed Nap 4 ;Low power rest if pin3 = 0 then goto startFlash ;Check switch to see if it has been pressed goto moi startFlash: low 1,2,4 ;Turn ALL LEDs OFF FlashLED: gosub randomGenerator ;Generate a random number of LED flashes gosub clockwise ; Make LEDs flash in a clock wise direction gosub randomFlash ; Make LEDs flash a random number of times before choosing ONE goto main ;*************END OF MAIN LOOP********************************************************* randomGenerator: b10 = b10 + 1 ;KEEP changing the number of times w1 is randomised in each fortunetelling for b5 = 1 to b10 ;For Next Loop to randomise w1 a random number of times random w1 ; put random value into w1 next b5 write 1,w1 ;Write the value of w1 into memory to store for next time Device is switched on b10 = w1 // maximum_Flashes ;Modulus divide random w1 to give maximum number of flashes b10 = b10 min 2 ; set the minimum number of flashes flashCount = b10 ; assign B10 value to b9 FLASHCOUNT. Flashcount will be reduced each flash in CheckCount Function ;debug return ;***************************************************************** randomFlash: flash1: high 1 ;Turn ON pin 1 pause flashtime ;Pause for Constant FLASHTIME, set flashtime value at the top of the code gosub checkCount ;Subroutine CHECKCOUNT counts down from RANDOM GENERATOR number to zero. low 1 ;Turns OFF pin 1 flash2: high 2 pause flashtime gosub checkCount low 2 flash4: high 4 pause flashtime gosub checkCount low 4 goto randomFlash return ;*************************************************** CheckCount: debug if flashcount = 0 then goto main ;Checks FlashCount to see if it has reached Zero, Then send back to Main Loop dec flashcount ; take 1 away from flashcount, same as "flashCount = Flashcount - 1" return ;*************************************************** clockwise: ;Beginning routine flashing LEDs at diminishing speed in a clockwise direction high 0 ;Turns on LED at pin 0 to show Fortune Teller started for b5 = 50 to 250 step 50 ;In Diminishing Steps of 50 Milliseconds, flash each LED in clockwise direction high 2 ;Turn ON pin 2 pause b5 ;Pause for the current value of "B5" in the FOR NEXT LOOP low 2 ;Turn OFF pin 2 nap 3 ;short Low power sleep high 1 pause b5 low 1 nap 3 high 4 pause b5 low 4 nap 3 next return