Code corruption? Thoughts?

And no, I haven't caught it at the end of the day but I'll need to start checking.

I'm wondering about sensor initialization. I will look at that part of the code. Some drivers return a pass/fail boolean flag rather than just blindly trying to set something up. But if you don't check the flag, it's not much use. I don't see any begin() method for the TCS34725, though... just an object declaration. This is probably the cause... the Adafruit example sketch does.

if (tcs.begin()) {
    //Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1); // halt!
  }

In your case, no serial connection exists, so to eliminate any mystery, you could, for example, set all the LEDs to red if there is a sensor setup failure.

To be clear, though, I think failure to call begin(), is the actual problem.

So basically I should be UN-commenting this stuff.

 /*

  if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1); // halt!
  }

  */

Shoot, I didn't even see that, because it was commented! Yes, you need it. In a stand alone environment, a class constructor is limited in what kinds of initialization it can perform. So there are begin() methods for that.

Ahhh okok. So it's been commented all along though, do you think i was just getting lucky the past 4 months and it skated by or what?

Regardless I'll uncomment it and upload it to the 40 arduinos. That'll be my "fun" project for tomorrow morning.

Yeah.

1 Like

Hey, I really appreciate the insight on this, like I said, I'm an artist just trying to get some project running. This code stuff is kinda dark magic to me. I'd be loosing my mind if it weren't for helpful souls such as you. THANK YOU.

If you don't want to be blind as to what is happening, you can add a visual indication if someting is wrong; e.g. blink the built-in led.

void setup() {

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);


  if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1) // halt!
    {
      digitalWrite(LED_BUILTIN, HIGH);
      delay(1000);
      digitalWrite(LED_BUILTIN, LOW);
      delay(1000);
    }
  }

If you move

  strip.begin();
  strip.show(); // Initialize all pixels to 'off'

to before the line with tcs.begin(), you can flash the neopixels instead of the built-in led when initialisation of the TCS fails.

1 Like

I hate that I had to come back to this thread but I've apparently really ticked off some circuitry god. After running fine for a week the same problem hit another one of the boards. It's just hanging there, no response until I upload the code to the board again then it's FINE! I adjusted the code a little so that after it finds the sensor it changes the Neopixel strip green for 2 seconds but its not even making it that far and that's very early in the setup..... I'm at a loss here. :tired_face:


#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include <Adafruit_NeoPixel.h>
#include "Flora_Pianoglove.h"


// our RGB -> eye-recognized gamma color
byte gammatable[256];

// color sensor
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_2_4MS, TCS34725_GAIN_4X); // integreation times can be 2_4MS, 24MS, 50MS, 101MS, 154MS, 700MS -gain can be 1X, 4X, 16X, 60X
// 24 pixels on pin 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(24, 6, NEO_GRB + NEO_KHZ800);


void setup() {



strip.begin();
strip.show();

 

   
  if (tcs.begin()) {
    Serial.println("Found sensor");
    {
      
       for(int i = 0; i < 25 ; i++)
    strip.setPixelColor(i, strip.Color(10, 80, 10));
   strip.show(); 
   delay(2000);
      }
  } else {
    {
   for(int i = 0; i < 25 ; i++)
    strip.setPixelColor(i, strip.Color(100, 10, 0));
   strip.show(); 
   

}
    Serial.println("No TCS34725 found ... check your connections");
    while (1); // halt!

  
  
  
  }



  // thanks PhilB for this gamma table!
  // it helps convert RGB colors to what humans see
  for (int i=0; i<256; i++) {
    float x = i;
    x /= 255;
    x = pow(x, 3.3); //WAS 3.3
    x *= 255;
      
    gammatable[i] = x;      

    Serial.println(gammatable[i]);
  }
  
}

void loop() {


   

  uint16_t clear, red, green, blue;


  delay(80);  // takes 50ms to read 

  
  tcs.getRawData(&red, &green, &blue, &clear);
  delay(40);



  // Figure out some basic hex code for visualization
  uint32_t 
  sum = red;
  sum += green;
  sum += blue;
  sum = clear;
  float r, g, b;
  r = red; r /= sum;
  g = green; g /= sum;
  b = blue; b /= sum;
  r *= 620; g *= 555; b *= 510; //HERE IS WHERE WE LIVE NOW WAS 255, 240, 220
  if (r > 255) r = 255;
  if (g > 255) g = 255;
  if (b > 255) b = 255;
  


 

  // OK we have to find the two primary colors
  // check if blue is smallest. MEME: fix for 'white'
  float remove, normalize;
  if ((b < g) && (b < r)) {
    remove = b;
    normalize = max(r-b, g-b);
  } else if ((g < b) && (g < r)) {
    remove = g;
    normalize = max(r-g, b-g);
  } else {
    remove = r;
    normalize = max(b-r, g-r);
  }
 // get rid of minority report
  float rednorm = r - remove;
  float greennorm = g - remove;
  float bluenorm = b - remove;
  // now normalize for the highest number
  rednorm /= normalize;
  greennorm /= normalize;
  bluenorm /= normalize;


  
  for(int i = 0; i < 25 ; i++)
{
   strip.setPixelColor(i, strip.Color(gammatable[(int)r], gammatable[(int)g], gammatable[(int)b]));
}
strip.show();

 delay(20);

 
  

 
}





RgbColor HsvToRgb(HsvColor hsv)
{
    RgbColor rgb;
    unsigned char region, remainder, p, q, t;

    if (hsv.s == 0)
    {
        rgb.r = hsv.v;
        rgb.g = hsv.v;
        rgb.b = hsv.v;
        return rgb;
    }

    region = hsv.h / 43;
    remainder = (hsv.h - (region * 43)) * 6; 

    p = (hsv.v * (255 - hsv.s)) >> 8;
    q = (hsv.v * (255 - ((hsv.s * remainder) >> 8))) >> 8;
    t = (hsv.v * (255 - ((hsv.s * (255 - remainder)) >> 8))) >> 8;

    switch (region)
    {
        case 0:
            rgb.r = hsv.v; rgb.g = t; rgb.b = p;
            break;
        case 1:
            rgb.r = q; rgb.g = hsv.v; rgb.b = p;
            break;
        case 2:
            rgb.r = p; rgb.g = hsv.v; rgb.b = t;
            break;
        case 3:
            rgb.r = p; rgb.g = q; rgb.b = hsv.v;
            break;
        case 4:
            rgb.r = t; rgb.g = p; rgb.b = hsv.v;
            break;
        default:
            rgb.r = hsv.v; rgb.g = p; rgb.b = q;
            break;
    }

    return rgb;
}

HsvColor RgbToHsv(RgbColor rgb)
{
    HsvColor hsv;
    unsigned char rgbMin, rgbMax;

    rgbMin = rgb.r < rgb.g ? (rgb.r < rgb.b ? rgb.r : rgb.b) : (rgb.g < rgb.b ? rgb.g : rgb.b);
    rgbMax = rgb.r > rgb.g ? (rgb.r > rgb.b ? rgb.r : rgb.b) : (rgb.g > rgb.b ? rgb.g : rgb.b);

    hsv.v = rgbMax;
    if (hsv.v == 0)
    {
        hsv.h = 0;
        hsv.s = 0;
        return hsv;
    }

    hsv.s = 255 * long(rgbMax - rgbMin) / hsv.v;
    if (hsv.s == 0)
    {
        hsv.h = 0;
        return hsv;
    }

    if (rgbMax == rgb.r)
        hsv.h = 0 + 43 * (rgb.g - rgb.b) / (rgbMax - rgbMin);
    else if (rgbMax == rgb.g)
        hsv.h = 85 + 43 * (rgb.b - rgb.r) / (rgbMax - rgbMin);
    else
        hsv.h = 171 + 43 * (rgb.r - rgb.g) / (rgbMax - rgbMin);

    return hsv;

}