Connect with us

Christmas Tree Project - PICAXE

  • Hello Everyone! :)

    Christmas time is coming! So I created a mini Christmas tree decoration to be a gift. It has four modes:

    • It plays a strip of lights rotating around the tree.
    • It alternately plays the lights flashing on it.
    • It selects a random (kind of) light to flash.
    • It selects a random (also kind of) strip and then plays the LEDs on it one by one.

    The modes are selectable by the switch 'S1' on the schematic. The switch 'S2' and the pot 'R11' are for adjusting the flash rate. You turn the pot and then press 'S2' and it loads a new flash rate to the current mode playing (it only adjusts the current mode playing). It uses the PICAXEs EEPROM, so once you turn it off it does not forget the mode it's playing or the flash rate for any of the modes (so when you start it back up it will be playing the same mode as when you turned it off). Here's the schematic:

    CTREE.png

    and Here's the code:
    Code:
    ;***********************PIN_LAYOUT************************;
    ;*  || Pin____Grid__No._GNo._||__Pin___Grid_No._GNo._||  *;
    ;*  || C.0 -> column 1   01   ||  B.3 -> row 1   05   || *;
    ;*  || C.1 -> column 2   02   ||  B.4 -> row 2   06   || *;
    ;*  || C.2 -> column 3   03   ||  B.5 -> row 3   07   || *;
    ;*  || C.4 -> column 4   04   ||                      || *;
    ;*********************OTHER_PIN_LAYOUT********************;
    ;*  || Pin____Function_______||__Pin____Function_____||  *;
    ;*  || C.3 -> Mode switch    || B.2  -> Set Speed    ||  *;
    ;*  || B.1 -> ADC            ||                      ||  *;
    ;*********************************************************;
    ;*_________________________Grid__________________________*;
    ;*           01    02    03    04                    *;
    ;*               LED---LED---LED---LED 05                *;
    ;*           LED---LED---LED---LED 06                *;
    ;*           LED---LED---LED---LED 07                *;
    ;*                                             *;
    ;* Logic 0 selects each channel                          *;
    ;*********************************************************;
    
    
    ;**************************MAIN***************************;
    ;* The main code block checks the mode switch and stores *;
    ;* its last state in the on board EPROM. It also executes*;
    ;* the correct pattern.                                  *;
    ;*********************************************************;
    main: ;this is to make all the variables equal zero (only done once)
        setfreq M32 ;set the PICAXE chip frequency to 32MHZ
        let b8=0
        let b7=0
        let b6=0
        let b5=0
        let b4=0
        let w13=0
        let w12=0 ;this is for the random command. So do NOT use b25 or b24
        let w11=0
        let w10=0 ;this is for the next random command. So do NOT use b20 or b21
        let w9=0
        let w8=0
        let w7=0
        let w6=0
        let bit0=0
        let bit1=0
        let bit2=0
        let bit3=0
        let bit4=0
        let bit5=0
        let bit6=0
        let bit7=0
        let bit8=0
        let bit9=0
        let bit10=0
        goto start
    start:
        do ;start endless loop
            if pinb.2=0 then ;if switch is pressed update playing time
                readadc b.1,b8
                let w6=b8*2
                if b7=0 then ;if playing the 1st pat
                    write 1, WORD w6 ;write the ADC value to space 1 and 2
                endif
                if b7=1 then ;if playing the 2nd pat
                    write 3, WORD w6 ;write the ADC value to space 3 and 4
                endif
                if b7=2 then ;if playing the 3rd pat
                    write 5, WORD w6 ;write the ADC value to space 5 and 6
                endif
                if b7=3 then ;if playing the 4th pat
                    write 7, WORD w6 ;write the ADC value to space 7 and 8
                endif
            endif
            high c.0,c.1,c.2,c.4,b.2,b.3,b.4,b.5 ;turn off all lights
            if pinc.3=0 AND bit10=0 then ;if the switch is pressed update pat playing
                inc b6 ;debounce with delay                  
                if b6=30 then
                    let bit10=1 ;switch has to go to high before this is repeated
                               inc b5
                    if b5=4 then ;all pats have been shown so reset
                        let b5=0
                    endif
                    write 255,b5 ;write the pat number to play to the EEPROM
                    let b6=0  
                endif
            endif
            if pinc.3=1 then ;this will complete making sure the switch goes back to
                let bit10=0         ;high before repeating that code above
            endif  
            read 255,b4 ;find out what pat to play
            if b4=0 then
                let b7=0 ;this tells the ADC thing above that pat1 is playing
                read 1, WORD w6 ;read the value from the 1st and 2nd space to w6
                gosub pat1
            endif
            if b4=1 then
                let b7=1 ;this tells the ADC thing above that pat2 is playing
                read 3, WORD w6 ;read the value from the 3rd and 4th space to w6
                gosub pat2
            endif
            if b4=2 then
                let b7=2 ;this tells the ADC thing above that pat3 is playing
                read 5, WORD w6 ;read the value from the 5th and 6th space to w6
                gosub pat3
            endif
            if b4=3 then
                let b7=3 ;this tells the ADC thing above that pat4 is playing
                read 7, WORD w6 ;read the value from the 7th and 8th space to w6
                gosub pat4
            endif
        loop until b4=10 ;loop forever
        goto start
    
    ;**************************PAT1***************************;
    ;* This pattern plays a rotating strip of lights         *;
    ;*********************************************************;
    pat1:
        if bit0=0 then ;get rid of back loop
            low c.0 ;turn on column 1
            low b.3,b.4,b.5 ;turn on row one,two,and three
            inc w13
            if w13<w6 then ;delay
                return
            endif
        endif
        let bit0=1;make sure it does not back loop
        if bit1=0 then ;get rid of back loop
            if w13=w6 then ;make sure not to reset count
                let w13=0
            endif
            low c.1 ;turn on column 2
            low b.3,b.4,b.5 ;turn on row one,two, and three
            inc w13
            if w13<w6 then ;delay
                return
            endif
        endif
        let bit1=1;make sure it does not back loop
        if bit2=0 then ;get rid of back loop
            if w13=w6 then ;make sure not to reset count
                let w13=0
            endif
            low c.2 ;turn on column 3
            low b.3,b.4,b.5 ;turn on row one,two, and three
            inc w13
            if w13<w6 then ;delay
                return
            endif
        endif
        let bit2=1;make sure it does not back loop
        if w13=w6 then ;make sure not to reset count
            let w13=0
        endif
        low c.4 ;turn on column 4
        low b.3,b.4,b.5 ;turn on row one,two, and three
        inc w13
        if w13<w6 then
            return
        endif
        let bit0=0
        let bit1=0
        let bit2=0
        let w13=0
    return
    
    ;**************************PAT2***************************;
    ;* This pattern plays stripes rotating around the tree   *;
    ;*********************************************************;
    pat2:
        if w13<w6 AND bit9=0 then
            inc w13
            if bit8=0 then
                low c.0,c.2 ;turn on solum 1 and 3
                low b.3,b.5 ;turn on row 1 and 3
                let bit8=1
                return
            endif
            low c.1,c.4 ;turn on column 2 and 4
            low b.4 ;turn on row 2
            let bit8=0
            return
        endif
        if bit9=0 then ;make sure not to reset second players count
            let w13=0
        endif
        let bit9=1
        if w13<w6 then
            inc w13
            if bit8=0 then
                low c.1,c.4 ;turn on column 2 and 4
                low b.3,b.5 ;turn on row 1 and 3
                let bit8=1
                return
            endif
            low c.0,c.2 ;turn on column 1 and 3
            low b.4 ;turn on row 2
            let bit8=0
            return
        endif
        let w13=0
        let bit9=0
        return
    
    ;**************************PAT3****************************;
    ;* This pattern randomly selects 1 LED to light           *;
    ;**********************************************************;
    pat3:
        random w8
        if bit4=0 then
            ;let w12=time ;seed random
            ;random w12 ;generate random number
            let w12=w8
            let bit4=1
        endif
        if w11<w6 then
            random w9
            inc w11
            if w12<=5461 then
                low c.0,b.3 ;select column 1 row 1
            endif
            if w12>5461 AND w12<=10922 then
                low c.0,b.4 ;select column 1 row 2
            endif
            if w12>10922 AND w12<=16383 then
                low c.0,b.5 ;select column 1 row 3
            endif
            if w12>16383 AND w12<=21844 then
                low c.1,b.3 ;select column 2 row 1
            endif
            if w12>21844 AND w12<=27305 then
                low c.1,b.4 ;select solum 2 row 2
            endif
            if w12>27305 AND w12<=32766 then
                low c.1,b.5 ;select column 2 row 3
            endif
            if w12>32766 AND w12<=38227 then
                low c.2,b.3 ;select column 3 row 1
            endif
            if w12>38227 AND w12<=43688 then
                low c.2,b.4 ;select column 3 row 2
            endif
            if w12>43688 AND w12<=49149 then
                low c.2,b.5 ;select column 3 row 3
            endif
            if w12>49149 AND w12<=54610 then
                low c.4,b.3 ;select column 4 row 1
            endif
            if w12>54610 AND w12<=60071 then
                low c.4,b.4 ;select column 4 row 2
            endif
            if w12>60071 AND w12<=65535 then
                low c.4,b.5 ;select column 4 row 3
            endif
            return
        endif
        let bit4=0 ;reset
        let w11=0
        return
    
    ;**************************PAT4***************************;
    ;* This pattern makes it look like water drops are       *;
    ;* falling off of the tree.                              *;
    ;*********************************************************;
    pat4:
        random w7 ;generate random number
        if bit5=0 then
            let w10=w7 ;let w10 = the random number once
            let bit5=1 ;make sure it only happens once per playing
        endif
        if w9<w6 AND bit6=0 then
            random w7
            inc w9
            if w10<=16383 then
                low c.0,b.3 ;select column 1 row 1
            endif
            if w10>16383 AND w10<=32766 then
                low c.1,b.3 ;select column 2 row 1
            endif
            if w10>32766 AND w10<=49149 then
                low c.2,b.3 ;select column 3 row 1
            endif
            if w10>49149 AND w10<=65535 then
                low c.4,b.3 ;select column 4 row 1
            endif
            return
        endif
        if bit6=0  then ;make sure it does not play the code above again
            let w9=0                                 
        endif
        let bit6=1 ;get rid of back loop
        if w9<w6 AND bit7=0 then
            random w7
            inc w9
            if w10<=16383 then
                low c.0,b.4 ;select column 1 row 2
            endif
            if w10>16383 AND w10<=32766 then
                low c.1,b.4 ;select column 2 row 2
            endif
            if w10>32766 AND w10<=49149 then
                low c.2,b.4 ;select column 3 row 2
            endif
            if w10>49149 AND w10<=65535 then
                low c.4,b.4 ;select column 4 row 2
            endif
            return
        endif
        if bit7=0 then
            let w9=0
        endif
        let bit7=1 ;get rid of back loop
        if w9<w6 then
            random w7
            inc w9
            if w10<=16383 then
                low c.0,b.5 ;select column 1 row 3
            endif
            if w10>16383 AND w10<=32766 then
                low c.1,b.5 ;select column 2 row 3
            endif
            if w10>32766 AND w10<=49149 then
                low c.2,b.5 ;select column 3 row 3
            endif
            if w10>49149 AND w10<=65535 then
                low c.4,b.5 ;select column 4 row 3
            endif
            return
        endif
        let w9=0   ;reset
        let bit5=0
        let bit6=0
        let bit7=0
        let w10=0
    return
    
    ;*********************************************************;
    ;*              <- END OF CTREE PROGRAM ->               *;
    ;*********************************************************;
    
    I used 12 SMD 0603 blue LEDs for it. They are each mounted on a header that was soldered to the tree before the LEDs where put on. The tree is made of paper clips soldered together. And it has one SMD led on the top that is always on (D13). Here's some photos of it (more in gallery):

    MODE1_10_23_14__1.JPG

    TREE_10_23_14__1.JPG

    CLOSEUP_10_23_14__1.JPG

    I cut down a perf board to fit the the bottom of the tree. Here's how I laid it out:

    BOARDFRONT_10_23_14__1.JPG BOARDBACK_10_23_14__1.JPG

    I hope this is useful to someone! :)
    Dan
Electronics Point Logo
Continue to site
Quote of the day

-