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 ![]()
#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 ![]()