My first [very] simple project
Tomato
Posts: 23
Hey guys, just wanted to share my first project, which was really really simple and most of you can do it in your sleep lol. It was fun to do make it and I think I might try to expand on it a little to get the hang of some of the other commands.
Description: Stamp controls 4 LEDs (red, orange, yellow, green) and takes input from two little buttons (call them 1 and 2). When you press button 1, the LEDs light up in succession from green to red. When you press button 2, they light up from red to green. When you press both buttons together, the light "bounces" back and forth between green and red. If you don't press any buttons, all LEDs stay off. Here is a very poor video I made of the thing in action:
http://s273.photobucket.com/albums/jj234/AdamO1182/?action=view¤t=MVI_0697.mp4
Everything in this video (except for the BS2 and the breadboard)--including the laptop--is from the garbage. Yay for trashpicking.
and here is the code:
Pointers are appreciated!
Tomato
Description: Stamp controls 4 LEDs (red, orange, yellow, green) and takes input from two little buttons (call them 1 and 2). When you press button 1, the LEDs light up in succession from green to red. When you press button 2, they light up from red to green. When you press both buttons together, the light "bounces" back and forth between green and red. If you don't press any buttons, all LEDs stay off. Here is a very poor video I made of the thing in action:
http://s273.photobucket.com/albums/jj234/AdamO1182/?action=view¤t=MVI_0697.mp4
Everything in this video (except for the BS2 and the breadboard)--including the laptop--is from the garbage. Yay for trashpicking.
and here is the code:
' {$STAMP BS2} ' {$PBASIC 2.5} INPUT 8 ' "Up" switch INPUT 9 ' "Down" switch Timing CON 60 ' Constant for defining "pause" time Main: IF IN9 = 0 AND IN8 = 1 THEN ' Flashes LEDs from green to red GOSUB Up ENDIF IF IN9 = 1 AND IN8 = 0 THEN ' Flashes LEDs from red to green GOSUB Down ENDIF IF IN9 = 0 AND IN8 = 0 THEN ' All LEDs stay off GOSUB Nothing ENDIF IF IN9 = 1 AND IN8 = 1 THEN ' LEDs flash from green to red and back again GOSUB Up GOSUB Down ENDIF GOTO Main '~~~~~~~~~~~~~~~~~~subroutines~~~~~~~~~~~~~~~~~~~~~ Nothing: ' All LEDs stay off (subroutine) LOW 0 LOW 1 LOW 2 LOW 3 RETURN Down: ' LEDs flash from red to green (subroutine) HIGH 0 PAUSE Timing HIGH 1 PAUSE Timing LOW 0 PAUSE Timing HIGH 2 PAUSE Timing LOW 1 PAUSE Timing HIGH 3 PAUSE Timing LOW 2 PAUSE Timing LOW 3 PAUSE Timing + Timing RETURN Up: ' LEDs flash from green to red (subroutine) HIGH 3 PAUSE Timing HIGH 2 PAUSE Timing LOW 3 PAUSE Timing HIGH 1 PAUSE Timing LOW 2 PAUSE Timing HIGH 0 PAUSE Timing LOW 1 PAUSE Timing LOW 0 PAUSE Timing + Timing RETURNProgamming is more fun that I thought! As you can see in the video, it works just fine. I am thinking I will try adding tones (like rising tones and falling tones and stuff like that) later on. But I have one question: is there a way to call a subroutine backwards? my "Up" and "Down" subroutines are more or less the reverse of each other, so if I could just write one subroutine and then call it backwards, that would make my whole code more efficient i think.
Pointers are appreciated!
Tomato
Comments
They talk about this in Andy Lindsay's book "What's a Microcontroller" in chapter #8 of this book and the PDF can be downloaded from Parallax's website as well. This chapter shows you how a piezoelectric speaker makes sounds using the FREQOUT command.
thanks for the advice, I went to Parallax's website. There's a lot of really useful info there thanks!
-Tomato
It's amazing you got all that out of a dumpster! Thank about luck man.... Good job
-Tomato
Here is a shorter example of your code:
button_up pin 8
button_down pin 9
light_number var nib
timing con 60
LED var nib(4)
LED(0) = 0
LED(1) = 1
LED(2) = 2
LED(3) = 3
do
if(button_up = 1) then
gosub leds_up
endif
if(button_down = 1) then
gosub leds_down
endif
loop
leds_up:
for light_number = 0 to 3
high LEDS(light_number)
pause timing
if(light_number > 0) then
low LEDS(light_number - 1)
endif
next
low LEDS(3)
return
leds_up:
for light_number = 3 to 0
high LEDS(light_number)
pause timing
if(light_number < 3) then
low LEDS(light_number + 1)
endif
next
low LEDS(0)
return
Basically...since you are in a loop you dont need to check both buttons, it will just check the first button, go leds_up, then check the second button and go leds_down
also if you want to only have 1 subroutine...
direction var bit
counter var nib
led_number = 0
direction = 0
gosub move_leds
direction = 1
gosub move_leds
move_leds:
for counter = 0 to 3
high LED(led_number)
pause timing
if (led_number - (2 * (direction + 1)) + 1) < 4 then
low LED(led_number - (2 * (direction + 1)) + 1)
led_number = led_number - (2 * direction) + 1
next
return
I appreciate it!:thumb:
-Tomato
Good work!