Radar effect using depth data sent over the network from an Xbox 360 Kinect which controls an LED strip

Dependencies:   EthernetInterface LPD8806 mbed-rtos mbed

Revision:
2:23fb8e32cf1b
Parent:
1:a5690556741c
--- a/main.cpp	Thu Jul 21 13:14:17 2016 +0000
+++ b/main.cpp	Fri Aug 05 10:42:30 2016 +0000
@@ -10,8 +10,46 @@
 LPD8806 strip = LPD8806(36);
 
 Serial pc(USBTX, USBRX);
+InterruptIn KITT(PTA4);
+
+//KITT bouncing light effect
+void scanner() {
+    uint8_t r=127;
+    uint8_t g=0;
+    uint8_t b=0;
+    uint8_t delay=50;
+    int i, j, pos, dir;
+
+    pos = 0;
+    dir = 1;
+
+    for (i=0; i<((strip.numPixels()-1) * 8); i++) {
+        strip.setPixelColor(pos - 2, strip.Color(r/4, g/4, b/4));
+        strip.setPixelColor(pos - 1, strip.Color(r/2, g/2, b/2));
+        strip.setPixelColor(pos, strip.Color(r, g, b));
+        strip.setPixelColor(pos + 1, strip.Color(r/2, g/2, b/2));
+        strip.setPixelColor(pos + 2, strip.Color(r/4, g/4, b/4));
+
+        strip.show();
+        wait_ms(delay);
+
+        for (j=-2; j<= 2; j++)
+            strip.setPixelColor(pos+j, strip.Color(0,0,0));
+
+        pos += dir;
+        if (pos < 0) {
+            pos = 1;
+            dir = -dir;
+        } else if (pos >= strip.numPixels()) {
+            pos = strip.numPixels() - 2;
+            dir = -dir;
+        }
+    }
+}
 
 int main() {
+    KITT.rise(&scanner);
+    
     EthernetInterface eth;
     eth.init(); //Use DHCP
     eth.connect();
@@ -46,7 +84,7 @@
             }      
             else if(buffer[i] == 120) //x = invalid data
             {
-                rawdata_array[index] = -1;
+                rawdata_array[index] = 30; //-1 for off.  30 for max brightness
                 index++;
                 data_string = ""; //clear string
             }
@@ -66,19 +104,19 @@
             }
         }
         
-        int length = 36; //32 LEDs on strip
-        int m_defaultRange = -1;
+        int length = 36; //36 LEDs on strip
+        int m_defaultRange = -1; 
         int m_minRange = 30;
         int m_maxRange = 250;
         double m_minData = 127.0;
         double m_maxData = 0.0;
         double m_defaultData = 0.0;
     
-        double bucketSize = 640 / length; //double bucketSize = values.size() / length(); = 20
+        double bucketSize = 640 / length; 
 
-        for(int bucket=0; bucket<length; bucket++) //for(int bucket=0; bucket<length(); bucket++)
+        for(int bucket=0; bucket<length; bucket++) 
         {
-            int bucketStart = bucketSize*bucket; //0-620 = 
+            int bucketStart = bucketSize*bucket; 
             int bucketEnd = bucketSize*(bucket+1) - 1; 
 
             double accumulator = 0;
@@ -86,7 +124,6 @@
             for(int index=bucketStart; index<=bucketEnd; index++)
             {
                 double value = (double)rawdata_array[index];
-                //printf("index=%i\tvalue=%f\n", index, value);
                 if(value != m_defaultRange)
                 {
                     accumulator +=  value;
@@ -105,9 +142,10 @@
             double score = m_defaultData;
             if ((average != m_defaultRange) && (average >= m_minRange) && (average <= m_maxRange))
             {
-                score = average * (m_maxData - m_minData) / (m_maxRange - m_minRange) + m_minData; //from 0-127
+                score = average * (m_maxData - m_minData) / (m_maxRange - m_minRange) + m_minData; 
                 if(score > 0)
                 {
+                    //creates non-linear scaling for LED brightness
                     float x_scale = 18.142857;
                     float x = score/x_scale;
                     float y = pow(2, x);
@@ -121,8 +159,12 @@
                 score_array[bucket] = score;
             }
             //printf("bucket:%i, score=%i\r\n", bucket, score_array[bucket]);
+            
+            /*                  blue->red
+            strip.setPixelColor(bucket, strip.Color(score_array[bucket],0,127-score_array[bucket]));
+            */
          
-            /* //for using the full colour spectrum
+            /*                  blue->green->red
             int cyan_start=33;
             int green_start=66;
             int yellow_start=99;