Shop OBEX P1 Docs P2 Docs Learn Events
Need help in completing this project and streamlining the code! 10 LED bargrap — Parallax Forums

Need help in completing this project and streamlining the code! 10 LED bargrap

kingspudkingspud Posts: 128
edited 2006-06-22 00:46 in BASIC Stamp
Hello all,

I have tried to write a program that will control the individual LED's on a 10 LED bar graph.

The program below will allow the user to move up or down the 10 LEDs with two buttons, and with another button you can set the LED to stay on or off.· Another button with clear and reset all 10 LEDs and start over AND the last two buttons allow you to go up or down frames, which means you can create movie type sequences per 10 LED frames and scroll through them one at a time.· The last button saves the LED state of that frame and write's it to memory.

So rehash:
6 buttons
10 LED bargraph
uses all 16 ports on my BS2px24 stamp!

the ports used for the 10 LED bargraph are:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10

The LED scroll up and down buttons are on ports:
0 and 11

The frame up and down buttons are on ports:
13 and 15

The LED·set to on or off·button is on port:
12

The clear button is on port:
14


The problem is:

1.· I can't seem to define the variable correctly and/or I keep running out of space?
2.· Is there a better way of coding the program to use the btn command and not IF...THEN all the time?
3.· Can this program be condensed or not?· If seem as if it is too long!!!
4.· The program is complicated and I can't seem to get it to work right, if I could get it to work it would create more possibilities or ideas for other programs?·
5.· If anyone out there is really good with Basic Stamp coding please help....· I know there has got to be a better way to code this application!


Here is the code if you want to mess with it, I think it is a lot of fun but hard to figure out!!!
I put in comments to help and if you have any questions pleas email me!!!!!

' {$STAMP BS2px}
' {$PBASIC 2.5}
'Defined Variables
stump·········· VAR···· Nib
track·········· VAR···· Byte······ ' This increments the array set by 10 for each new frame
light·········· VAR···· Nib(20)··· ' This is the 10 light array set for each frame
index·········· VAR···· Byte······ ' Used for each 1 to 10 loop counter
led············ VAR···· Nib(10)··· '
frame·········· VAR···· Nib······· '
counter········ VAR···· Nib
stepper········ VAR···· Nib
'Pre set variables
frame = 1
track = 0
stump = 0
stepper = 0
counter = 10

'This sets all the LED's to a OFF state
·'DEBUG " ",CR
· FOR index = 1 TO 10
·· LOW index
· NEXT

'This sets each light array to the 0 state!
· FOR index = 1 TO 30
·· WRITE light(index), 0
·· 'READ light(index), stump
· NEXT
· 'WRITE light(5), 1
· 'WRITE light(13), 1
· 'WRITE light(15), 1
· 'WRITE light(22), 1
· FOR index = 1 TO 30
·· READ light(index), stump
·· DEBUG "light(",DEC2 index, ") = ", DEC2 stump , CR
· NEXT
'**************************************************************************
' This program has six [noparse][[/noparse]6] buttons, which control 10 LEDs.
' The program allows you to control 10 individual LEDs on an LED bar graph
' and turn them ON or OFF one at a time!· Once you are finished with the LED
' pattern you can save the frame and go to the next frame.· Once on the next
' frame you can repeat the process again and go to the next frame.
' Once all the frames are complete you can scroll through all the saved frames
' one at a time at different speeds.
'
'*********************************************************
HIGH counter


· '***** MAIN PROGAM· *****

'Continues loop for the main program to monitor each buttons
DO
'*************************************
·· 'If you press button [noparse][[/noparse]0] This will set the LED to ON or OFF and write the status in memory
······· IF IN12 = 0 AND led(counter) = 0 THEN
········ led(counter) = 1
········· light(track) = led(counter)
·········· stepper = track + counter
·········· DEBUG "Stepper = ", DEC2 stepper, "·· "
·········· DEBUG "Light = ", DEC2 counter, "··· - ON -·· ", DEC2 led(counter), "·· Counter = ", DEC2 counter, CR
··········· WRITE light(stepper) ,led(counter)
··········· 'DEBUG "array = ",DEC2 light(nextlight), "···· light ", DEC2 nextlight, CR
········· PAUSE 300
······· ENDIF
······· IF IN12 = 0 AND led(counter) = 1 THEN
········· led(counter) = 0
·········· light(counter) = led(counter)
··········· stepper = track + counter
··········· DEBUG "Stepper = ", DEC2 stepper , "·· "
··········· DEBUG "Light = ", DEC2 counter, "·· - OFF -·· ", DEC2 led(counter), "·· Counter = ", DEC2 counter, CR
············ WRITE light(stepper) ,led(counter)
··········· 'DEBUG "array = ",DEC2 light(nextlight), "···· light ", DEC2 nextlight, CR
········ PAUSE 300
······· ENDIF


'*********************************************
·· 'If you press button [noparse][[/noparse]5] the index count will increase from 1 to 10 and stop
··· IF IN0 = 0 AND counter >=10 THEN
····· 'DEBUG DEC2 counter, "· YYYY· ", DEC2 led(counter), CR',"·· Frame = ", DEC2 frame, CR
······· counter = 10
······· HIGH counter
······· PAUSE 300
··· ELSEIF IN0 = 0 AND counter <10 THEN
····· IF led(counter) = 1· THEN
······· HIGH counter
····· ELSE
······· LOW counter
····· ENDIF
····· 'PAUSE 300
······ counter = counter + 1
····· HIGH counter

····· 'DEBUG "UP·· Nextlight = ",DEC2 counter, "· led = ", DEC2 led(counter),· "·· Counter = ", DEC2 counter, CR
······ PAUSE 400
··· ENDIF

'*********************************************
·· 'If you press button [noparse][[/noparse]4] the index count will decrease from 10 to 1 and stop
·· IF IN11 = 0 AND counter <=1 THEN
····· 'DEBUG DEC2 counter, "· XXXX· ", DEC2 led(counter), CR', "·· Frame = ", DEC2 frame, CR
······· counter = 1
······· 'DEBUG "Under 1 = ", DEC2 counter, CR
······· HIGH counter
······· PAUSE 200
··· ELSEIF IN11 = 0 AND counter >1 THEN
····· 'DEBUG "led(",DEC2 counter,") = ", DEC2 led(counter),CR
····· IF led(counter) = 1· THEN
······· HIGH counter
····· ELSE
······· LOW counter
····· ENDIF
····· 'PAUSE 500
······ counter = counter - 1
····· HIGH counter
··· 'DEBUG "DOWN Nextlight = ", DEC2 counter, "· Led = ", DEC2 led(counter),· "·· Counter = ", DEC2 counter, CR
··· PAUSE 400
· ENDIF


'*********************************************
· 'If you press button [noparse][[/noparse] 0 ] then frame goes DOWN by 1
··· IF IN13 = 0 AND frame <1 THEN
····· frame = 1
····· PAUSE 300
····· ELSEIF IN13 = 0 AND frame >1 THEN
······ frame = frame - 1
······ IF frame = 1 THEN
······ DEBUG "Frame = ",DEC2 frame, CR
······· track = 0
······ ELSEIF frame = 2 THEN
······ DEBUG "Frame = ",DEC2 frame, CR
······· track = 10
······ ELSEIF frame = 3 THEN
······ DEBUG "Frame = ",DEC2 frame, CR
······· track = 20
······ ELSEIF frame = 4 THEN
······ DEBUG "Frame = ",DEC2 frame, CR
······· track = 30
······ ELSEIF frame = 5 THEN
······ DEBUG "Frame = ",DEC2 frame, CR
······· track = 40
······ ENDIF
···· DEBUG "· track = ", DEC2 track, CR
···· FOR index = 1 TO 10
····· LOW index
···· NEXT

··· PAUSE 300
··· FOR index = 1 TO 10
······· track = track + 1
······· 'DEBUG "· track = ", DEC2 track, CR
······ READ light(track) ,stump
······ DEBUG "led(",DEC2 track,") = ", DEC2· stump, CR
······ IF stump = 1 THEN
········ HIGH index
······ ELSE
········ LOW· index
······ ENDIF
'······ DEBUG "track = ", DEC· track, "··· index = ", DEC· index,"·· frame = ", DEC· frame,CR
···· NEXT
···· counter = 10
··· HIGH counter
··· ENDIF


'*********************************************
· 'If you press button [noparse][[/noparse] 1 ] then frame goes UP by 1

·· IF IN15 = 0 AND frame >5 THEN
····· frame = 5
····· PAUSE 300
··· ELSEIF IN15 = 0 AND frame <5 THEN
···· frame = frame + 1
···· IF frame = 2 THEN
···· DEBUG "Frame = ",DEC2 frame, CR
····· track = 10
···· ELSEIF frame = 3 THEN
···· DEBUG "Frame = ",DEC2 frame, CR
····· track = 20
···· ELSEIF frame = 4 THEN
···· DEBUG "Frame = ",DEC2 frame, CR
····· track = 30
···· ELSEIF frame = 5 THEN
···· DEBUG "Frame = ",DEC2 frame, CR
····· track = 40
···· ENDIF
··· DEBUG "· track = ", DEC2 track, CR
···· FOR index = 1 TO 10
····· LOW index
···· NEXT
··· PAUSE 300
····· FOR index = 1 TO 10
······· track = track + 1
······ 'DEBUG "· track = ", DEC2 track, CR
······ READ light(track) ,stump
······ DEBUG "led(",DEC2 track,") = ", DEC2· stump, CR
······ IF stump = 1 THEN
········ HIGH index
······ ELSE
········ LOW· index
······ ENDIF
'······ DEBUG "track = ", DEC· track, "··· index = ", DEC· index,"·· frame = ", DEC· frame,CR
···· NEXT
··· counter = 10
··· HIGH counter
··· ENDIF


'*********************************************
· 'If you press button [noparse][[/noparse] 7 ] this will save the current LED bar graph status display in current frame
· ' IF IN13 = 0 THEN
· '
· '· DEBUG " ",CR
· '
· '·· FOR index = 1 TO 10
· '··· 'counter = counter + track + 1
· '···· WRITE light(counter) ,led(index)
· '····· DEBUG· "Display LED = ", DEC2 index, "···· Array light =" ,DEC2 light(counter), "··· track = ",DEC2 counter, CR
· '
· '·· NEXT
· '
· '·· DEBUG " ",CR
· '·· counter = 0
· '·· PAUSE 300
· ' ENDIF


'*********************************************
· 'If you press button [noparse][[/noparse] 7 ] this will clear the current LED bar graph display
· ' and WRITEs all the LEDs to an OFF state!
·· IF IN14 = 0 THEN
··· DEBUG " ", CR
···· FOR index = 1 TO 10
····· LOW index
····· 'counter = counter + track + 1
····· DEBUG "track = ", DEC· track,· "· "
····· led(index) = 0
····· WRITE light(index) ,led(index)
····· DEBUG "Light = ", DEC· index, "····· status = ", DEC· led(index), "····· Frame = ", DEC· frame, CR
···· NEXT
····· PAUSE 300
····· 'nextlight = 10
···· counter = 10
···· HIGH counter
·· ENDIF
LOOP

END

Comments

  • Lee HarkerLee Harker Posts: 104
    edited 2006-06-21 18:44
    Kingspud,
    Whenever you are looking for ways to streamline a program, first look for a series of commands that look very similar. There will usually be an easier and more understandable way to achieve the goal.
    Here is an example from your program:

    This
      'If you press button [noparse][[/noparse] 0 ] then frame goes DOWN by 1
        IF IN13 = 0 AND frame <1 THEN
          frame = 1
          PAUSE 300
          ELSEIF IN13 = 0 AND frame >1 THEN
           frame = frame - 1
           IF frame = 1 THEN
           DEBUG "Frame = ",DEC2 frame, CR
            track = 0
           ELSEIF frame = 2 THEN
           DEBUG "Frame = ",DEC2 frame, CR
            track = 10
           ELSEIF frame = 3 THEN
           DEBUG "Frame = ",DEC2 frame, CR
            track = 20
           ELSEIF frame = 4 THEN
           DEBUG "Frame = ",DEC2 frame, CR
            track = 30
           ELSEIF frame = 5 THEN
           DEBUG "Frame = ",DEC2 frame, CR
            track = 40
           ENDIF
         DEBUG "  track = ", DEC2 track, CR
         FOR index = 1 TO 10
          LOW index
         NEXT
    
    

    nono.gif

    becomes:
    'If you press button [noparse][[/noparse] 0 ] then frame goes DOWN by 1
        IF IN13 = 0 AND frame <1 THEN
          frame = 1
          PAUSE 300
          ELSEIF IN13 = 0 AND frame >1 AND frame<6 THEN
           frame = frame - 1
           DEBUG "Frame = ",DEC2 frame, CR
            track = (frame  - 1) * 10
           ENDIF
         DEBUG "  track = ", DEC2 track, CR
         FOR index = 1 TO 10
          LOW index
         NEXT
    
    


    smilewinkgrin.gif

    Lee Harker
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2006-06-21 21:07
    Kingspud,

    It seems to me there are too many state variables and arrays. Maybe I am missing something, but, it should take 10 bits in one word to hold the LED states, and that might even be the OUTS variable.
    outs = 0 ' to set all leds to off
    out0(10) = 1 ' to set the led on pin p10 to HIGH
    WRITE patIdx*2, WORD outs ' to transfer a 10 bit pattern to eeprom at location patIdx
    READ patIdx*2, WORD outs ' to transfer a 10 bit pattern for eeprom to the LEDs.
    Also, I see a need for only one or two nibbles and bytes to serve as the indeces into the bits and into the patterns stored in eeprom. But that's it.

    The main DO loop is quite long. It would be easier to follow if it were shorter, with calls out to subroutines. For example, put all the buttons on one port (instead of just one button isolated on p0), That way you can read one byte and determine in a subroutine which button has been pressed. Then, having the index of which button was pressed, use SELECT CASE statements to branch to the actions. The buttons need to have a debounce action, so the action only happens once for each press. I don't think you need to get into the BUTTON command, although one thing it is good for is autorepeat action, and it does have debounce action.

    An index that has to rotate around a group of values can be handled with the // operator instead of IF.

    frame = frame + 1 // 10 ' rotate from frame 9 back to frame 0
    frame = frame - 1 + 10 // 10 ' rotate from frame 0 back up to frame 9

    Just some ideas. Maybe I'm way misunderstanding the actions.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • kingspudkingspud Posts: 128
    edited 2006-06-22 00:46
    Thank you Lee and Tracy,

    I am kinda new to the programming side of the Basic Stamp and your ideas are VERY useful... once I understand them that is. hehehe
    It may take me some time to absorb what you both just wrote.

    I will come back with questions soon!!!!

    Thanks a lot though!

    Joe
Sign In or Register to comment.