// // simion: // Version of the popular "Simon" memory game for // the Rasberry Ladder board // PROC ladderSetup // // 100 memory slots - ought to be enough.. // DIM memory(100) // CLS PRINT PRINT "== Simon Says - Remember the LEDs!" PRINT "== Memory Game for the Raspberry Ladder Board ==" PRINT "================================================" PRINT PRINT "The Ladder board will flash seuences of LEDs. You must try to" PRINT "remember the sequence and push the buttons next to the LEDs" PRINT "to confirm your memory. Each cycle, one more LED colour will" PRINT "be added to the sequence..." // CYCLE memCount = 0 userCount = 0 lost = FALSE PRINT PRINT "Push Button A (next to the Blue LEDs) to begin" PRINT // DigitalWrite (gMan, 0) CYCLE DigitalWrite (rMan, 1) WAIT (0.1) DigitalWrite (rMan, 0) WAIT (0.1) IF DigitalRead (buttonA) = 0 THEN BREAK REPEAT // WAIT (0.01) WHILE DigitalRead (buttonA) = 0 CYCLE // Wait for button up REPEAT DigitalWrite (gMan, 1) WAIT (0.5) // // This is it // CYCLE PROC addNewColour PROC playSequence PROC getInput WAIT (0.5) REPEAT UNTIL lost // We can never win... // PROC mainOff DigitalWrite (rMan, 1) PRINT PRINT "You ran out of memory!" PRINT "There were "; memCount; " changes in the sequence, you got: "; userCount PRINT "The sequence was:" PRINT PRINT " "; FOR i = 0 TO memCount - 1 CYCLE led = memory(i) IF led = 0 THEN TCOLOUR = Blue PRINT "B"; ENDIF IF led = 1 THEN TCOLOUR = Green PRINT "G"; ENDIF IF led = 2 THEN TCOLOUR = Yellow PRINT "Y"; ENDIF IF led = 3 THEN TCOLOUR = Red PRINT "R"; ENDIF TCOLOUR = White IF i = userCount - 1 THEN PRINT "<"; ELSE PRINT " "; ENDIF REPEAT PRINT PRINT REPEAT END // // getInput: // Read the buttons, but if we wait too long, or push the wrong one, // then it's game over! // DEF PROC getInput userCount = 0 WHILE userCount < memCount CYCLE button = FN getButton IF button = -1 THEN // Took too long lost = TRUE BREAK ENDIF // // was it the right one? // IF button <> memory(userCount) THEN PROC lightLED(memory(userCount)) lost = TRUE BREAK ENDIF // // Light up the light for as long as the button is pushed // PROC lightLED(button) WHILE DigitalRead (buttonA) + DigitalRead (buttonB) + DigitalRead (buttonC) + DigitalRead (buttonD) <> 4 CYCLE REPEAT PROC mainOff WAIT (0.05) userCount = userCount + 1 REPEAT // ENDPROC // // getButton: // function to return the button pressed - as 0, 1, 2 or 3 // or return -1 if we don't push a button inside 2 seconds // DEF FN getButton LOCAL start, button etime = TIME + 2000 button = -1 CYCLE IF DigitalRead (buttonA) = 0 THEN button = 0 IF DigitalRead (buttonB) = 0 THEN button = 1 IF DigitalRead (buttonC) = 0 THEN button = 2 IF DigitalRead (buttonD) = 0 THEN button = 3 REPEAT UNTIL (TIME > etime) OR (button <> -1) = button // // // addNewColour // Add a new colour to the sequence // DEF PROC addNewColour memory(memCount) = RND (4) memCount = memCount + 1 ENDPROC // // playSequence: // Play back the sequence stored so-far // DEF PROC playSequence LOCAL i FOR i = 0 TO memCount - 1 CYCLE PROC display(memory(i)) REPEAT ENDPROC // // display: // Light up an LED pair for 1/5 a second // DEF PROC display(led) PROC lightLED(led) WAIT (0.4) PROC mainOff WAIT (0.2) ENDPROC // // lightLED // Light up the colour pair // DEF PROC lightLED(ledCol) SWITCH (ledCol) CASE 0 DigitalWrite (ledBlue1, 1) DigitalWrite (ledBlue2, 1) ENDCASE CASE 1 DigitalWrite (ledGreen1, 1) DigitalWrite (ledGreen2, 1) ENDCASE CASE 2 DigitalWrite (ledYellow1, 1) DigitalWrite (ledYellow2, 1) ENDCASE CASE 3 DigitalWrite (ledRed1, 1) DigitalWrite (ledRed2, 1) ENDCASE ENDSWITCH ENDPROC // // mainOff // Turn the main LEDs off // DEF PROC mainOff LOCAL i FOR i = 0 TO 7 CYCLE DigitalWrite (i, 0) REPEAT ENDPROC // // ladderSetup: // This is the setup procedure. We initialise the various // pins into the correct modes and extinbuish all the LEDs // DEF PROC ladderSetup LOCAL i FOR i = 0 TO 9 CYCLE PinMode (i, 1) // Output DigitalWrite (i, 0) // Off REPEAT FOR i = 10 TO 13 CYCLE PinMode (i, 0) // Input PullUpDn (i, 2) // Activate internal pull-up REPEAT // // Make some globals // ledRed1 = 0 ledRed2 = 1 ledYellow1 = 2 ledYellow2 = 3 ledGreen1 = 4 ledGreen2 = 5 ledBlue1 = 6 ledBlue2 = 7 gMan = 8 rMan = 9 // buttonA = 11 buttonB = 10 buttonC = 12 buttonD = 13 // ENDPROC