LED1 = !(MyByte != 1)
LED2 = !(MyByte != 2)
LED3 = !(MyByte != 3)
LED4 = !(MyByte != 4)
LED5 = !(MyByte != 5)
LED6 = !(MyByte != 6)
LED1 = !(MyByte != 1)
LED2 = !(MyByte != 2)
LED3 = !(MyByte != 3)
LED4 = !(MyByte != 4)
LED5 = !(MyByte != 5)
LED6 = !(MyByte != 6)
DT
Excellent, the top of my fishing rod is moving a touch!
but I don't understand the response!
eg if the random number comes out as 1, meaning I want to light LED1.....normally - if wanting to light LED1 for 200ms, I'd simply do this....
but, how do I do this......Code:HIGH LED1 PAUSE 200 LOW LED1 PAUSE 200
Apologies if that is what you just told me Darrel, but I'm not sure how to use the info you gave me (that'll be the n00bishness that's never far away from all I do!)Code:HIGH LED'result_of_random_number' PAUSE 200 LOW LED'result_of_random_number' PAUSE 200
Change the conditions on me eh ...
How about ...Code:;--------------------------------------- Main: Random MyWord MyByte=(MyWord//6)+1 GOSUB HIGH_LED PAUSE 200 GOSUB LOW_LED PAUSE 200 GOTO Main ;--------------------------------------- HIGH_LED: SELECT CASE MyByte CASE 1 : HIGH LED1 CASE 2 : HIGH LED2 CASE 3 : HIGH LED3 CASE 4 : HIGH LED4 CASE 5 : HIGH LED5 CASE 6 : HIGH LED6 END SELECT RETURN ;--------------------------------------- LOW_LED: SELECT CASE MyByte CASE 1 : LOW LED1 CASE 2 : LOW LED2 CASE 3 : LOW LED3 CASE 4 : LOW LED4 CASE 5 : LOW LED5 CASE 6 : LOW LED6 END SELECT RETURN
DT
Hey Darrel...that works a treat ...I've now got pretty twinkly Blue leds randomly flashing before my eyes (which strangely, makes me feel warm inside).
I note that a fair few of the random numbers are the same as the previous (I guess when you've only six to choose from, then that's bound to happen!), so I'll sit down for a few weeks & try & code this out!
Many thanks!
Hank.
Edit: Actually, dialing out the successive repeats only took a couple of minutes...
Code:MyWord var Word MyByte var Byte previous_random var Byte ;--------------------------------------- Main: Random MyWord MyByte=(MyWord//6)+1 if previous_random = MyByte then goto Main GOSUB HIGH_LED PAUSE 50 GOSUB LOW_LED PAUSE 50 previous_random = MyByte GOTO Main ;--------------------------------------- HIGH_LED: SELECT CASE MyByte CASE 1 : HIGH LED1 CASE 2 : HIGH LED2 CASE 3 : HIGH LED3 CASE 4 : HIGH LED4 CASE 5 : HIGH LED5 CASE 6 : HIGH LED6 END SELECT RETURN ;--------------------------------------- LOW_LED: SELECT CASE MyByte CASE 1 : LOW LED1 CASE 2 : LOW LED2 CASE 3 : LOW LED3 CASE 4 : LOW LED4 CASE 5 : LOW LED5 CASE 6 : LOW LED6 END SELECT
Last edited by HankMcSpank; - 22nd April 2011 at 22:48.
Just had a sleep on this - I've never previously used case, and now I fresh look at it, to my eyes it's just a load of IFs?
For example....
could be written as...Code:HIGH_LED: SELECT CASE MyByte CASE 1 : HIGH LED1 CASE 2 : HIGH LED2 CASE 3 : HIGH LED3 CASE 4 : HIGH LED4 CASE 5 : HIGH LED5 CASE 6 : HIGH LED6 END SELECT RETURN
So when to use case & when to use if?Code:IF MYBYTE = 1 THEN HIGH LED1 IF MYBYTE = 2 THEN HIGH LED2 IF MYBYTE = 3 THEN HIGH LED3 IF MYBYTE = 4 THEN HIGH LED4 IF MYBYTE = 5 THEN HIGH LED5 IF MYBYTE = 6 THEN HIGH LED6
Going back 20 years to when I used to write simple VMS DCL programs, I could construct code like thus...
HIGH "LED"+'MyByte'
the stuff inbetween " " was text & stuff in the ' ' was the value held in a variable
therefore the above would actually run as.....
HIGH LED1 (assuming MyByte held 1) ......presumably no similar ways of constucting in Picbasic?
Select Case is a lot like a "Load of IF's" with a few differences.The same functionality using IF statements would look like this ...
- It removes some of the (n00besque content contained) look and feel.
- Only the first "CASE" that evaluates TRUE will execute, then it exits the SELECT block.
In your list of IF's, if the first one is true, it still goes on and evaluates all the other IF's too.
This allows more complex logic, where more than one test can be true, but only the first one is acted on.OR, if you have PBP 2.60 you could do it like this ...Code:IF MYBYTE = 1 THEN HIGH LED1 ELSE IF MYBYTE = 2 THEN HIGH LED2 ELSE IF MYBYTE = 3 THEN HIGH LED3 ELSE IF MYBYTE = 4 THEN HIGH LED4 ELSE IF MYBYTE = 5 THEN HIGH LED5 ELSE IF MYBYTE = 6 THEN HIGH LED6 ENDIF ENDIF ENDIF ENDIF ENDIF ENDIFIt may not seem like much difference with this simple example, but with large SELECT CASE blocks using more complex comparisons, it can save a boat load of execution time not having to evaluate every condition.Code:IF MYBYTE = 1 THEN HIGH LED1 ELSEIF MYBYTE = 2 THEN HIGH LED2 ELSEIF MYBYTE = 3 THEN HIGH LED3 ELSEIF MYBYTE = 4 THEN HIGH LED4 ELSEIF MYBYTE = 5 THEN HIGH LED5 ELSEIF MYBYTE = 6 THEN HIGH LED6 ENDIF
* SELECT CASE is also much easier to read. Especially when it's not your program.
PicBasic Pro, was written to generate the Smallest and Fastest code possible. As it is the HIGH/LOW statements only use a couple of instruction cycles. If you add in the abilities to use them like array's and access pins non-contiguously, it would make EVERY HIGH/LOW statement Bigger and Slower, breaking everyone's code, and causing more complaints than not having those abilities produces.
Last edited by Darrel Taylor; - 23rd April 2011 at 16:29.
DT
Many thanks Darrel....crystal clear now![]()
Bookmarks