Trying to set a LED-Strip to the colors given by a sensor

So for my setup:

I'm using an Arduino Uno, a WS2812 LED-Strip with 144 LEDs per metre and a GY-31 TCS3200 colorsensor. The RFID Reader is not relevant for this part of my project, so it's just mentioned to give a full view of the used components.

My problem is, that I can't get the LED strips to shine in the color that is read by the color sensor. I am fairly new to the whole programming stuff and not sure about what to do.

The function in question is titled "Trinket".

The troubleshooting focused on the values that are given.

First of all - the values in row 73 isn't working. I thought I could implement the variables to transfer into the function with the values behind the variables, but that isn't working.
Second - before the code in 204 I used the serial print to give out the values that are set for redFrequency, greenFrequeny and blueFrequency. The values are printed, but they wont be used in the "strip.setPixelColor".

I tried out some code from other project (like "LED Chamaleon"), but the base code itself wasn't working.

Hope your can help me with this one. Thanks!

#include <FastLED_NeoPixel.h>
#include <stdint.h>
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
#define NUM_LEDS 50
CRGB leds[NUM_LEDS];
#define PIN 3

#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define sensorOut 8

// Stores frequency read by the photodiodes
byte redFrequency = 0;
byte greenFrequency = 0;
byte blueFrequency = 0;

MFRC522 mfrc522(SS_PIN, RST_PIN);

const int potPin = A0;

int potValue = 0;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(50, PIN, NEO_GRB + NEO_KHZ800);

void setup()
{
  FastLED.addLeds<WS2812, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  strip.begin();
  strip.setBrightness(85);  // Lower brightness and save eyeballs!
  strip.show(); // Initialize all pixels to 'off'
  pinMode(potPin, INPUT);
  potValue = analogRead(potPin) * 4;
  Serial.begin(9600);
  SPI.begin();        // Init SPI bus
  mfrc522.PCD_Init(); // Init MFRC522
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);

  // Setting the sensorOut as an input
  pinMode(sensorOut, INPUT);

  // Setting frequency scaling to 20%
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);
}

void loop() {
  if (mfrc522.PICC_IsNewCardPresent()) {
    strip.show();
    strip.setBrightness(85);
    Fire(70, 120, 15);
  }
  else if (analogRead(potPin) > 50) {
    strip.show();
    digitalWrite(S2, LOW);
    digitalWrite(S3, LOW);
    redFrequency = pulseIn(sensorOut, LOW);
    // Reading the output frequency
    digitalWrite(S2, HIGH);
    digitalWrite(S3, HIGH);
    greenFrequency = pulseIn(sensorOut, LOW);
    // Reading the output frequency
    digitalWrite(S2, LOW);
    digitalWrite(S3, HIGH);
    blueFrequency = pulseIn(sensorOut, LOW);
    Trinket(redFrequency,greenFrequency,blueFrequency);
  }
  else {
    strip.show();
    Fire(0, 0, 0);
  }
}

// START OF FIRE-CODE


//https://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/#LEDStripEffectFire
//Modified by: Oliver Weber
void Fire(int Cooling, int Sparking, int SpeedDelay) {
  static byte heat[NUM_LEDS];
  int cooldown;

  // Step 1.  Cool down every cell a little
  for ( int i = 0; i < NUM_LEDS; i++) {
    cooldown = random(0, ((Cooling * 10) / NUM_LEDS) + 2);

    if (cooldown > heat[i]) {
      heat[i] = 0;
    }
    else {
      heat[i] = heat[i] - cooldown;
    }
  }

  // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  for ( int k = NUM_LEDS - 1; k >= 2; k--) {
    heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
  }

  // Step 3.  Randomly ignite new 'sparks' near the bottom
  if ( random(255) < Sparking ) {
    int y = random(7);
    heat[y] = heat[y] + random(160, 255);
    //heat[y] = random(160,255);
  }

  // Step 4.  Convert heat to LED colors
  for ( int j = 0; j < NUM_LEDS; j++) {
    setPixelHeatColor(j, heat[j] );
  }

  showStrip();
  delay(SpeedDelay);
}

void setPixelHeatColor (int Pixel, byte temperature) {
  // Scale 'heat' down from 0-255 to 0-191
  byte t192 = round((temperature / 255.0) * 191);

  // calculate ramp up from
  byte heatramp = t192 & 0x3F; // 0..63
  heatramp <<= 2; // scale up to 0..252

  // figure out which third of the spectrum we're in:
  if ( t192 > 0x80) {                    // hottest
    setPixel(Pixel, heatramp - 200, heatramp - 200, heatramp - 200);
  } else if ( t192 > 0x40 ) {            // middle
    setPixel(Pixel, heatramp - 200, heatramp - 200, heatramp - 200);
  } else {                               // coolest
    setPixel(Pixel, heatramp, heatramp, heatramp);
  }
}

void showStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
  // NeoPixel
  strip.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
  // FastLED
  FastLED.show();
#endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
#ifdef ADAFRUIT_NEOPIXEL_H
  // NeoPixel
  strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
  // FastLED
  leds[Pixel].r = red;
  leds[Pixel].g = green;
  leds[Pixel].b = blue;
#endif
}

void setAll(byte red, byte green, byte blue) {
  for (int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue);
  }
  showStrip();
}


// START OF TRINKET CODE

void Trinket(byte redFrequency, byte greenFrequency, byte blueFrequency) {
  //Written by: Jason Yandell
  //Modified by: Oliver Weber
  potValue = analogRead(potPin);
  int TOTAL_LEDS = 100;
  float MaximumBrightness = map(potValue, 0, 1023, 0, 254);
  float SpeedFactor = 0.01; // I don't actually know what would look good
  float StepDelay = 1; // ms for a step delay on the lights

  // Make the lights breathe
  for (int x = 0; x < 32766; x++) {
    // Serial.println(x);
    potValue = analogRead(potPin);
    // Reading the output frequency


    int TOTAL_LEDS = 100;
    float MaximumBrightness = map(potValue, 0, 1023, 0, 254);
    // Intensity will go from 10 - MaximumBrightness in a "breathing" manner
    float intensity = MaximumBrightness / 2.0 * (1.0 + sin(SpeedFactor * x));
    strip.setBrightness(intensity);
    if (potValue < 50) {
      x = 0;
      strip.show();
      break;
    }

    // Now set every LED to that color
    for (int ledNumber = 0; ledNumber < TOTAL_LEDS; ledNumber++) {
        strip.setPixelColor(ledNumber,redFrequency,greenFrequency,blueFrequency);{
        }
    }

    strip.show();
    //Wait a bit before continuing to breathe
    delay(StepDelay);

  }
}

Break your code up into minimal sized bits in new sketches with appropriate names. Get 1 sensor working on its own and store the input in a variable. Consider encapsulating code into a function for ease of merging together. Then work on the output code for the leds. If you are able to control only the LEDs in code then you should have 2 bits of code that will work together. 1 function that will take the reading from the sensor and create a value and another function that will take a value and output to the LEDs

Each step should be the minimum size and as simple as you can get. The end result will only appear complex. Do it all together and it is complex.

The 'pulseIn()' function returns an 'unsigned long' value in microseconds. You are throwing away three of the four bytes of the value by cramming the unsigned long into a byte.

It is highly unlikely that the bottom byte of the unsigned long will map directly to the color intensity. You should change the variables to unsigned long and print them out so you can get an idea of the range of values you can expect. Then you can map the range to the 0-255 range of a byte.

The mapping seems to do the trick! I just need to adjust the values a bit. I'll let you know asap if it works in the end.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.