Hey Guys,
Having trouble with coding my current project and was wondering if I could get a helping hand!
I am currently looking at being able to control the number of LED's lit on my neopixel ring however I would like the colour of the LED to be determined by the colour picked up with the colour sensor.
Here is my code so far, I realize its not the best and there are elements that could do with not being in there. Any Help with the coding would be amazing.
I have gather ed elements of code from these links:
http://www.scienceexposure.com/coding/neopixel-led-controller-arduino/
http://caliban-belgique.e-monsite.com/pages/tuto/capteur-de-couleur.html
CODE:
// Arduino code for Neopixel LED controller
// using a potentiometer and switch button
// (C) Ismail Uddin, 2015
// www.scienceexposure.com
#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include <Adafruit_NeoPixel.h>
#include <Adafruit_NeoPixel.h>
#define PIN 3
Adafruit_NeoPixel strip = Adafruit_NeoPixel(24, PIN, NEO_GRB + NEO_KHZ800);
// Le capteur de couleur se connecte sur les broche SLC et SDA
// Parameter 1 = number of pixels in strip (dans mon cas c'est 16)
// Parameter 2 = pin number (most are valid) (pin 6 dans mon cas)
// 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)
int NbresLED = 25; //Nombres de led sur le strip ou anneau néopixel
int PinLED = 6; // Broche de connection à l'arduino
int potPin = 2;
int val = 0;
int colorVal = 0;
int reading = 0;
int x;
int prevVal = 0;
int switchPin =6;
boolean lastBtn = LOW;
boolean NeopixelColor = false;
boolean lastButton = LOW;
//
// our RGB -> eye-recognized gamma color
byte gammatable[256];
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
void setup() {
// put your setup code here, to run once:
strip.begin();
strip.show();
pinMode(switchPin, INPUT);
Serial.begin(9600);
Serial.println("Color View Test!");
strip.begin();
strip.setBrightness(40);
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 = x; *
_ //Serial.println(gammatable);_
_ }*_
* 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 );*
}
void loop() {
* // put your main code here, to run repeatedly:*
* reading = analogRead(potPin);*
_ val = (reading/1024.0) * 25;
colorVal = (reading/1024.0) * 255;_
* if (digitalRead(switchPin) == HIGH && lastButton == LOW)*
* {*
* delay(250); // Account for contact debounce*
* NeopixelColor = !NeopixelColor;*
* }*
* if (NeopixelColor == false)*
* {*
* // Neopixel LED number code*
* strip.setBrightness(40);*
* if (val != prevVal)*
* {*
* for ( x = 0; x < val; x++)*
* {*
* strip.setPixelColor(x,0,0,255);*
* }*
* for (x=val; x<25; x++)*
* {*
* strip.setPixelColor(x,0,0,0);*
* strip.show();*
* }*
* prevVal = val;*
* }*
* else*
* {*
* strip.show();*
* }*
* }*
* else*
* {*
* // Neopixel Color code*
* for (x=0; x < prevVal; x++)*
* {*
* strip.setPixelColor(x,colorVal,0,255-colorVal);*
* strip.show();*
* }*
* }*
}
Colour_sensor_with_pot.ino (3.73 KB)