Xmas tree serial LED blinking example code
Fork of JBB_SerialLED_test by
main.cpp@1:832ea8c566a8, 2016-12-12 (annotated)
- Committer:
- MACRUM
- Date:
- Mon Dec 12 05:55:57 2016 +0000
- Revision:
- 1:832ea8c566a8
- Parent:
- 0:57df396a2de8
- Child:
- 2:20fc73d3b706
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:57df396a2de8 | 1 | /** |
MACRUM | 1:832ea8c566a8 | 2 | * X'mas tree LED blinky using NeoPixel |
jksoft | 0:57df396a2de8 | 3 | * |
MACRUM | 1:832ea8c566a8 | 4 | * Copyright (C) 2016 Toyomasa Watarai (MACRUM) |
jksoft | 0:57df396a2de8 | 5 | */ |
MACRUM | 1:832ea8c566a8 | 6 | |
jksoft | 0:57df396a2de8 | 7 | #include "mbed.h" |
jksoft | 0:57df396a2de8 | 8 | #include "LEDStrip.h" |
jksoft | 0:57df396a2de8 | 9 | |
MACRUM | 1:832ea8c566a8 | 10 | #define START_ID 2 |
MACRUM | 1:832ea8c566a8 | 11 | #define NUM_LEDS 40 |
MACRUM | 1:832ea8c566a8 | 12 | #define MAX_LEVEL 255 |
jksoft | 0:57df396a2de8 | 13 | |
MACRUM | 1:832ea8c566a8 | 14 | DigitalIn rip(p17); |
MACRUM | 1:832ea8c566a8 | 15 | DigitalOut myled(LED1); |
MACRUM | 1:832ea8c566a8 | 16 | |
MACRUM | 1:832ea8c566a8 | 17 | void blink_1(int); |
MACRUM | 1:832ea8c566a8 | 18 | void blink_2(int); |
MACRUM | 1:832ea8c566a8 | 19 | void blink_3(int); |
MACRUM | 1:832ea8c566a8 | 20 | |
MACRUM | 1:832ea8c566a8 | 21 | void blink_1(int loops) |
MACRUM | 1:832ea8c566a8 | 22 | { |
MACRUM | 1:832ea8c566a8 | 23 | while(loops--) { |
MACRUM | 1:832ea8c566a8 | 24 | for (int idx = 0; idx < 8; idx++) { |
MACRUM | 1:832ea8c566a8 | 25 | for (int j = START_ID; j <= (START_ID + NUM_LEDS); j += 7) { |
MACRUM | 1:832ea8c566a8 | 26 | tapeSet(j+((idx+0)%7), 0, 0, 0); |
MACRUM | 1:832ea8c566a8 | 27 | tapeSet(j+((idx+1)%7), 0, 0, MAX_LEVEL); |
MACRUM | 1:832ea8c566a8 | 28 | tapeSet(j+((idx+2)%7), 0, MAX_LEVEL, 0); |
MACRUM | 1:832ea8c566a8 | 29 | tapeSet(j+((idx+3)%7), 0, MAX_LEVEL, MAX_LEVEL); |
MACRUM | 1:832ea8c566a8 | 30 | tapeSet(j+((idx+4)%7), MAX_LEVEL, 0, 0); |
MACRUM | 1:832ea8c566a8 | 31 | tapeSet(j+((idx+5)%7), MAX_LEVEL, 0, MAX_LEVEL); |
MACRUM | 1:832ea8c566a8 | 32 | tapeSet(j+((idx+6)%7), MAX_LEVEL, MAX_LEVEL, 0); |
MACRUM | 1:832ea8c566a8 | 33 | tapeSet(j+((idx+7)%7), MAX_LEVEL, MAX_LEVEL, MAX_LEVEL); |
MACRUM | 1:832ea8c566a8 | 34 | } |
MACRUM | 1:832ea8c566a8 | 35 | tapeSend(); |
MACRUM | 1:832ea8c566a8 | 36 | wait_ms(300); |
MACRUM | 1:832ea8c566a8 | 37 | } |
MACRUM | 1:832ea8c566a8 | 38 | } |
MACRUM | 1:832ea8c566a8 | 39 | } |
MACRUM | 1:832ea8c566a8 | 40 | |
MACRUM | 1:832ea8c566a8 | 41 | |
MACRUM | 1:832ea8c566a8 | 42 | void blink_2(int loops) |
MACRUM | 1:832ea8c566a8 | 43 | { |
MACRUM | 1:832ea8c566a8 | 44 | while(loops--) { |
MACRUM | 1:832ea8c566a8 | 45 | for( int i=0 ; i <= MAX_LEVEL ; i++ ) { |
MACRUM | 1:832ea8c566a8 | 46 | for (int j = START_ID; j <= (START_ID + NUM_LEDS); j++) { |
MACRUM | 1:832ea8c566a8 | 47 | tapeSet(j, i, 0, 0); // LED No , Red , Green , Blue |
MACRUM | 1:832ea8c566a8 | 48 | } |
MACRUM | 1:832ea8c566a8 | 49 | tapeSend(); |
MACRUM | 1:832ea8c566a8 | 50 | wait_ms(10); |
MACRUM | 1:832ea8c566a8 | 51 | } |
MACRUM | 1:832ea8c566a8 | 52 | for( int i=0 ; i <= MAX_LEVEL ; i++ ) { |
MACRUM | 1:832ea8c566a8 | 53 | for (int j = START_ID; j <= (START_ID + NUM_LEDS); j++) { |
MACRUM | 1:832ea8c566a8 | 54 | tapeSet(j, MAX_LEVEL, i, 0); // LED No , Red , Green , Blue |
MACRUM | 1:832ea8c566a8 | 55 | } |
MACRUM | 1:832ea8c566a8 | 56 | tapeSend(); |
MACRUM | 1:832ea8c566a8 | 57 | wait_ms(10); |
MACRUM | 1:832ea8c566a8 | 58 | } |
MACRUM | 1:832ea8c566a8 | 59 | for( int i=0 ; i <= MAX_LEVEL ; i++ ) { |
MACRUM | 1:832ea8c566a8 | 60 | for (int j = START_ID; j <= (START_ID + NUM_LEDS); j++) { |
MACRUM | 1:832ea8c566a8 | 61 | tapeSet(j, MAX_LEVEL, MAX_LEVEL, i); // LED No , Red , Green , Blue |
MACRUM | 1:832ea8c566a8 | 62 | } |
MACRUM | 1:832ea8c566a8 | 63 | tapeSend(); |
MACRUM | 1:832ea8c566a8 | 64 | wait_ms(10); |
MACRUM | 1:832ea8c566a8 | 65 | } |
MACRUM | 1:832ea8c566a8 | 66 | for( int i=MAX_LEVEL ; i >= 0 ; i-- ) { |
MACRUM | 1:832ea8c566a8 | 67 | for (int j = START_ID; j <= (START_ID + NUM_LEDS); j++) { |
MACRUM | 1:832ea8c566a8 | 68 | tapeSet(j, i, i, i); // LED No , Red , Green , Blue |
MACRUM | 1:832ea8c566a8 | 69 | } |
MACRUM | 1:832ea8c566a8 | 70 | tapeSend(); |
MACRUM | 1:832ea8c566a8 | 71 | wait_ms(10); |
MACRUM | 1:832ea8c566a8 | 72 | } |
MACRUM | 1:832ea8c566a8 | 73 | } |
MACRUM | 1:832ea8c566a8 | 74 | } |
MACRUM | 1:832ea8c566a8 | 75 | |
MACRUM | 1:832ea8c566a8 | 76 | void blink_3(int loops) |
MACRUM | 1:832ea8c566a8 | 77 | { |
MACRUM | 1:832ea8c566a8 | 78 | while(loops--) { |
MACRUM | 1:832ea8c566a8 | 79 | for( int i = START_ID ; i <= (START_ID + NUM_LEDS); i++) { |
MACRUM | 1:832ea8c566a8 | 80 | for (int j = (START_ID + NUM_LEDS); j >= START_ID; j--) { |
MACRUM | 1:832ea8c566a8 | 81 | tapeSet(j, MAX_LEVEL / ((( j - i ) - 1) * 5), 0, 0); // LED No , Red , Green , Blue |
MACRUM | 1:832ea8c566a8 | 82 | } |
MACRUM | 1:832ea8c566a8 | 83 | tapeSend(); |
MACRUM | 1:832ea8c566a8 | 84 | wait_ms(50); |
MACRUM | 1:832ea8c566a8 | 85 | } |
MACRUM | 1:832ea8c566a8 | 86 | } |
MACRUM | 1:832ea8c566a8 | 87 | } |
MACRUM | 1:832ea8c566a8 | 88 | |
MACRUM | 1:832ea8c566a8 | 89 | void clear(void) |
MACRUM | 1:832ea8c566a8 | 90 | { |
MACRUM | 1:832ea8c566a8 | 91 | for (int j = START_ID; j <= (START_ID + NUM_LEDS); j++) { |
MACRUM | 1:832ea8c566a8 | 92 | tapeSet(j, 0, 0, 0); // LED No , Red , Green , Blue |
MACRUM | 1:832ea8c566a8 | 93 | } |
MACRUM | 1:832ea8c566a8 | 94 | tapeSend(); |
MACRUM | 1:832ea8c566a8 | 95 | } |
MACRUM | 1:832ea8c566a8 | 96 | |
MACRUM | 1:832ea8c566a8 | 97 | int main() |
MACRUM | 1:832ea8c566a8 | 98 | { |
MACRUM | 1:832ea8c566a8 | 99 | tapeInit(0, START_ID + NUM_LEDS); // freq( 0:default ) , SerialLED num |
MACRUM | 1:832ea8c566a8 | 100 | rip.mode(PullDown); |
MACRUM | 1:832ea8c566a8 | 101 | |
MACRUM | 1:832ea8c566a8 | 102 | int sensor = rip.read(); |
MACRUM | 1:832ea8c566a8 | 103 | while(1) { |
MACRUM | 1:832ea8c566a8 | 104 | if (sensor == 1) { |
MACRUM | 1:832ea8c566a8 | 105 | myled = 1; |
MACRUM | 1:832ea8c566a8 | 106 | blink_1(6); |
MACRUM | 1:832ea8c566a8 | 107 | blink_2(1); |
MACRUM | 1:832ea8c566a8 | 108 | blink_3(5); |
MACRUM | 1:832ea8c566a8 | 109 | clear(); |
MACRUM | 1:832ea8c566a8 | 110 | } |
MACRUM | 1:832ea8c566a8 | 111 | myled = 0; |
MACRUM | 1:832ea8c566a8 | 112 | wait(0.5); |
MACRUM | 1:832ea8c566a8 | 113 | sensor = rip.read(); |
MACRUM | 1:832ea8c566a8 | 114 | |
MACRUM | 1:832ea8c566a8 | 115 | } |
MACRUM | 1:832ea8c566a8 | 116 | } |