Sensors and Addressable RGB's

So I'm trying to control 6 addressable RGBs with 6 infrared sensors. I'd also like the LED to "fade" off, rather than just turn off. I've been able to successfully program both of these independently, but can't seem to combine them. I can get one LED with one sensor to turn on when the sensor sees something, it stays on until the sensor is released, and then the LED fades off. But when trying to do this with multiple sensors, only one will work at a time. Each sensor has its own LED and I want them to turn on/off regardless of the status of any other sensor.

Keep in mind I'm on week two of learning this so I'm sure there is parts of these codes that could be compressed.

Working fade:

#include <FastLED.h>

#define LED_Pin 2
#define NUM_LEDS 1
#define OFF 0,0,0
#define ON 0,150 ,0
int IRSOne = 8;



CRGB leds[NUM_LEDS];



void setup() {

 pinMode (IRSOne, INPUT);
 FastLED.addLeds<WS2812B, LED_Pin, GRB>(leds,NUM_LEDS);
 Serial.begin(9600);
 
}


void loop() 
{
  {
  //First Sensor 

  int StatusSensor = digitalRead (IRSOne);
  if (StatusSensor == 0)
  {    
    while (StatusSensor == 0) {
      leds[0] = CRGB (ON);
      FastLED.show();
      delay(1000);
      StatusSensor = digitalRead (IRSOne);
      delay(200);
  }
  leds[0] = CRGB (0,140,0);
  FastLED.show();
  delay(75);
  leds[0] = CRGB (0,130,0);
  FastLED.show();
  delay(75);
  leds[0] = CRGB (0,120,0);
  FastLED.show();
  delay(75);
  leds[0] = CRGB (0,110,0);
  FastLED.show();
  delay(75);
  leds[0] = CRGB (0,100,0);
  FastLED.show();
  delay(75);
  leds[0] = CRGB (0,80,0);
  FastLED.show();
  delay(75);
  leds[0] = CRGB (0,70,0);
  FastLED.show();
  delay(75);
  leds[0] = CRGB (0,50,0);
  FastLED.show();
  delay(75);
  leds[0] = CRGB (0,25,0);
  FastLED.show();
  delay(75);
  leds[0] = CRGB (0,0,0);
  FastLED.show();
  delay(75);
  }
  }



}

The above code works well with one sensor and one led, fading the LED instead of just shutting off.

Below works well with multiple sensors and LEDs, but they don't fade :frowning:

#include <FastLED.h>

#define LED_Pin 2
#define NUM_LEDS 6
#define OFF 0,0,0
#define ON 0,155 ,155
int IRSOne = 8;
int IRSTwo = 9;
int IRSThree = 10;
int IRSFour = 11;
int IRSFive = 12;
int IRSSix = 13;

CRGB leds[NUM_LEDS];



void setup() {

 pinMode (IRSOne, INPUT);
 pinMode (IRSTwo, INPUT);
 pinMode (IRSThree, INPUT);
 pinMode (IRSFour, INPUT);
 pinMode (IRSFive, INPUT);
 pinMode (IRSSix, INPUT);
 FastLED.addLeds<WS2812B, LED_Pin, GRB>(leds,NUM_LEDS);
 Serial.begin(9600);
 
}


void loop() 
{
  //First Sensor 
{
  int StatusSensor = digitalRead (IRSOne);
  if (StatusSensor == 0){         
  leds[0] = CRGB (ON);
  FastLED.show();
  delay(50);
  }
  else {
  leds[0] = CRGB (OFF);
  FastLED.show();
  delay(50);
  }
}
  //Second Sensor
{
  int StatusSensor = digitalRead (IRSTwo);
  if (StatusSensor == 0){
  leds[1] = CRGB (ON);
  FastLED.show();
  delay(50);
  }
  else {
  leds[1] = CRGB (OFF);
  FastLED.show();
  delay(50);
  }
}
    //Third Sensor
{
  int StatusSensor = digitalRead (IRSThree);
  if (StatusSensor == 0){
  leds[2] = CRGB (ON);
  FastLED.show();
  delay(50);
  }
  else {
  leds[2] = CRGB (OFF);
  FastLED.show();
  delay(50);
  }
}
    //Fourth Sensor
    {
  int StatusSensor = digitalRead (IRSFour);
  if (StatusSensor == 0){
  leds[3] = CRGB (ON);
  FastLED.show();
  delay(50);
  }
  else {
  leds[3] = CRGB (OFF);
  FastLED.show();
  delay(50);
  }
}
    //Fifth Sensor
    {
  int StatusSensor = digitalRead (IRSFive);
  if (StatusSensor == 0){
  leds[4] = CRGB (ON);
  FastLED.show();
  delay(50);
  }
  else {
  leds[4] = CRGB (OFF);
  FastLED.show();
  delay(50);
  }
}
    //Sixth Sensor
    {
  int StatusSensor = digitalRead (IRSSix);
  if (StatusSensor == 0){
  leds[5] = CRGB (ON);
  FastLED.show();
  delay(50);
  }
  else {
  leds[5] = CRGB (OFF);
  FastLED.show();
  delay(50);
  }
}
}

And finally, this was my attempt to combine the two, but to no success.

#include <FastLED.h>

#define LED_Pin 2
#define NUM_LEDS 2
#define OFF 0,0,0
#define ON 0,150 ,0
int IRSOne = 8;
int IRSTwo = 9;
int IRSThree = 10;
int IRSFour = 11;
int IRSFive = 12;
int IRSSix = 13;


CRGB leds[NUM_LEDS];



void setup() {

 pinMode (IRSOne, INPUT);
 pinMode (IRSTwo, INPUT);
 pinMode (IRSThree, INPUT);
 pinMode (IRSFour, INPUT);
 pinMode (IRSFive, INPUT);
 pinMode (IRSSix, INPUT);
 FastLED.addLeds<WS2812B, LED_Pin, GRB>(leds,NUM_LEDS);
 Serial.begin(9600);
 
}


void loop() 
{
  {
  //First Sensor 

  int StatusSensor = digitalRead (IRSOne);
  if (StatusSensor == 0)
  {    
    while (StatusSensor == 0) {
      leds[0] = CRGB (ON);
      FastLED.show();
      delay(1000);
      StatusSensor = digitalRead (IRSOne);
      delay(200);
  }
  leds[0] = CRGB (0,140,0);
  FastLED.show();
  delay(75);
  leds[0] = CRGB (0,130,0);
  FastLED.show();
  delay(75);
  leds[0] = CRGB (0,120,0);
  FastLED.show();
  delay(75);
  leds[0] = CRGB (0,110,0);
  FastLED.show();
  delay(75);
  leds[0] = CRGB (0,100,0);
  FastLED.show();
  delay(75);
  leds[0] = CRGB (0,80,0);
  FastLED.show();
  delay(75);
  leds[0] = CRGB (0,70,0);
  FastLED.show();
  delay(75);
  leds[0] = CRGB (0,50,0);
  FastLED.show();
  delay(75);
  leds[0] = CRGB (0,25,0);
  FastLED.show();
  delay(75);
  leds[0] = CRGB (0,0,0);
  FastLED.show();
  delay(75);
  }
  }
   {
  //Second Sensor 

  int StatusSensor = digitalRead (IRSTwo);
  if (StatusSensor == 0)
  {    
    while (StatusSensor == 0) {
      leds[1] = CRGB (ON);
      FastLED.show();
      delay(1000);
      StatusSensor = digitalRead (IRSTwo);
      delay(200);
  }
  leds[1] = CRGB (0,140,0);
  FastLED.show();
  delay(75);
  leds[1] = CRGB (0,130,0);
  FastLED.show();
  delay(75);
  leds[1] = CRGB (0,120,0);
  FastLED.show();
  delay(75);
  leds[1] = CRGB (0,110,0);
  FastLED.show();
  delay(75);
  leds[1] = CRGB (0,100,0);
  FastLED.show();
  delay(75);
  leds[1] = CRGB (0,80,0);
  FastLED.show();
  delay(75);
  leds[1] = CRGB (0,70,0);
  FastLED.show();
  delay(75);
  leds[1] = CRGB (0,50,0);
  FastLED.show();
  delay(75);
  leds[1] = CRGB (0,25,0);
  FastLED.show();
  delay(75);
  leds[1] = CRGB (0,0,0);
  FastLED.show();
  delay(75);
  }
  }



}

Thanks for any helppp :smiley:

    while (StatusSensor == 0) {
      leds[0] = CRGB (ON);
      FastLED.show();
      delay(1000);
      StatusSensor = digitalRead (IRSOne);
      delay(200);
  }

This is the main cause of your problem because the code is stuck in the while loop all the time that the button is pressed. Start by changing the while to an if although that is not the only problem in the code

Right. That makes sense. Changing to if yielded the same results. Is there a more efficient way of fading the LED? The middle code using if/else works well, I'm just not sure of the proper way to get it to "fade" off instead of just turning off.

You are not going to like this but you need to eliminate all of the delay()s from your sketch. While a delay() is occurring nothing else can happen and having several of them in sequence exacerbates the problem

So, what to do ?
Well, the standard way of doing it would be to use the millis() function for timing. millis() returns the number of millseconds since the processor was powered up or reset.

The principle is simple. Save the value of millis() when an action, such as setting the level of an LED happens, then go round loop() checking each time whether the current value of millis() minus the start value equals or is greater than the required period. If not, then keep going round loop() until the period ends. When it does, save the current time as the new start time, change the state of the LED and off you go again

The advantage of this method is that loop() runs freely so you can do other things such as reading inputs and fading one or more other LEDs

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE for examples and ideas

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