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!
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!
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
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 <> <font color="#FF0000">3 </font><font color="#000080"><i>' Look for three "SET"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 "SET"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
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 ???
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
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.
It all depending of our device in use, some have ADC, some have analog comparator, some have open-drain, and the list is almost endless. But there's less chance that any issues would be caused by a timer.
I suggest you to have a look at this link.
http://www.picbasic.co.uk/forum/showthread.php?t=11116
Remember, datasheet MUST be your friend![]()
Steve
It's not a bug, it's a random feature.
There's no problem, only learning opportunities.
I changed the start of the code
it was a part of the comments beforeCode:@ DEVICE PIC16F628A, INTRC_OSC_NOCLKOUT, WDT_OFF, PWRT_OFF, MCLR_OFF,PROTECT_ON,CPD_ON, BOD_OFF, LVP_OFF
and changed
toCode:FOR Index = 0 TO 15 ' Scan through the bits.
and the final code:Code:FOR Index = 0 TO 14 ' Scan through the bits.
it's now working good hooray!Code:@ DEVICE PIC16F628A, INTRC_OSC_NOCLKOUT, WDT_OFF, PWRT_OFF, MCLR_OFF,PROTECT_ON,CPD_ON, BOD_OFF, LVP_OFF DEFINE OSC 4 CMCON = 7 PAUSE 1 VRCON = 0 PAUSE 1 TRISA = 0 TRISB = 0 PORTA = 0 PORTB = 0 T1CON.1 = 0 OPTION_REG.5 = 0 ' Internal clk. OPTION_REG.3 = 0 ' Prescaler assigned to TMR0. OPTION_REG.2 = 0 ' OPTION_REG.1 = 0 ' OPTION_REG.0 = 0 ' 1:1 PAUSE 10 Led1 VAR PORTA.0 'Assign leds. Led2 VAR PORTA.1 Led3 VAR PORTA.2 Led4 VAR PORTA.3 Led5 VAR PORTA.4 Led6 VAR PORTB.0 Led7 VAR PORTB.1 Led8 VAR PORTB.2 Led9 VAR PORTB.3 Led10 VAR PORTB.4 Led11 VAR PORTB.5 Led12 VAR PORTB.6 Led13 VAR PORTB.7 Led14 VAR PORTA.6 Led15 VAR PORTA.7 Index VAR BYTE LookForThree VAR BYTE Val VAR WORD Result VAR BYTE PAUSE 100 Start: LookForThree = 0 WHILE LookForThree <> 3 ' Look for three "SET"s. RANDOM Val ' Get a random value. PAUSEUS TMR0 ' To get a good random number, have a changing value using TMR0 or TMR1. LookForThree = 0 FOR Index = 0 TO 14 ' Scan through the bits. IF Val.0[Index] = 1 THEN LookForThree = LookForThree + 1 'Count "SET"s. NEXT Index WEND GOSUB SetPins PAUSE 1000 GOTO Start SetPins: Led1 = Val.0 Led2 = Val.1 Led3 = Val.2 Led4 = Val.3 Led5 = Val.4 Led6 = Val.5 Led7 = Val.6 Led8 = Val.7 Led9 = Val.8 Led10 = Val.9 Led11 = Val.10 Led12 = Val.11 Led13 = Val.12 Led14 = Val.13 Led15 = Val.14 RETURN END![]()
thanks for your help pips more power!
![]()
Bookmarks