Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Example sketch showing a few ways to set up the LED strip (NOT a libr…

…ary yet)
  • Loading branch information...
commit 2cc9e46f45bb982c990466ee9fc55ced7277fab7 0 parents
Limor "Ladyada" Fried authored November 13, 2010

Showing 1 changed file with 243 additions and 0 deletions. Show diff stats Hide diff stats

  1. 243  hl1606.pde
243  hl1606.pde
... ...
@@ -0,0 +1,243 @@
  1
+#include "LEDStrip.h"
  2
+
  3
+// dot is S
  4
+// dash is Data
  5
+// x is Clock
  6
+// longdash is latch
  7
+
  8
+#define STRIP_S 2
  9
+#define STRIP_D 3
  10
+#define STRIP_C 4
  11
+#define STRIP_L 5
  12
+byte colorArray[15];
  13
+
  14
+#define BLACK 0b000
  15
+#define WHITE 0b111
  16
+#define RED 0b100
  17
+#define YELLOW 0b110
  18
+#define GREEN 0b010
  19
+#define TEAL 0b011
  20
+#define BLUE 0b001
  21
+#define VIOLET 0b101
  22
+ 
  23
+#define numLEDs (32*5)  // 32 leds per meter
  24
+
  25
+LEDStrip strip = LEDStrip(STRIP_D, STRIP_S, STRIP_L, STRIP_C);
  26
+
  27
+uint8_t leds[numLEDs];
  28
+
  29
+
  30
+void setup(void) {
  31
+  Serial.begin(9600);
  32
+  
  33
+  Serial.print("hello!");
  34
+
  35
+   fillStrip(BLACK);
  36
+  delay(1000);
  37
+
  38
+}
  39
+
  40
+
  41
+void loop(void) { 
  42
+   fillStrip(RED);
  43
+   fillStrip(YELLOW);
  44
+   fillStrip(GREEN);
  45
+   fillStrip(TEAL);
  46
+   fillStrip(BLUE);
  47
+   fillStrip(VIOLET);
  48
+   fillStrip(WHITE);
  49
+   fillStrip(BLACK);
  50
+
  51
+   chaseSingle(RED);
  52
+   chaseSingle(YELLOW);
  53
+   chaseSingle(GREEN);
  54
+   chaseSingle(TEAL);
  55
+   chaseSingle(VIOLET);
  56
+   chaseSingle(WHITE);
  57
+    rainbow();
  58
+  
  59
+}
  60
+
  61
+/**********************************************/
  62
+
  63
+void setLED(uint8_t n, uint8_t color) {
  64
+  uint8_t x;
  65
+  
  66
+  x = 0x80; // latch
  67
+  
  68
+  if (n > numLEDs) return;
  69
+
  70
+  if (color & BLUE) 
  71
+    x |= 0x01;
  72
+  if (color & RED) 
  73
+    x |= 0x04;
  74
+  if (color & GREEN) 
  75
+    x |= 0x10;
  76
+  
  77
+  leds[n] = x;
  78
+}
  79
+
  80
+void fadeLEDin(uint8_t n, uint8_t color) {
  81
+  uint8_t x;
  82
+  
  83
+  x = 0x80; // latch
  84
+  
  85
+  if (n > numLEDs) return;
  86
+
  87
+  if (color & BLUE) 
  88
+    x |= 0x02;
  89
+  if (color & RED) 
  90
+    x |= 0x08;
  91
+  if (color & GREEN) 
  92
+    x |= 0x20;
  93
+  
  94
+  leds[n] = x;
  95
+}
  96
+
  97
+
  98
+void fadeLEDout(uint8_t n, uint8_t color) {
  99
+  uint8_t x;
  100
+  
  101
+  x = 0x80; // latch
  102
+  
  103
+  if (n > numLEDs) return;
  104
+
  105
+  if (color & BLUE) 
  106
+    x |= 0x03;
  107
+  if (color & RED) 
  108
+    x |= 0x0C;
  109
+  if (color & GREEN) 
  110
+    x |= 0x30;
  111
+  
  112
+  leds[n] = x;
  113
+}
  114
+
  115
+uint8_t getLED(uint8_t n) {
  116
+  uint8_t x;
  117
+
  118
+  if (n > numLEDs) return 0;
  119
+  
  120
+  x = leds[n];
  121
+  
  122
+  x &= 0x7F; // get rid of latch
  123
+  
  124
+  uint8_t r, g, b;
  125
+  r = g = b = 0;
  126
+  if (x & 0x3) { b = 1; }
  127
+  if (x & 0xC) { r = 1; } 
  128
+  if (x & 0x30) { g = 1; }
  129
+  
  130
+  return (g << 1) | (r << 2) | b;
  131
+}
  132
+
  133
+
  134
+// this takes about 20ms for a 160 LED strip
  135
+void writestrip(void) {
  136
+  for (uint8_t i=0; i<numLEDs; i++) {
  137
+    strip.pushCmd(leds[numLEDs-1-i]);
  138
+  }
  139
+  strip.latch();
  140
+  
  141
+}
  142
+
  143
+/**********************************************/
  144
+
  145
+void rainbow(void) {
  146
+  uint8_t i;
  147
+
  148
+  for (i=0; i<numLEDs; i+=6) {
  149
+    setLED(i, RED);
  150
+    setLED(i+1, YELLOW);
  151
+    setLED(i+2, GREEN);
  152
+    setLED(i+3, TEAL);
  153
+    setLED(i+4, BLUE);
  154
+    setLED(i+5, VIOLET);
  155
+ 
  156
+  }
  157
+  writestrip();   
  158
+  
  159
+  for (uint8_t j=0; j<numLEDs; j++) {
  160
+    uint8_t savedcolor = getLED(0);
  161
+    for (i=1; i<numLEDs; i++) {
  162
+      setLED(i-1, getLED(i));
  163
+    }
  164
+    setLED(numLEDs-1, savedcolor);
  165
+    writestrip();
  166
+    delay(50);
  167
+  }
  168
+}
  169
+
  170
+
  171
+void rainbowfade(void) {
  172
+  uint8_t i;
  173
+
  174
+  for (i=0; i<numLEDs; i+=6) {
  175
+    setLED(i, RED);
  176
+    setLED(i+1, YELLOW);
  177
+    setLED(i+2, GREEN);
  178
+    setLED(i+3, TEAL);
  179
+    setLED(i+4, BLUE);
  180
+    setLED(i+5, VIOLET);
  181
+ 
  182
+  }
  183
+  writestrip();   
  184
+  
  185
+  for (uint8_t j=0; j<numLEDs; j++) {
  186
+    for (i=0; i<numLEDs; i++) {
  187
+      fadeLEDout(i, getLED(i));
  188
+    }
  189
+    
  190
+    for (i=0; i< 64; i++) {
  191
+          strip.sPulse();
  192
+          delay(10);
  193
+    }
  194
+    
  195
+    uint8_t savedcolor = getLED(0);
  196
+    for (i=1; i<numLEDs; i++) {
  197
+      fadeLEDin(i-1, getLED(i));
  198
+    }
  199
+    //fadeLEDin(numLEDs-1, savedcolor);
  200
+    //writestrip();
  201
+    //for (i=0; i< 64; i++) {
  202
+    //      strip.sPulse();
  203
+    //      delay(10);
  204
+    //}
  205
+  }
  206
+}
  207
+
  208
+void stripOff(void) {
  209
+  // turn all LEDs off!
  210
+  for (uint8_t i=0; i<numLEDs; i++) {
  211
+      setLED(i, BLACK);
  212
+  }
  213
+  writestrip();   
  214
+}
  215
+
  216
+void chaseSingle(uint8_t color) {
  217
+  uint8_t i;
  218
+  
  219
+  // turn everything off
  220
+  for (i=0; i< numLEDs; i++) {
  221
+    setLED(i, BLACK);
  222
+  }
  223
+
  224
+  for (i=0; i< numLEDs; i++) {
  225
+    setLED(i, color);
  226
+    if (i != 0) {
  227
+      setLED(i-1, BLACK);
  228
+    }
  229
+    writestrip();
  230
+    delay(10);  
  231
+  }
  232
+  setLED(numLEDs-1, BLACK);
  233
+}
  234
+
  235
+
  236
+void fillStrip(uint8_t color) {
  237
+  uint8_t i;
  238
+  
  239
+  for (i=0; i<numLEDs; i++) {
  240
+      setLED(i, color);
  241
+      writestrip();   
  242
+  }
  243
+}

0 notes on commit 2cc9e46

Please sign in to comment.
Something went wrong with that request. Please try again.