Dependencies:   mbed

Cylon like sequencer for on board LEDs demo. Turns on LED and shifts it left to right and back again.

Committer:
tombelpasso
Date:
Wed Dec 30 19:41:45 2009 +0000
Revision:
0:1bf2c942b27f

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tombelpasso 0:1bf2c942b27f 1 /*
tombelpasso 0:1bf2c942b27f 2 * Cylon like sequencer for on board LEDs demo.
tombelpasso 0:1bf2c942b27f 3 *
tombelpasso 0:1bf2c942b27f 4 * Turns on LED and shifts it left to right and back again.
tombelpasso 0:1bf2c942b27f 5 *
tombelpasso 0:1bf2c942b27f 6 * Copyright (C) <2009> Tom Belpassso <tombelpasso@gmail.com>
tombelpasso 0:1bf2c942b27f 7 *
tombelpasso 0:1bf2c942b27f 8 * This is free software: you can redistribute it and/or modify
tombelpasso 0:1bf2c942b27f 9 * it under the terms of the GNU General Public License as published by
tombelpasso 0:1bf2c942b27f 10 * the Free Software Foundation, either version 3 of the License, or
tombelpasso 0:1bf2c942b27f 11 * (at your option) any later version.
tombelpasso 0:1bf2c942b27f 12 *
tombelpasso 0:1bf2c942b27f 13 * OneWire/DS18S20 is distributed in the hope that it will be useful,
tombelpasso 0:1bf2c942b27f 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
tombelpasso 0:1bf2c942b27f 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tombelpasso 0:1bf2c942b27f 16 * GNU General Public License for more details.
tombelpasso 0:1bf2c942b27f 17 *
tombelpasso 0:1bf2c942b27f 18 * a copy of the GNU General Public License, see <http://www.gnu.org/licenses/>.
tombelpasso 0:1bf2c942b27f 19 */
tombelpasso 0:1bf2c942b27f 20
tombelpasso 0:1bf2c942b27f 21 #include "mbed.h"
tombelpasso 0:1bf2c942b27f 22 // configuration equates
tombelpasso 0:1bf2c942b27f 23 // time between shifting to next LED
tombelpasso 0:1bf2c942b27f 24 #define SHIFT_TIME 0.04
tombelpasso 0:1bf2c942b27f 25 // total time for sequence
tombelpasso 0:1bf2c942b27f 26 #define REPEAT_TIME 1.0
tombelpasso 0:1bf2c942b27f 27 // blank time between sequences
tombelpasso 0:1bf2c942b27f 28 #define BLANK_TIME REPEAT_TIME - 9 * SHIFT_TIME
tombelpasso 0:1bf2c942b27f 29
tombelpasso 0:1bf2c942b27f 30 BusOut leds(LED1, LED2, LED3, LED4);
tombelpasso 0:1bf2c942b27f 31
tombelpasso 0:1bf2c942b27f 32 int main() {
tombelpasso 0:1bf2c942b27f 33 // i is used to turn on first LED in the sequence
tombelpasso 0:1bf2c942b27f 34 short i = 1;
tombelpasso 0:1bf2c942b27f 35 bool dir = 0 ; // 0 shift left, 1 shift right
tombelpasso 0:1bf2c942b27f 36 leds = 0;
tombelpasso 0:1bf2c942b27f 37 while(1) {
tombelpasso 0:1bf2c942b27f 38 if (dir)
tombelpasso 0:1bf2c942b27f 39 leds = (leds >> 1) + i; // shift down
tombelpasso 0:1bf2c942b27f 40 else
tombelpasso 0:1bf2c942b27f 41 leds = (leds << 1) + i; // shift
tombelpasso 0:1bf2c942b27f 42 wait(SHIFT_TIME);
tombelpasso 0:1bf2c942b27f 43 if (leds != 0) { // if any leds lit, don't turn any more on
tombelpasso 0:1bf2c942b27f 44 i = 0;
tombelpasso 0:1bf2c942b27f 45 }
tombelpasso 0:1bf2c942b27f 46 // see if all LEDs are off
tombelpasso 0:1bf2c942b27f 47 if (leds == 0) {
tombelpasso 0:1bf2c942b27f 48 // leds = 0 ;
tombelpasso 0:1bf2c942b27f 49 if (dir) {
tombelpasso 0:1bf2c942b27f 50 dir = 0 ; // toggle directon
tombelpasso 0:1bf2c942b27f 51 i = 1; // LSB LED on next shift
tombelpasso 0:1bf2c942b27f 52 wait(BLANK_TIME);
tombelpasso 0:1bf2c942b27f 53 } else {
tombelpasso 0:1bf2c942b27f 54 dir = 1;
tombelpasso 0:1bf2c942b27f 55 i = 8; // MSB LED on next shift
tombelpasso 0:1bf2c942b27f 56 wait(SHIFT_TIME);
tombelpasso 0:1bf2c942b27f 57 }
tombelpasso 0:1bf2c942b27f 58 }
tombelpasso 0:1bf2c942b27f 59 }
tombelpasso 0:1bf2c942b27f 60 }