0

tcs34725 Hangs
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

tcs34725 Hangs

by PascalPossum on Mon Feb 22, 2021 7:02 pm

I have a bit of code that was working quite well but now hangs my sketch. I'm sure I've done something bad but I can't seem to find it. I've changed out the color sensor and even the Arduino with the same problem so I'm relatively sure it's the code. (Note: I've stripped this out of the full sketch so it may not compile but you get the point. Full sketch is attached as a file.)

The monitor output I'm getting is this:

Loop Duration: 64
Slot at the Top: 3
Top has bead
Reading the current color
Ra

That's where it stops. The "Reading the current color" is writing just before the tcs.getRawData. The "Ra" is the start of the next Serial.print but this is where it hangs. Strange that it returns and STARTS to do something else but crashes.

Any help would be appreciated.

Thanks,
Pascal

Code: Select all | TOGGLE FULL SIZE
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);

// Color values
int minR, minG, minB = 0;
int maxR, maxG, maxB = 255;

struct rgbType {
  int r = 0;
  int g = 0;
  int b = 0;
};
//===========================================================
// readRBG
//  Reads the color sensor to retrieve the RGB value
//===========================================================
rgbType readRGB(bool calibrating = false) {

  rgbType retval;

  if (debug) {
    Serial.println("Reading the current color");
  }

  uint16_t red, grn, blu, clr;
  tcs.setInterrupt(false);           // turn on LED
  delay(60);                         // takes 50ms to read
  tcs.getRawData(&red, &grn, &blu, &clr);
  tcs.setInterrupt(true);            // turn off LED

  if (debug) {
    Serial.print("Raw values: ");
    Serial.print("["); Serial.print(red);
    Serial.print(","); Serial.print(grn);
    Serial.print(","); Serial.print(blu);
    Serial.print(","); Serial.print(clr);
    Serial.println("]");
  }

  uint32_t sum = clr;
  float fltR, fltG, fltB;
  int r2, g2, b2;

  fltR = red;
  fltR /= sum;

  fltG = grn;
  fltG /= sum;

  fltB = blu;
  fltB /= sum;

  fltR *= 256;
  fltG *= 256;
  fltB *= 256;

  // If we're calibrating, don't do the mapping
  if (calibrating) {
    r2 = fltR;
    g2 = fltG;
    b2 = fltB;
  } else {
    r2 = map(fltR, minR, maxR, 0, 255);
    g2 = map(fltG, minG, maxG, 0, 255);
    b2 = map(fltB, minB, maxB, 0, 255);
  }

  retval.r = constrain(r2, 0, 255);
  retval.g = constrain(g2, 0, 255);
  retval.b = constrain(b2, 0, 255);

  if (debug) {
    Serial.print(" RGB ["); Serial.print(retval.r);
    Serial.print(","); Serial.print(retval.g);
    Serial.print(","); Serial.print(retval.b);
    Serial.println("]");
  }

  return retval;

}//===========================================================
// respondToSerial
//  Responds to input from the serial monitor
//===========================================================
void respondToSerial() {
  char serialIn;
  int eAction, lSlot;
  rgbType rgbVal;

  serialIn = Serial.read();
  Serial.println(serialIn);

  if (serialIn == '1') {
    pauseRun = !pauseRun;
    runOne = !runOne;
    Serial.println("Running one loop");

  } else if (serialIn == 'c') {
    Serial.println("Current Color");
    rgbVal = readRGB();
    Serial.print(" ["); Serial.print(rgbVal.r);
    Serial.print(","); Serial.print(rgbVal.g);
    Serial.print(","); Serial.print(rgbVal.b);
    Serial.println("]");
 

  } else if (serialIn == 'd') {
    debug = !debug;
    Serial.print("Debug set to ");
    Serial.println(debug);

  } else if (serialIn == 'l') {
    Serial.print("Loop Time ");
    Serial.println(looptime);

  } else if (serialIn == 'p') {
    pauseRun = !pauseRun;
    if (pauseRun) {
      Serial.print("Pausing");
    } else {
      Serial.print("Resuming");
    }
  }

}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
// setup
//  Runs once to set up the initial parameters
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
void setup() {
  Serial.begin(9600);

  Serial.println("Starting Up...");
 
  // Initialize the RGB sensor
  if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1);
  }
}

//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
// loop
//  Main loop.  Repeats indefinitely
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
void loop() {
  int aPos;

  if (Serial.available() > 0) {
    respondToSerial();
  }


  if (!pauseRun) {
    if (debug) {
      Serial.print("Slot at the Top: ");
      Serial.println(topSlot);
    }

    // Reset the slot at the top of the wheel
    resetSlot(topSlot);


    // Go through each of the slots and perform the action for their position
    for (slot = 0; slot < slotCnt; slot++) {
      aPos = wheel[slot].actionPos;
      switch (actions[aPos].action) {
        case ColorSensor:
          if (!wheel[slot].empty) {
            // Get the color of this bead
            wheel[slot].color = readRGB ();
            if (debug) {
              Serial.print(" Slot ");
              Serial.print("["); Serial.print(wheel[slot].color.r);
              Serial.print(","); Serial.print(wheel[slot].color.g);
              Serial.print(","); Serial.print(wheel[slot].color.b);
              Serial.println("]");
            }
          }
    }

    looptime = millis() - lMillis;
    // This will give us a time per loop
    if (debug) {
      Serial.print("Loop Duration: "); Serial.print(looptime); Serial.println(" ");
    }
    lMillis = millis();

    if (runOne) {
      pauseRun = !pauseRun;
      runOne = !runOne;
    }
  }
}
Attachments
ColorSorterLogic.ino
(14.95 KiB) Downloaded 2 times

PascalPossum
 
Posts: 4
Joined: Mon Feb 22, 2021 6:44 pm

Re: tcs34725 Hangs

by adafruit_support_bill on Mon Feb 22, 2021 7:07 pm

Does the example sketch from the library still work?

adafruit_support_bill
 
Posts: 80311
Joined: Sat Feb 07, 2009 10:11 am

Re: tcs34725 Hangs

by PascalPossum on Mon Feb 22, 2021 9:35 pm

Yes. I just ran the example sketch without any pin changes and it works just fine.

PascalPossum
 
Posts: 4
Joined: Mon Feb 22, 2021 6:44 pm

Re: tcs34725 Hangs

by adafruit_support_bill on Mon Feb 22, 2021 9:47 pm

Yes. I just ran the example sketch without any pin changes and it works just fine.

Good. That rules out a problem with the i2c bus.

What processor are you running this on?

adafruit_support_bill
 
Posts: 80311
Joined: Sat Feb 07, 2009 10:11 am

Re: tcs34725 Hangs

by PascalPossum on Mon Feb 22, 2021 10:14 pm

Arduino Uno

PascalPossum
 
Posts: 4
Joined: Mon Feb 22, 2021 6:44 pm

Re: tcs34725 Hangs

by adafruit_support_bill on Tue Feb 23, 2021 7:13 am

It is possible that you are running short of dynamic memory (SRAM). https://learn.adafruit.com/memories-of- ... ot-dot-dot

There are some tips here on optimizing SRAM usage. In particular, it looks like you have a lot of string literals that could be moved to Flash.
https://learn.adafruit.com/memories-of- ... izing-sram

adafruit_support_bill
 
Posts: 80311
Joined: Sat Feb 07, 2009 10:11 am

Re: tcs34725 Hangs

by PascalPossum on Tue Feb 23, 2021 12:01 pm

There's definitely some memory issues going on. I went a few paths trying to clean up the string literals but what ultimately worked was calling my "calibrateRGB" function during setup. A read from the tcs early on seems to allocate that memory block and it's happily chugging along now no matter the print statements (granted, I'll be more cautious now...). If I waited until the loop, even the first iteration of loop, it would fail. Something must be fragmented in there once the loop starts and I'll need to spend some more time understanding it. This is my first foray into Arduino so I guess I've got a lot to learn about memory management.

Thanks for your help!!

-Pascal

PascalPossum
 
Posts: 4
Joined: Mon Feb 22, 2021 6:44 pm

Re: tcs34725 Hangs

by adafruit_support_bill on Tue Feb 23, 2021 12:06 pm

Good to hear that's working for you. Thanks for the update.

adafruit_support_bill
 
Posts: 80311
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.