Shop OBEX P1 Docs P2 Docs Learn Events
My first [very] simple project — Parallax Forums

My first [very] simple project

TomatoTomato Posts: 23
edited 2011-01-22 18:34 in BASIC Stamp
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&current=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
RETURN
Progamming 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

  • bsnutbsnut Posts: 521
    edited 2011-01-18 01:38
    Tomato,
    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.
  • TomatoTomato Posts: 23
    edited 2011-01-18 19:49
    Hey bsnut,

    thanks for the advice, I went to Parallax's website. There's a lot of really useful info there thanks!

    -Tomato
  • bsnutbsnut Posts: 521
    edited 2011-01-19 00:36
    I am glad to help you out
  • Spiral_72Spiral_72 Posts: 791
    edited 2011-01-19 09:29
    Hey! That's pretty nice!

    It's amazing you got all that out of a dumpster! Thank about luck man.... Good job :)
  • TomatoTomato Posts: 23
    edited 2011-01-19 10:36
    Spiral_72 wrote: »
    Hey! That's pretty nice!

    It's amazing you got all that out of a dumpster! Thank about luck man.... Good job :)
    Yeah haha that is how I roll. I have a pretty big parts stock...resistors, caps, inductors, transistors, ICs, motors, speakers, fans, heatsinks, battery clips, fuses, diodes, pots, neon lamps, filament bulbs, 7-seg displays, relays, LEDs, power resistors, everything....all from the garbage dump! Just a matter of harvesting enough junked boards. The Trashtop was definitely unusual though.

    -Tomato
  • Qwaszx72Qwaszx72 Posts: 30
    edited 2011-01-19 13:16
    Great job on your first project!

    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
  • TomatoTomato Posts: 23
    edited 2011-01-19 14:24
    Hey Qwaszx72 wow thanks man! I still do not have the hang of things like for...next and I'm still kinda wrapping my head around declaring variables, so i'm going through the code you wrote step by step.

    I appreciate it!:thumb:

    -Tomato
  • AttorneyJonAttorneyJon Posts: 10
    edited 2011-01-22 18:34
    Nice project! And, uh, well, doesn't look so [very] simple to me! :) I would give yourself some more credit.

    Good work!
Sign In or Register to comment.