0

newb needs help
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

newb needs help

by skindeepfx on Mon Jan 09, 2017 11:18 am

Good morning everyone

i am pretty new to understanding the Arduino programming and could use some pointers

I am trying to use the 8 led neopixel stick with the RGB sensor TCS34725 and a uno3

i used the chameleon scarf code and it works fine to change colors by pressing the reset button

i know that the code only looks for color at startup

how can i modify the code that the sensor always looks for color changes ?

my instinct tells me it has something to do with a loop and it is probably a very simple solution

Thank you for all of your help

skindeepfx
 
Posts: 11
Joined: Sun Jan 08, 2017 7:37 pm

Re: newb needs help

by adafruit_support_bill on Mon Jan 09, 2017 12:14 pm

You are correct, you need to move some code to the loop.

These lines of code should stay in your setup() function:
Code: Select all | TOGGLE FULL SIZE
  Serial.begin(9600);
  Serial.println("Color View Test!");
 
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
 
  if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    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, 2.5);
    x *= 255;
     
    gammatable[i] = x;     
    //Serial.println(gammatable[i]);
  }
 
  for (int i=0; i<3; i++){ //this sequence flashes the first pixel three times as a countdown to the color reading.
    strip.setPixelColor (0, strip.Color(188, 188, 188)); //white, but dimmer-- 255 for all three values makes it blinding!
    strip.show();
    delay(1000);
    strip.setPixelColor (0, strip.Color(0, 0, 0));
    strip.show();
    delay(500);
  }



The rest can move to the loop() function so it will take a reading every time through the loop and update the pixels to match.

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

Re: newb needs help

by skindeepfx on Mon Jan 09, 2017 2:54 pm

thanks for the swift reply
my code looks like this and compiled
but i only get a white flashing led at startup
must have overlooked something


Code: Select all | TOGGLE FULL SIZE
 #include <Wire.h>
#include "Adafruit_TCS34725.h"
#include <Adafruit_NeoPixel.h>
#define PIN 6
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, 6, NEO_GRB + NEO_KHZ800);


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


Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);

void setup() {
  Serial.begin(9600);
  Serial.println("Color View Test!");
 
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
 
  if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    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, 2.5);
    x *= 255;
     
    gammatable[i] = x;     
    //Serial.println(gammatable[i]);
  }
 
  for (int i=0; i<3; i++){ //this sequence flashes the first pixel three times as a countdown to the color reading.
    strip.setPixelColor (0, strip.Color(188, 188, 188)); //white, but dimmer-- 255 for all three values makes it blinding!
    strip.show();
    delay(1000);
    strip.setPixelColor (0, strip.Color(0, 0, 0));
    strip.show();
    delay(500);
  }
}

void loop() {
uint16_t clear, red, green, blue;

  tcs.setInterrupt(false);      // turn on LED

  delay(60);  // takes 50ms to read
 
  tcs.getRawData(&red, &green, &blue, &clear);

  tcs.setInterrupt(true);  // turn off LED
 
  Serial.print("C:\t"); Serial.print(clear);
  Serial.print("\tR:\t"); Serial.print(red);
  Serial.print("\tG:\t"); Serial.print(green);
  Serial.print("\tB:\t"); Serial.print(blue);

  // 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 *= 256; g *= 256; b *= 256;
  Serial.print("\t");
  Serial.print((int)r, HEX); Serial.print((int)g, HEX); Serial.print((int)b, HEX);
  Serial.println();

 // Serial.print((int)r ); Serial.print(" "); Serial.print((int)g);Serial.print(" ");  Serial.println((int)b );
 // colorWipe(strip.Color(gammatable[(int)r], gammatable[(int)g], gammatable[(int)b]), 0);
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}
Last edited by adafruit_support_bill on Mon Jan 09, 2017 3:14 pm, edited 1 time in total.
Reason: Please use [code] tags when posting code to the forums

skindeepfx
 
Posts: 11
Joined: Sun Jan 08, 2017 7:37 pm

Re: newb needs help

by adafruit_support_bill on Mon Jan 09, 2017 3:17 pm

Code: Select all | TOGGLE FULL SIZE
...
 // Serial.print((int)r ); Serial.print(" "); Serial.print((int)g);Serial.print(" ");  Serial.println((int)b );
 // colorWipe(strip.Color(gammatable[(int)r], gammatable[(int)g], gammatable[(int)b]), 0);
}


You have the "colorWipe()" at the end of the loop commented out, so it never sets the pixel colors.

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

Re: newb needs help

by skindeepfx on Mon Jan 09, 2017 4:32 pm

silly me

Thanks for your help it works fine now

skindeepfx
 
Posts: 11
Joined: Sun Jan 08, 2017 7:37 pm

Re: newb needs help

by adafruit_support_bill on Mon Jan 09, 2017 4:58 pm

Great! Thanks for the follow-up.

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

Please be positive and constructive with your questions and comments.