Step 4Programming the PIC
The Picaxe I used was an 18X, this chip has 9 output pins and 5 input pins and can hold up to 600 lines of code. At this stage I only make use of the 6 of the output pins. 5 to run the LED's and 1 to turn on and off the sound module. At a later stage I could break the LED's up into 8 blocks for a higher resolution, remember my darlington driver can run up to 8 outputs at once. Or alternatively I could make the blade up of 4 blocks (Like another brand) and control each blade individually
All that said and done I can pretty much make the blades light up and down with about 20 lines of code. So what about the other 580? This seems a waste so I have added some extra modes 1. Normal mode, 2. Normal mode no sound 3. Night Rider mode 5. Pig mode. To change modes you hold down the push button for 5 seconds the saber flashes twice to signify program mode and depending on how many times you hit the push button with in 5 seconds dictates your new mode. If you don't press the button or press it too many times it reverts back to normal mode.
Here is the code that I have started with anyway.....
b5 = %00000001 ' Intiallise saber mode variable
b1 =0 ' Intialise mode change counter variable
b0 =0 ' Not sure why I did this
b2 = 1 ' Set the default mode to Saber normal
let pins = %00100000 ' Set pin 6 HIGH, LOW will turn on the sound module
Reset: ' resets all the pins after a mode change
let pins = %00100000
pause 100
goto loop
loop: ' main loop checking push button
if pin2 = 0 then mode ??? jump to flsh if pin0 is high
goto loop
mode:
if b1 > 5 then mode_change ' if the counter is at 5 goto mode change
pause 20
if pin2 = 1 then decide ' if the switch is up goto select a mode
if pin2 = 0 then count_t ' goto counter
count_t: ' this counter will count to five
pause 10
b1=b1+1
goto mode
mode_change: ' flashes saber twice to signify mode change
b2 = 0
let pins = %00111111
pause 300
let pins = %00100000
pause 300
let pins = %00111111
pause 300
let pins = %00100000
pause 300
count 2, 5000, b2 'counts bush button actions with in 5 seconds and stores this in b2
let pins = %00111111
pause 300
let pins = %00100000
pause 300
b1 = 0
goto loop
Decide: ' decision made depending on value in b2
b1=0
if b2 = 1 then saber
if b2 = 2 then silent_saber
if b2 = 3 then Knight_rider
if b2 = 4 then pigs
if b2 > 4 then saber
if b2 < 1 then saber
goto loop
saber: ' basic saber mode
b1 = 0
if b5 != %00000001 then p_down
if b5 = %00000001 then p_up
goto loop
p_up:
let pins = %00110000
pause 200
let pins = %00010000
pause 100
let pins = %00011000
pause 100
let pins = %00011100
pause 100
let pins = %00011110
pause 100
let pins = %00011111
b5 = %00000000
goto loop
p_down:
let pins = %00111111
pause 200
let pins = %00011111
pause 400
let pins = %00011111
pause 100
let pins = %00011110
pause 100
let pins = %00011100
pause 100
let pins = %00011000
pause 100
let pins = %00010000
pause 100
let pins = %00000000
b5 = %00000001
goto loop
silent_saber: ' says it all really
b1 = 0
if b5 != %00000001 then p_down_s
if b5 = %00000001 then p_up_s
goto loop
p_up_s:
let pins = %00110000
pause 100
let pins = %00111000
pause 100
let pins = %00111100
pause 100
let pins = %00111110
pause 100
let pins = %00111111
b5 = %00000000
goto loop
p_down_s:
let pins = %00111111
pause 100
let pins = %00111110
pause 100
let pins = %00111100
pause 100
let pins = %00111000
pause 100
let pins = %00110000
pause 100
let pins = %00100000
b5 = %00000001
goto loop
Knight_rider:
if pin2 = 0 then reset
let pins = %00100000
pause 100
let pins = %00110000
pause 100
let pins = %00101000
pause 100
let pins = %00100100
pause 100
if pin2 = 0 then reset
let pins = %00100010
pause 100
let pins = %00100001
pause 100
let pins = %00100010
if pin2 = 0 then reset
pause 100
let pins = %00100100
pause 100
let pins = %00101000
pause 100
let pins = %00110000
if pin2 = 0 then reset
goto Knight_rider
pigs:
if pin2 = 0 then reset
let pins = %00100000
pause 100
let pins = %00110000
pause 100
let pins = %00101000
pause 100
let pins = %00100100
pause 100
if pin2 = 0 then reset
let pins = %00100010
pause 100
let pins = %00100001
if pin2 = 0 then reset
goto pigs
Eventually I think it would be cool to use a microphone or something and use the saber as a VU meter - Bring on the tri colour LED's!
manymodes.wmv289 KB| « Previous Step | Download PDFView All Steps | Next Step » |
1
comment
|
Add Comment
|
![]() |
Add Comment
|







































