Red LED continuously blinking
nich0las
Posts: 2
in BASIC Stamp
I've searched the forums and help menu for any coding that may help, but I still have no idea how to execute this exercise! My pseudocode is as follows:
1. Red LED on-off once every second
2. Button 1 flash yellow at 20HZ
3. button 2 flash green at 40HZ
4. Buttons 1 & 2 flash both alternately at 10HZ
5. Red LED is always flashing at once a second
I've made the buttons flash the corresponding LEDs (not too worried about the correct HZ yet), but obviously when they break into the if statement the red LED is off. I've thought of adding the red LED high/low command in the IF statements but it would cause the yellow/green LEDs to pause for half a second! I've also tried making the red LED its own loop, but since the loop has to be continuous, the program will only make the red LED blink and ignore the rest of the coding. So far, here's my code:
' {$STAMP BS2}
' {$PBASIC 2.5}
x VAR Byte
y VAR Byte
z VAR Byte
DO
x = 0
y = 0
z = 0
HIGH 13
PAUSE 500
LOW 13
PAUSE 500
IF (IN0 = 1 AND IN1 = 1) THEN
DO
HIGH 15
LOW 14
PAUSE 50
LOW 15
HIGH 14
PAUSE 50
LOW 14
z = z + 1
LOOP UNTIL (z = 10)
ENDIF
IF (IN0 = 1) THEN
DO
HIGH 14
PAUSE 50
LOW 14
PAUSE 50
x = x + 1
LOOP UNTIL (x = 10)
ENDIF
IF (IN1 = 1) THEN
DO
HIGH 15
PAUSE 20
LOW 15
PAUSE 20
y = y + 1
LOOP UNTIL (y = 10)
ENDIF
LOOP
I've thought of GOTO, RETURN, SELECT/CASE.. nothing would work correctly. The only thing I can think of is embedding the red LED high/low code with the IF/THEN statements and just trying to time it out to correspond correctly. Any help would be appreciated though!
1. Red LED on-off once every second
2. Button 1 flash yellow at 20HZ
3. button 2 flash green at 40HZ
4. Buttons 1 & 2 flash both alternately at 10HZ
5. Red LED is always flashing at once a second
I've made the buttons flash the corresponding LEDs (not too worried about the correct HZ yet), but obviously when they break into the if statement the red LED is off. I've thought of adding the red LED high/low command in the IF statements but it would cause the yellow/green LEDs to pause for half a second! I've also tried making the red LED its own loop, but since the loop has to be continuous, the program will only make the red LED blink and ignore the rest of the coding. So far, here's my code:
' {$STAMP BS2}
' {$PBASIC 2.5}
x VAR Byte
y VAR Byte
z VAR Byte
DO
x = 0
y = 0
z = 0
HIGH 13
PAUSE 500
LOW 13
PAUSE 500
IF (IN0 = 1 AND IN1 = 1) THEN
DO
HIGH 15
LOW 14
PAUSE 50
LOW 15
HIGH 14
PAUSE 50
LOW 14
z = z + 1
LOOP UNTIL (z = 10)
ENDIF
IF (IN0 = 1) THEN
DO
HIGH 14
PAUSE 50
LOW 14
PAUSE 50
x = x + 1
LOOP UNTIL (x = 10)
ENDIF
IF (IN1 = 1) THEN
DO
HIGH 15
PAUSE 20
LOW 15
PAUSE 20
y = y + 1
LOOP UNTIL (y = 10)
ENDIF
LOOP
I've thought of GOTO, RETURN, SELECT/CASE.. nothing would work correctly. The only thing I can think of is embedding the red LED high/low code with the IF/THEN statements and just trying to time it out to correspond correctly. Any help would be appreciated though!
Comments
Here's something to try. It may be advanced beyond your present programming skills, but will be worth studying to help you improve (FTR, this program compiles, but I don't have time to hook it up for testing).
Note that I avoid the use of "magic numbers" in the code. You would do yourself a favor by defining your IO points (as you did in your description above) as it makes programs far easier to read and debug. Don't get by the "quick and dirty" gag; it's never quick, it's always dirty.
out15 = x.bit2 ' current state of x.bit2
out14 = ~x.bit2 ' complement of x.bit2
Does that help? The quick and dirty summary.
Regardless of language, breaking a program into discrete pieces that are functional makes good sense. An inexperienced programmer would have a very difficult time with your original listing. It only takes a little extra time to format neatly, and I find it saves hours in debugging time.
http://forums.parallax.com/discussion/161294/5-led-blink-challenge