Can someone help me make a code for this:


Closed Thread
Results 1 to 10 of 10

Hybrid View

  1. #1
    Join Date
    Apr 2009
    Posts
    24

    Thumbs up Can someone help me make a code for this:




    Can someone teach/help me make a code for a random led effects
    the code supposed to be like this:

    light 3 leds every 5 seconds in a random way which means every 5 seconds different sets of 3 should light

    TIA!

  2. #2
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    699


    Did you find this post helpful? Yes | No

    Default

    Joseph,

    Try to write the program by yourself and then if you have questions post your code and we will try to help you. I think that you should try to do it first. There are many books and tutorials out there. The following book, I use it as a guide and it has a few examples with LEDs very similar to what you want.

    http://www.amazon.com/PIC-Basic-Proj...0150464&sr=8-2

  3. #3
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    This one may work for you.
    Just work on this sample code.



    Code:
    <font color="#000080"><i>'@ DEVICE PIC16F628A, INTRC_OSC_NOCLKOUT, WDT_OFF, PWRT_OFF, MCLR_OFF,PROTECT_ON,CPD_ON, BOD_OFF, LVP_OFF
    
    </i><b>DEFINE </b></font>OSC <font color="#FF0000">4
    
    </font>CMCON = <font color="#FF0000">7
    </font><font color="#000080"><b>PAUSE </b></font><font color="#FF0000">1
    </font>VRCON = <font color="#FF0000">0
    </font><font color="#000080"><b>PAUSE </b></font><font color="#FF0000">1
    
    </font>TRISA = <font color="#FF0000">0
    </font>TRISB = <font color="#FF0000">0
    </font>PORTA = <font color="#FF0000">0
    </font>PORTB = <font color="#FF0000">0
    
    </font>OPTION_REG.<font color="#FF0000">5 </font>= <font color="#FF0000">0 </font><font color="#000080"><i>' Internal clk.
    </i></font>OPTION_REG.<font color="#FF0000">3 </font>= <font color="#FF0000">0 </font><font color="#000080"><i>' Prescaler assigned to TMR0.
    </i></font>OPTION_REG.<font color="#FF0000">2 </font>= <font color="#FF0000">0 </font><font color="#000080"><i>' 
    </i></font>OPTION_REG.<font color="#FF0000">1 </font>= <font color="#FF0000">0 </font><font color="#000080"><i>' 
    </i></font>OPTION_REG.<font color="#FF0000">0 </font>= <font color="#FF0000">0 </font><font color="#000080"><i>' 1:1
    
    </i><b>PAUSE </b></font><font color="#FF0000">10
    
    </font>Led1 <font color="#000080"><b>VAR </b></font>PORTA.<font color="#FF0000">0    </font><font color="#000080"><i>'Assign leds. 
    </i></font>Led2 <font color="#000080"><b>VAR </b></font>PORTA.<font color="#FF0000">1
    </font>Led3 <font color="#000080"><b>VAR </b></font>PORTA.<font color="#FF0000">2
    </font>Led4 <font color="#000080"><b>VAR </b></font>PORTA.<font color="#FF0000">3
    </font>Led5 <font color="#000080"><b>VAR </b></font>PORTA.<font color="#FF0000">4
    
    </font>Led6 <font color="#000080"><b>VAR </b></font>PORTB.<font color="#FF0000">0
    </font>Led7 <font color="#000080"><b>VAR </b></font>PORTB.<font color="#FF0000">1
    </font>Led8 <font color="#000080"><b>VAR </b></font>PORTB.<font color="#FF0000">2
    </font>Led9 <font color="#000080"><b>VAR </b></font>PORTB.<font color="#FF0000">3
    </font>Led10 <font color="#000080"><b>VAR </b></font>PORTB.<font color="#FF0000">4
    </font>Led11 <font color="#000080"><b>VAR </b></font>PORTB.<font color="#FF0000">5
    </font>Led12 <font color="#000080"><b>VAR </b></font>PORTB.<font color="#FF0000">6
    </font>Led13 <font color="#000080"><b>VAR </b></font>PORTB.<font color="#FF0000">7
    </font>Led14 <font color="#000080"><b>VAR </b></font>PORTA.<font color="#FF0000">7
    </font>Led15 <font color="#000080"><b>VAR </b></font>PORTA.<font color="#FF0000">6
    
    </font>Index <font color="#000080"><b>VAR BYTE
    </b></font>LookForThree <font color="#000080"><b>VAR BYTE
    </b></font>Val    <font color="#000080"><b>VAR WORD
    </b></font>Result <font color="#000080"><b>VAR BYTE
    PAUSE </b></font><font color="#FF0000">100
    
    
    </font>Start:
    
    
        LookForThree = <font color="#FF0000">0
        </font><font color="#000080"><b>WHILE </b></font>LookForThree &lt;&gt; <font color="#FF0000">3   </font><font color="#000080"><i>' Look for three &quot;SET&quot;s.
            </i><b>RANDOM </b></font>Val            <font color="#000080"><i>' Get a random value.
            </i><b>PAUSEUS </b></font>TMR0          <font color="#000080"><i>' To get a good random number, have a changing value using TMR0 or TMR1.
        
            </i></font>LookForThree = <font color="#FF0000">0
            </font><font color="#000080"><b>FOR </b></font>Index = <font color="#FF0000">0 </font><font color="#000080"><b>TO </b></font><font color="#FF0000">15   </font><font color="#000080"><i>' Scan through the bits.
                </i><b>IF </b></font>Val.<font color="#FF0000">0</font>[Index] = <font color="#FF0000">1 </font><font color="#000080"><b>THEN </b></font>LookForThree = LookForThree + <font color="#FF0000">1  </font><font color="#000080"><i>'Count &quot;SET&quot;s.
            </i><b>NEXT </b></font>Index
        
        <font color="#000080"><b>WEND
    
        GOSUB </b></font>SetPins
        <font color="#000080"><b>PAUSE </b></font><font color="#FF0000">5000
    
        </font><font color="#000080"><b>GOTO </b></font>Start
    
    SetPins:
    
        Led1  = Val.<font color="#FF0000">0
        </font>Led2  = Val.<font color="#FF0000">1
        </font>Led3  = Val.<font color="#FF0000">2
        </font>Led4  = Val.<font color="#FF0000">3
        
        </font>Led5  = Val.<font color="#FF0000">4
        </font>Led6  = Val.<font color="#FF0000">5
        </font>Led7  = Val.<font color="#FF0000">6
        </font>Led8  = Val.<font color="#FF0000">7
        </font>Led9  = Val.<font color="#FF0000">8
        </font>Led10 = Val.<font color="#FF0000">9
        </font>Led11 = Val.<font color="#FF0000">10
        </font>Led12 = Val.<font color="#FF0000">11
        </font>Led13 = Val.<font color="#FF0000">12
        </font>Led14 = Val.<font color="#FF0000">13
        </font>Led15 = Val.<font color="#FF0000">14            
        </font><font color="#000080"><b>RETURN
    
    
    END
    
    </b></font>
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  4. #4
    Join Date
    Apr 2009
    Posts
    24


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer View Post
    This one may work for you.
    Just work on this sample code.



    Code:
    <font color="#000080"><i>'@ DEVICE PIC16F628A, INTRC_OSC_NOCLKOUT, WDT_OFF, PWRT_OFF, MCLR_OFF,PROTECT_ON,CPD_ON, BOD_OFF, LVP_OFF
    
    </i><b>DEFINE </b></font>OSC <font color="#FF0000">4
    
    </font>CMCON = <font color="#FF0000">7
    </font><font color="#000080"><b>PAUSE </b></font><font color="#FF0000">1
    </font>VRCON = <font color="#FF0000">0
    </font><font color="#000080"><b>PAUSE </b></font><font color="#FF0000">1
    
    </font>TRISA = <font color="#FF0000">0
    </font>TRISB = <font color="#FF0000">0
    </font>PORTA = <font color="#FF0000">0
    </font>PORTB = <font color="#FF0000">0
    
    </font>OPTION_REG.<font color="#FF0000">5 </font>= <font color="#FF0000">0 </font><font color="#000080"><i>' Internal clk.
    </i></font>OPTION_REG.<font color="#FF0000">3 </font>= <font color="#FF0000">0 </font><font color="#000080"><i>' Prescaler assigned to TMR0.
    </i></font>OPTION_REG.<font color="#FF0000">2 </font>= <font color="#FF0000">0 </font><font color="#000080"><i>' 
    </i></font>OPTION_REG.<font color="#FF0000">1 </font>= <font color="#FF0000">0 </font><font color="#000080"><i>' 
    </i></font>OPTION_REG.<font color="#FF0000">0 </font>= <font color="#FF0000">0 </font><font color="#000080"><i>' 1:1
    
    </i><b>PAUSE </b></font><font color="#FF0000">10
    
    </font>Led1 <font color="#000080"><b>VAR </b></font>PORTA.<font color="#FF0000">0    </font><font color="#000080"><i>'Assign leds. 
    </i></font>Led2 <font color="#000080"><b>VAR </b></font>PORTA.<font color="#FF0000">1
    </font>Led3 <font color="#000080"><b>VAR </b></font>PORTA.<font color="#FF0000">2
    </font>Led4 <font color="#000080"><b>VAR </b></font>PORTA.<font color="#FF0000">3
    </font>Led5 <font color="#000080"><b>VAR </b></font>PORTA.<font color="#FF0000">4
    
    </font>Led6 <font color="#000080"><b>VAR </b></font>PORTB.<font color="#FF0000">0
    </font>Led7 <font color="#000080"><b>VAR </b></font>PORTB.<font color="#FF0000">1
    </font>Led8 <font color="#000080"><b>VAR </b></font>PORTB.<font color="#FF0000">2
    </font>Led9 <font color="#000080"><b>VAR </b></font>PORTB.<font color="#FF0000">3
    </font>Led10 <font color="#000080"><b>VAR </b></font>PORTB.<font color="#FF0000">4
    </font>Led11 <font color="#000080"><b>VAR </b></font>PORTB.<font color="#FF0000">5
    </font>Led12 <font color="#000080"><b>VAR </b></font>PORTB.<font color="#FF0000">6
    </font>Led13 <font color="#000080"><b>VAR </b></font>PORTB.<font color="#FF0000">7
    </font>Led14 <font color="#000080"><b>VAR </b></font>PORTA.<font color="#FF0000">7
    </font>Led15 <font color="#000080"><b>VAR </b></font>PORTA.<font color="#FF0000">6
    
    </font>Index <font color="#000080"><b>VAR BYTE
    </b></font>LookForThree <font color="#000080"><b>VAR BYTE
    </b></font>Val    <font color="#000080"><b>VAR WORD
    </b></font>Result <font color="#000080"><b>VAR BYTE
    PAUSE </b></font><font color="#FF0000">100
    
    
    </font>Start:
    
    
        LookForThree = <font color="#FF0000">0
        </font><font color="#000080"><b>WHILE </b></font>LookForThree &lt;&gt; <font color="#FF0000">3   </font><font color="#000080"><i>' Look for three &quot;SET&quot;s.
            </i><b>RANDOM </b></font>Val            <font color="#000080"><i>' Get a random value.
            </i><b>PAUSEUS </b></font>TMR0          <font color="#000080"><i>' To get a good random number, have a changing value using TMR0 or TMR1.
        
            </i></font>LookForThree = <font color="#FF0000">0
            </font><font color="#000080"><b>FOR </b></font>Index = <font color="#FF0000">0 </font><font color="#000080"><b>TO </b></font><font color="#FF0000">15   </font><font color="#000080"><i>' Scan through the bits.
                </i><b>IF </b></font>Val.<font color="#FF0000">0</font>[Index] = <font color="#FF0000">1 </font><font color="#000080"><b>THEN </b></font>LookForThree = LookForThree + <font color="#FF0000">1  </font><font color="#000080"><i>'Count &quot;SET&quot;s.
            </i><b>NEXT </b></font>Index
        
        <font color="#000080"><b>WEND
    
        GOSUB </b></font>SetPins
        <font color="#000080"><b>PAUSE </b></font><font color="#FF0000">5000
    
        </font><font color="#000080"><b>GOTO </b></font>Start
    
    SetPins:
    
        Led1  = Val.<font color="#FF0000">0
        </font>Led2  = Val.<font color="#FF0000">1
        </font>Led3  = Val.<font color="#FF0000">2
        </font>Led4  = Val.<font color="#FF0000">3
        
        </font>Led5  = Val.<font color="#FF0000">4
        </font>Led6  = Val.<font color="#FF0000">5
        </font>Led7  = Val.<font color="#FF0000">6
        </font>Led8  = Val.<font color="#FF0000">7
        </font>Led9  = Val.<font color="#FF0000">8
        </font>Led10 = Val.<font color="#FF0000">9
        </font>Led11 = Val.<font color="#FF0000">10
        </font>Led12 = Val.<font color="#FF0000">11
        </font>Led13 = Val.<font color="#FF0000">12
        </font>Led14 = Val.<font color="#FF0000">13
        </font>Led15 = Val.<font color="#FF0000">14            
        </font><font color="#000080"><b>RETURN
    
    
    END
    
    </b></font>


    i tried to compile the sample code and simulate it using proteus at first it will light 3 leds but next it will only light two of them so i tried compiling it for other pic but it has some error can you please say to me what this line is for:

    VRCON = 0

    i got error with that when i try to compile it for other pic's ???

  5. #5
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Delete VRCON = 0, try again.


    Also,
    VAL is a WORD variable thus it has 16-bits.
    But, in the sample code, we have 15 bits (15 LEDS) getting set.
    Depending on the pic you are using, add one more led to an appropriate pic pin, and assign it to the last bit of VAL (Val.15) in SetPins subroutine.

    -------------------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  6. #6
    Join Date
    Apr 2009
    Posts
    24


    Did you find this post helpful? Yes | No

    Default

    i think the problem is on the configure bits ???

    i changed the pause 5000 to 100 and run it on simulation and i noticed that led's connected on RA6 and RA7 is never energized, what could be the problem?
    Last edited by joseph; - 18th May 2009 at 20:35.

  7. #7
    Join Date
    Apr 2009
    Posts
    24


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by rsocor01 View Post
    Joseph,

    Try to write the program by yourself and then if you have questions post your code and we will try to help you. I think that you should try to do it first. There are many books and tutorials out there. The following book, I use it as a guide and it has a few examples with LEDs very similar to what you want.

    http://www.amazon.com/PIC-Basic-Proj...0150464&sr=8-2

    I tried, but i just can't work it out

    @sayzer

    WOW thanks for the sample code it looks a bit clearer for me

Similar Threads

  1. Code doesn't work on 16F648A
    By Mr_Joe in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 1st September 2018, 22:09
  2. I2C Master/Slave 16F88/16F767 working code
    By DanPBP in forum Code Examples
    Replies: 2
    Last Post: - 23rd October 2012, 22:31
  3. HARDWARE I2C SAMPLE CODE question
    By Michael Wakileh in forum Code Examples
    Replies: 2
    Last Post: - 16th June 2009, 21:07
  4. Minimizing code space
    By Tobias in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 30th May 2009, 07:25
  5. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts