Newcomer needs starting help with RGB code / Basic-Stamp
Archiver
Posts: 46,084
Hi..
I am a newcomer and I need starting help.
Can anybody show me how I imlement three pushbuttons in my code?
Like this:
First pushbutton:
(to step thru the colours)
Push 1 = red only
Push 2 = red and green only
Push 3 = green only
Push 4 = green and blue only
Push 5 = blue only
Push 6 = red and blue only
Push 7 = start the sequenz
Push 8 = off
Second pushbutton: (changed the worth named "a", when sequenz is
selected, otherwise without function)
Push = Speed up
Third pushbutton: (changed the worth named "a", when sequenz is
selected, otherwise without function)
Push = speed down
And this is my code:
cut
'
======================================================================
'
' Changes the brightness of LED`s using PWM
'
' {$STAMP BS2}
'
======================================================================
Pin0 CON 0 'LED red
Pin1 CON 1 'LED green
Pin2 CON 2 'LED blue
level VAR Byte
a VAR Byte
'
DlyTm CON 0 'Pause
a = 1 'Steps
'
Main:
High Pin0 'LED red on / red on
Low Pin1 'LED green off
Low Pin2 'LED blue off
Loop:
FOR level = 0 TO 255 Step a 'LED green up / red and green on
PWM Pin1,level,25
PAUSE DlyTm
NEXT
FOR level = 255 TO 0 Step a 'LED red down / green on
PWM Pin0,level,25
PAUSE DlyTm
NEXT
Low Pin0 'LED red off
FOR level = 0 TO 255 Step a 'LED blue up / green and blue on
PWM Pin2,level,25
PAUSE DlyTm
NEXT
FOR level = 255 TO 0 Step a 'LED green down / blue on
PWM Pin1,level,25
PAUSE DlyTm
NEXT
Low Pin1 'LED green off
FOR level = 0 TO 255 Step a 'LED red up / blue and red on
PWM Pin0,level,25
PAUSE DlyTm
NEXT
FOR level = 255 TO 0 Step a 'LED blue down / red on
PWM Pin2,level,25
PAUSE DlyTm
NEXT
Low Pin2 'LED blue off
GOTO Loop
END
cut
Thanks for help.
Best regards, Kai
I am a newcomer and I need starting help.
Can anybody show me how I imlement three pushbuttons in my code?
Like this:
First pushbutton:
(to step thru the colours)
Push 1 = red only
Push 2 = red and green only
Push 3 = green only
Push 4 = green and blue only
Push 5 = blue only
Push 6 = red and blue only
Push 7 = start the sequenz
Push 8 = off
Second pushbutton: (changed the worth named "a", when sequenz is
selected, otherwise without function)
Push = Speed up
Third pushbutton: (changed the worth named "a", when sequenz is
selected, otherwise without function)
Push = speed down
And this is my code:
cut
'
======================================================================
'
' Changes the brightness of LED`s using PWM
'
' {$STAMP BS2}
'
======================================================================
Pin0 CON 0 'LED red
Pin1 CON 1 'LED green
Pin2 CON 2 'LED blue
level VAR Byte
a VAR Byte
'
DlyTm CON 0 'Pause
a = 1 'Steps
'
Main:
High Pin0 'LED red on / red on
Low Pin1 'LED green off
Low Pin2 'LED blue off
Loop:
FOR level = 0 TO 255 Step a 'LED green up / red and green on
PWM Pin1,level,25
PAUSE DlyTm
NEXT
FOR level = 255 TO 0 Step a 'LED red down / green on
PWM Pin0,level,25
PAUSE DlyTm
NEXT
Low Pin0 'LED red off
FOR level = 0 TO 255 Step a 'LED blue up / green and blue on
PWM Pin2,level,25
PAUSE DlyTm
NEXT
FOR level = 255 TO 0 Step a 'LED green down / blue on
PWM Pin1,level,25
PAUSE DlyTm
NEXT
Low Pin1 'LED green off
FOR level = 0 TO 255 Step a 'LED red up / blue and red on
PWM Pin0,level,25
PAUSE DlyTm
NEXT
FOR level = 255 TO 0 Step a 'LED blue down / red on
PWM Pin2,level,25
PAUSE DlyTm
NEXT
Low Pin2 'LED blue off
GOTO Loop
END
cut
Thanks for help.
Best regards, Kai
Comments
How about the following for your pushbuttons... This code uses the
notion of "state variables". Since this kind of programming is not
familiar to many newcomers, I'll follow up with more detailed
explanation of what is going on...
' buttons are attached to p4, p5, p6, low when pressed
' use pullup resistor and debounce capacitor.
state var nib ' 0 to 8 for push 0 to 8
buttons var nib ' current state of buttons pushed
button0 var nib ' old state of buttons
buttonx var nib ' detect change in state of buttons
a var byte ' steps of delay
buttonscan:
state=0 ' start up, no push yet
dirsA=%0111 ' light pins are outputs
buttonloop:
buttons=inB ' buttons are attached to p4, p5, p6, low when pressed
buttonx=button0^buttons&buttons ' look for 0->1 changes
button0=buttons
state=buttonx.bit0+state//9 ' step through 9 states
a=a + buttonx.bit1 - buttonx.bit2 min 1
if state=7 then goto sequenz ' play the sequence
if state=8 then goto off ' turn everything off
lookup state,[noparse][[/noparse]0,1,3,2,6,4,5],outA ' turn on pattern
goto buttonloop
The first line of the loop code grabs the state of the button input
pins. The second line uses the Stamp "^" operator ("^"=XOR in
Stampese) to detect that a change has taken place, compared with the
old state ("button0"), and that the change is from 0 to 1 ("&
buttons", "&"=AND in Stampese). So now the state variable buttonx
contains 1s only in bit positions where a key has been pressed and
released. The effect of this is to "debounce" the pushbuttons, so
that you have to press and release the button to get each step. The
4th and 5th lines of code make use of the change bits ("buttonx").
The main state variable ("state") is incremented when the 1st button
is pressed and released, and the delay variable ("a") is incremented
or decremented by the 2nd and 3rd buttons. The min 1 keeps the delay
variable from going below 1 if you repeatedly press the "down"
button. You might also want to include a "max" to limit the delay to
a reasonable top value. The last lines of the loop branch to the
other routines if the state reaches "7" or "8", or turns ON the
desired pattern of light by using the LOOKUP command. The loop scans
all three pushbuttons repeatedly and rapidly, but nothing happens
outside unless you press and release a button.
-- best regards
Tracy Allen
electronically monitored ecosystems
http://www.emesystems.com
mailto:tracy@e...
>Hi..
>
>I am a newcomer and I need starting help.
>
>Can anybody show me how I imlement three pushbuttons in my code?
>Like this:
>
>First pushbutton:
>(to step thru the colours)
>
>Push 1 = red only
>Push 2 = red and green only
>Push 3 = green only
>Push 4 = green and blue only
>Push 5 = blue only
>Push 6 = red and blue only
>Push 7 = start the sequenz
>Push 8 = off
>
>Second pushbutton: (changed the worth named "a", when sequenz is
>selected, otherwise without function)
>
>Push = Speed up
>
>Third pushbutton: (changed the worth named "a", when sequenz is
>selected, otherwise without function)
>
>Push = speed down
>
>And this is my code:
>
>
cut
>
>'
>======================================================================
>'
>' Changes the brightness of LED`s using PWM
>'
>' {$STAMP BS2}
>'
>======================================================================
>
>Pin0 CON 0 'LED red
>Pin1 CON 1 'LED green
>Pin2 CON 2 'LED blue
>
>level VAR Byte
>
>a VAR Byte
>
>'
>
>DlyTm CON 0 'Pause
>
>a = 1 'Steps
>
>'
>
>Main:
>
>High Pin0 'LED red on / red on
>Low Pin1 'LED green off
>Low Pin2 'LED blue off
>
>Loop:
>
>FOR level = 0 TO 255 Step a 'LED green up / red and green on
>PWM Pin1,level,25
>PAUSE DlyTm
>NEXT
>
>FOR level = 255 TO 0 Step a 'LED red down / green on
>PWM Pin0,level,25
>PAUSE DlyTm
>NEXT
>
>Low Pin0 'LED red off
>
>FOR level = 0 TO 255 Step a 'LED blue up / green and blue on
>PWM Pin2,level,25
>PAUSE DlyTm
>NEXT
>
>FOR level = 255 TO 0 Step a 'LED green down / blue on
>PWM Pin1,level,25
>PAUSE DlyTm
>NEXT
>
>Low Pin1 'LED green off
>
>FOR level = 0 TO 255 Step a 'LED red up / blue and red on
>PWM Pin0,level,25
>PAUSE DlyTm
>NEXT
>
>FOR level = 255 TO 0 Step a 'LED blue down / red on
>PWM Pin2,level,25
>PAUSE DlyTm
>NEXT
>
>Low Pin2 'LED blue off
>
>GOTO Loop
>
>END
>
>
cut
>
>Thanks for help.
>
>Best regards, Kai
thanks for your reply but I understand nothing :-(
To understand how I implement a button I think I must understand what
I do.
This simple code switched in a loop three LED`s on and off.
Please show me where must I implement "ONE" button to get one color
like e.g. blue.
Like this:
The loop is running.
Now I push a button and the loop stops and I get the color blue.
(I think the loop don`t stop, I think I must extend my code, it`s
correct?)
'{$STAMP BS2}
DlyTm CON 500
Loop:
HIGH 0 'LED red on
PAUSE DlyTm
HIGH 1 'LED green on
PAUSE DlyTm
LOW 0 'LED red off
PAUSE DlyTm
HIGH 2 'LED blue on
PAUSE DlyTm
LOW 1 'LED green off
PAUSE DlyTm
HIGH 0 'LED red on
PAUSE DlyTm
LOW 2 'LED blue off
GOTO Loop
I hope I don`t give you a lot of trouble.
Best regards, Kai
>
>thanks for your reply but I understand nothing :-(
Hi Kai,
Sorry about that--the program was condensed to do a bunch of things
all at once. Do you have the resource materials from Parallax? In
addition to the manual, there are things like the Stampworks book and
the Stamps-in-class series to download for free.
>
>To understand how I implement a button I think I must understand what
>I do.
>
>This simple code switched in a loop three LED`s on and off.
>Please show me where must I implement "ONE" button to get one color
>like e.g. blue.
>
>Like this:
>The loop is running.
>Now I push a button and the loop stops and I get the color blue.
>(I think the loop don`t stop, I think I must extend my code, it`s
>correct?)
>
>
>'{$STAMP BS2}
>
>DlyTm CON 500
>
>Loop:
>HIGH 0 'LED red on
>PAUSE DlyTm
>HIGH 1 'LED green on
>PAUSE DlyTm
>LOW 0 'LED red off
>PAUSE DlyTm
>HIGH 2 'LED blue on
>PAUSE DlyTm
>LOW 1 'LED green off
>PAUSE DlyTm
>HIGH 0 'LED red on
>PAUSE DlyTm
>LOW 2 'LED blue off
>GOTO Loop
>
I am not exactly sure what you want to do, but here is one trick.
Instead of "PAUSE Dlytm", make that a call every time to a
subroutine, "GOSUB delay". Make that subroutine come back to the
main program after a fixed delay, unless you press the pushbutton.
If you do press the button, the subroutine just repeats the delay, so
that the current color combination sticks.
delay:
pause Dlytm
if in4=0 then delay ' if button is pressed, repeat delay
return ' back to main code
Your main loop is now this...
Loop:
HIGH 0 'LED red on
gosub delay
HIGH 1 'LED green on
gosub delay
LOW 0 'LED red off
gosub delay
' etc.
If you want to force the blue light on when you press the button,
change the target of the if statement...
delay:
pause Dlytm
if in4=0 then blue_on
return
blue_on
outA=%0100
pause dlytm
' what now?
Note the "outA=%0100" statement. That may be useful for you. It
affects 4 output bits at once, turning the blue led ON and the red
and green both OFF, all in one instruction.
I hope that helps,
-- regards,
Tracy Allen
electronically monitored ecosystems
mailto:tracy@e...
http://www.emesystems.com
I am despairs........
Please show me anybody what I do wrong.
Without the BUTTON function works the code very fine,
with the BUTTON function the LED`s begin to spin.
I am helpless....
Despairs regards, Kai
snip
' =========================================================================
' File: RGB-fade.bs2
'
' Changes the brightness of LED`s using PWM
'
' {$STAMP BS2}
' =========================================================================
Pin0 CON 0 'LED red
Pin1 CON 1 'LED green
Pin2 CON 2 'LED blue
level VAR Byte
a VAR Byte
'
DlyTm CON 0 'Pause
a = 1 'Steps
'
Main:
High Pin0 'LED red on / red on
Low Pin1 'LED green off
Low Pin2 'LED blue off
'
BUTTON start
delay:
pause DlyTm
if in4=0 then blue_on ' if button is pressed, blue_on
return ' back to main code
blue_on
outA=%0100
pause DlyTm
'
BUTTON end
Loop:
FOR level = 0 TO 255 Step a 'LED green up / red and green on
PWM Pin1,level,25
gosub delay
NEXT
FOR level = 255 TO 0 Step a 'LED red down / green on
PWM Pin0,level,25
gosub delay
NEXT
Low Pin0 'LED red off
FOR level = 0 TO 255 Step a 'LED blue up / green and blue on
PWM Pin2,level,25
gosub delay
NEXT
FOR level = 255 TO 0 Step a 'LED green down / blue on
PWM Pin1,level,25
gosub delay
NEXT
Low Pin1 'LED green off
FOR level = 0 TO 255 Step a 'LED red up / blue and red on
PWM Pin0,level,25
gosub delay
NEXT
FOR level = 255 TO 0 Step a 'LED blue down / red on
PWM Pin2,level,25
gosub delay
NEXT
Low Pin2 'LED blue off
GOTO Loop
END
Anyhow, Look at the subroutine called "Blue_on" At first glance it
looks like you forgot the ":" (colon) after the routine name. That
would make the STAMP treat this as a variable not a subroutine
name. Therefore it will make the program not know where to go.
--- In basicstamps@y..., "Kai Kuehle" <kai@u...> wrote:
> Hello :-(
>
> I am despairs........
> Please show me anybody what I do wrong.
>
> Without the BUTTON function works the code very fine,
> with the BUTTON function the LED`s begin to spin.
>
> I am helpless....
>
> Despairs regards, Kai
>
>
snip
>
> '
=====================================================================
====
> ' File: RGB-fade.bs2
> '
> ' Changes the brightness of LED`s using PWM
> '
> ' {$STAMP BS2}
> '
=====================================================================
====
>
> Pin0 CON 0 'LED red
> Pin1 CON 1 'LED green
> Pin2 CON 2 'LED blue
>
> level VAR Byte
>
> a VAR Byte
>
> '
>
> DlyTm CON 0 'Pause
>
> a = 1 'Steps
>
> '
>
> Main:
>
> High Pin0 'LED red on / red on
> Low Pin1 'LED green off
> Low Pin2 'LED blue off
>
> '
BUTTON start
>
> delay:
> pause DlyTm
> if in4=0 then blue_on ' if button is pressed, blue_on
> return ' back to main code
>
> blue_on
> outA=%0100
> pause DlyTm
>
> '
BUTTON end
----
>
> Loop:
>
> FOR level = 0 TO 255 Step a 'LED green up / red and green on
> PWM Pin1,level,25
> gosub delay
> NEXT
>
> FOR level = 255 TO 0 Step a 'LED red down / green on
> PWM Pin0,level,25
> gosub delay
> NEXT
>
> Low Pin0 'LED red off
>
> FOR level = 0 TO 255 Step a 'LED blue up / green and blue on
> PWM Pin2,level,25
> gosub delay
> NEXT
>
> FOR level = 255 TO 0 Step a 'LED green down / blue on
> PWM Pin1,level,25
> gosub delay
> NEXT
>
> Low Pin1 'LED green off
>
> FOR level = 0 TO 255 Step a 'LED red up / blue and red on
> PWM Pin0,level,25
> gosub delay
> NEXT
>
> FOR level = 255 TO 0 Step a 'LED blue down / red on
> PWM Pin2,level,25
> gosub delay
> NEXT
>
> Low Pin2 'LED blue off
>
> GOTO Loop
>
> END