BoeBotTeacher.com

    Resources for teaching with the Parallax Boe-Bot

Exercise: Three Flashing Lights  

Question Posed Here

---------------------------------------------------------------------------------------------------------------

Hint#1
A common approach is to think of each LED in its own Do Loop. But the problem is how to integrate the loops so that the yellow loop would complete twice during the red loop. Instead of three loops, think of describing one loop that will control all three LEDs.

Start by drawing on a piece of paper a horizontal line and marking it off in 250 mSec ticks going up to two seconds. Now write above that line what should be happening at each tick. For example, at 0 mSec, all LEDs go on. At 250 mSec the green goes off, at 500 mSec yellow goes off and green comes on.Can you build the loop now?

 

 

---------------------------------------------------------------------------------------------------------------

Hint #2
Read these hints in a full-size window so that the graphics render correctly.

With this visualization, try creating a loop that has eight PAUSEs of 250 mSec each. We can call them segments and the segments together will make up two seconds. At the beginning of each segment you can turn on/off LEDs as needed for that 250 mSec.

 

---------------------------------------------------------------------------------------------------------------

Hint 3 / Answer
Read these hints in a full-size window so that the graphics render correctly.
You can examine the code below for an answer to the problem. Recall that all text that begins with a single quote (green in your code editor) is comment – it does not affect the code. (Except the two directives at top for hardware and software – they begin with comment but are actually used). I have added a few lines to conduct a little LED test at the beginning of each run. All three LEDs will flash on for a half second and then off for a half second.

 

' {$STAMP BS2}
' {$PBASIC 2.5}
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Leds Flashing at Multiple Frequencies
' John Kauffman www.BoeBotTeacher.com
' ver 1.1
' A program to demonstrate how to flash three LEDs at three different frequencies
' This exercise bridges the ideas from LED flashing to servo control
' ***
' Change hardware directive above to match your stamp microcontroller.

' *** ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' PINS
LedRed PIN 11 ' flash on 1 sec then off 1 sec
LedYellow PIN 13 ' flash on 500 mSec then off 500 mSec
LedGreen PIN 15 ' flash on 250 mSec then off 250 mSec
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' TEST ON/OFF
' First a little test to see that all the LEDS work
HIGH LedRed
HIGH LedYellow
HIGH LedGreen
PAUSE 500
LOW LedRed
LOW LedYellow
LOW LedGreen
PAUSE 500
' End Test ON/OFF
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Flashing Code
' Create Loop that lasts two seconds
' Loop is broken down into eight segments of 250 mSec each
' At beginning of each segment is an opportunity to change any of the LEDS
' Red will stay on four segments (4x250 mSec = 1 sec)
DO        
    '(initiate) elapse = 0
        HIGH LedRed
        HIGH LedYellow
        HIGH LedGreen
    PAUSE 250 'elapse = 250
        LOW LedGreen
    PAUSE 250 'elapse = 500
        LOW LedYellow
        HIGH LedGreen
    PAUSE 250 'elapse = 750
        LOW LedGreen
    PAUSE 250 'elapse = 1000
        LOW LedRed
        HIGH LedYellow
        HIGH LedGreen
    PAUSE 250 'elapse = 1250
        LOW LedGreen
    PAUSE 250 'elapse = 1500
        LOW LedYellow
        HIGH LedGreen
    PAUSE 250 'elapse = 1750
        LOW LedGreen
    PAUSE 250
LOOP