' =========================================================================
'
' File...... Cyclon-12.BS2
' Purpose... "Cylon" eyes (12 LEDs) control
' Author.... Jon Williams, EFX-TEK (www.efx-tek.com)
' E-mail.... jwilliams@efx-tek.com
' Started...
' Updated... 13 FEB 2007
'
' {$STAMP BS2}
' {$PBASIC 2.5}
'
' =========================================================================
' -----[ Program Description ]---------------------------------------------
' -----[ Revision History ]------------------------------------------------
' -----[ I/O Definitions ]-------------------------------------------------
Speed PIN 15 ' remove ULN, SETUP
Trigger PIN 14 ' SETUP = DN
Eyes VAR OUTS ' use P0..P11
' -----[ Constants ]-------------------------------------------------------
IsOn CON 1 ' for active-high trigger
IsOff CON 0
LastEye CON 11 ' P11 is last "eye" pin
MoveLf CON 0 ' direction flags
MoveRt CON 1
' -----[ Variables ]-------------------------------------------------------
direction VAR Bit ' eye movement direction
eyeNum VAR Nib ' which eye is lit
delay VAR Word ' to movement timing
' -----[ Initialization ]--------------------------------------------------
Reset:
OUTS = $0000 ' clear everything
DIRS = $0FFF ' P0..P11 are outputs
eyeNum = 0 ' reset cycle
direction = MoveLf
' -----[ Program Code ]----------------------------------------------------
Main:
DO WHILE (trigger = IsOn) ' run when triggered
HIGH eyeNum ' eye on
GOSUB Eye_Time ' hold a bit
LOW eyeNum ' eye off
IF (direction = MoveLf) THEN ' moving left?
IF (eyeNum < LastEye) THEN ' room to move?
eyeNum = eyeNum + 1 ' yes, move left
ELSE
eyeNum = LastEye - 1 ' no, go back
direction = MoveRt ' reverse direciton
ENDIF
ELSE
IF (eyeNum > 0) THEN ' room to move?
eyeNum = eyeNum - 1 ' yes, move right
ELSE
eyeNum = 1 ' no, start back up
direction = MoveLf ' reverse direction
ENDIF
ENDIF
LOOP
GOTO Reset
' -----[ Subroutines ]-----------------------------------------------------
' Reads Prop-Pot
' -- raw value (0 to ~615) is converted to 40 to ~100
Eye_Time:
HIGH Speed ' charge RC circuit
PAUSE 1
RCTIME Speed, 1, delay ' then read it
delay = delay / 10 + 40 ' make 40 to 100 ms
PAUSE delay
RETURN
' -------------------------------------------------------------------------
' -----[ EEPROM Data ]-----------------------------------------------------
Here's a cleaner version of that program. Using more sophisticated programming techniques, the code is quite a bit shorter. In this version the "eyes" complete an entire left-right sweep when the trigger is pressed.
' =========================================================================
'
' File...... Cyclon-12-v2.BS2
' Purpose... "Cylon" eyes (12 LEDs) control
' Author.... Jon Williams, EFX-TEK (www.efx-tek.com)
' E-mail.... jwilliams@efx-tek.com
' Started...
' Updated... 14 FEB 2007
'
' {$STAMP BS2}
' {$PBASIC 2.5}
'
' =========================================================================
' -----[ Program Description ]---------------------------------------------
' -----[ Revision History ]------------------------------------------------
' -----[ I/O Definitions ]-------------------------------------------------
Speed PIN 15 ' remove ULN, SETUP
Trigger PIN 14 ' SETUP = DN
Eyes VAR OUTS ' use P0..P11
' -----[ Constants ]-------------------------------------------------------
IsOn CON 1 ' for active-high trigger
IsOff CON 0
' -----[ Variables ]-------------------------------------------------------
delay VAR Word ' to movement timing
' -----[ Initialization ]--------------------------------------------------
Reset:
OUTS = $0000 ' clear everything
DIRS = $0FFF ' P0..P11 are outputs
Eyes = %000000000001 ' start on right
' -----[ Program Code ]----------------------------------------------------
Main:
DO WHILE Trigger = IsOff ' wait for trigger
LOOP
Sweep_Left:
DO
GOSUB Eye_Time
Eyes = Eyes << 1
LOOP UNTIL (Eyes = %100000000000)
Sweep_Right:
DO
GOSUB Eye_Time
Eyes = Eyes >> 1
LOOP UNTIL (Eyes = %000000000001)
GOTO Main
' -----[ Subroutines ]-----------------------------------------------------
' Reads Prop-Pot
' -- raw value (0 to ~615) is converted to 40 to ~100
Eye_Time:
HIGH Speed ' charge RC circuit
PAUSE 1
RCTIME Speed, 1, delay ' then read it
delay = delay / 10 + 40 ' make 40 to 100 ms
PAUSE delay
RETURN
' -------------------------------------------------------------------------
' -----[ EEPROM Data ]-----------------------------------------------------