Colour sensor + LED strip video

[UPDATE] RGB Slider – LED Strip with RGB slider 

As requested here is an example of my colour sensor in continuous mode setting the colours of the LED strip. The code below uses the tweaks shown in these two posts:

1) http://stevenjohnston.co.uk/2013/04/15/colour-sensor-on-net-gadgeteer-tcs3414/

2) http://stevenjohnston.co.uk/2013/04/11/individually-addressable-multicolour-leds-on-net-gadgeteer/

Video

The colour changes are much better that this video captures but it is hard to record them (Rather than looking at the ‘white’ over saturated LED, look at the surrounding colour!) In the first few seconds of the video I reset the entire 5m of LEDs and you can see the delay in sending the messages all the way to the last LED. Running the SPI bus at 14MHz I get a refresh of about 5hz (5 full refreshes per second)

[youtube=http://www.youtube.com/watch?v=Fo0yq2GQGaU]

Source code

  1. using System;
  2. using System.Collections;
  3. using System.Threading;
  4. using Microsoft.SPOT;
  5. using Microsoft.SPOT.Presentation;
  6. using Microsoft.SPOT.Presentation.Controls;
  7. using Microsoft.SPOT.Presentation.Media;
  8. using Microsoft.SPOT.Touch;
  9.  
  10. using Gadgeteer.Networking;
  11. using GT = Gadgeteer;
  12. using Gadgeteer.Interfaces;
  13. using GTM = Gadgeteer.Modules;
  14. using Gadgeteer.Modules.GHIElectronics;
  15.  
  16. namespace MagicLights
  17. {
  18.     public partial class Program
  19.     {
  20.         FEZtive feztive;
  21.         void ProgramStarted()
  22.         {
  23.             Debug.Print("Program Started");
  24.             //Calibrate the colour sensor
  25.             colorSense.SetIntegrationTime(ColorSense.TimingRegister.FallingEdge_FreeRunning_400ms);//Set the time span for integration
  26.             colorSense.SetGain(ColorSense.GainRegister.Gain16x_PreScalerDivide4);//Set Gain and PreScalar.
  27.  
  28.             //Init the LED strip (test other SPI bus speeds)
  29.             feztive = new GTM.GHIElectronics.FEZtive(9);
  30.             //feztive.Initialize(160, 500);//OK
  31.             //feztive.Initialize(160, 1000);//OK
  32.             //feztive.Initialize(160, 2000);//OK
  33.             //feztive.Initialize(160, 4000);//OK
  34.             //feztive.Initialize(160, 8000);//OK
  35.             //feztive.Initialize(160, 16000);//Some LEDS are the wrong colour
  36.             //feztive.Initialize(160, 12000);//OK
  37.             //feztive.Initialize(160, 14000);//OK
  38.             //feztive.Initialize(160, 15000);//Some LEDS are the wrong colour
  39.             //feztive.Initialize(160, 14500);//Some LEDS are the wrong colour
  40.             feztive.Initialize(160, 14000);//OK
  41.  
  42.             //Show some colours
  43.             feztive.SetAll(feztive.Black);
  44.             // Set every LED to Red
  45.             feztive.SetAll(feztive.Red);
  46.             // Set every LED to Red
  47.             feztive.SetAll(feztive.Red);
  48.             // Set every LED to Green
  49.             feztive.SetAll(feztive.Green);
  50.  
  51.             //Toggle the colour sensor white LEDs
  52.             button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
  53.  
  54.             //Threads: http://blogs.msdn.com/b/netmfteam/archive/2011/01/17/threads-and-thread-priorities-in-netmf.aspx
  55.             //Dont block the main thread/dispatcher          
  56.             Thread t = new Thread(ContiniousReader);
  57.             t.Start();
  58.         }
  59.  
  60.         //Some variables for debug
  61.         uint r = 0, g = 0, b = 0, c = 0;
  62.         private void ContiniousReader()
  63.         {
  64.             //Loop forever – seperate threads!
  65.             while (true)
  66.             {
  67.                 ColorSense.ColorChannels cChan = colorSense.ReadColorChannels();
  68.                 if (cChan.Red > r) { r = cChan.Red; }
  69.                 Debug.Print("RED: " + cChan.Red + " MAX" + r);//DEBUG – check for saturation
  70.                 if (cChan.Green > g) { g = cChan.Green; }
  71.                 Debug.Print("GREEN: " + cChan.Green + " MAX" + g);
  72.                 if (cChan.Blue > b) { b = cChan.Blue; }
  73.                 Debug.Print("BLUE: " + cChan.Blue + " MAX" + b);
  74.                 if (cChan.Clear > c) { c = cChan.Clear; }
  75.                 Debug.Print("CLEAR: " + cChan.Clear + " MAX" + c);
  76.                 GTM.GHIElectronics.Color newColour = new GTM.GHIElectronics.Color(
  77.                   normaliseColourChannel(cChan.Red),
  78.                   normaliseColourChannel(cChan.Green),
  79.                   normaliseColourChannel(cChan.Blue));
  80.                 feztive.SetAll(newColour);
  81.  
  82.             }
  83.         }
  84.  
  85.  
  86.         // Take values in the range 0-65k and scale to 0-127       
  87.         byte normaliseColourChannel(uint value)
  88.         {
  89.             var res = ((value / 65536.0) * 127.0);
  90.             return (byte)res;
  91.         }
  92.  
  93.         bool ledStatus = false;//Last known state
  94.         void button_ButtonPressed(Button sender, Button.ButtonState state)
  95.         {
  96.             ledStatus = !ledStatus;
  97.             colorSense.ToggleOnboardLED(ledStatus);
  98.         }
  99.     }
  100. }

Leave a Reply

Your email address will not be published. Required fields are marked *