Below is a code created by howtomechatronics for a led interactive coffee table project. It consists of 45 leds that each is controlled by an individual proximity sensor. Basically 1:1 (led to sensor). My question is how to adapt the original code to control multiple leds to one sensor. Say 3:1 (leds to sensor). I am using an arduino mega2560. Any help would be really appreciated. thanks.
#include "FastLED.h"
#define NUM_LEDS 45
#define LED_PIN 2
#define COLOR_ORDER GRB
#define brightness 128
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<WS2812, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(brightness);
// Set the 45 proximity sensors pins as inputs, from digital pin 3 to pin 48
for (int pinNo = 0 + 3; pinNo <= 45 + 3; pinNo++) {
pinMode(pinNo, INPUT);
}
}
void loop() {
for (int pinNo = 0; pinNo <= NUM_LEDS-1; pinNo++) {
leds[pinNo] = CRGB( 0, 255, 0); // Set all 45 LEDs to green color
// If an object is detected on top of the particular sensor, turn on the particular led
if ( digitalRead(pinNo + 3) == LOW ) {
leds[pinNo] = CRGB( 0, 0, 255); // Set the reactive LED to bluee
}
}
FastLED.show(); // Update the LEDs
delay(20);
}
if ( digitalRead(pinNo + 3) == LOW ) {
leds[pinNo + 0] = CRGB( 0, 0, 255); // Set the reactive LED to bluee
leds[pinNo + 1] = CRGB( 0, 0, 255); // Set the reactive LED to bluee
leds[pinNo + 2] = CRGB( 0, 0, 255); // Set the reactive LED to bluee
}
Well unfortunately after amending the code based on the previous feedback it is still only working in a 1:1 LED to sensor. Each sensor is plugged in to a pin on the arduino starting with #3 and up from there. The LED string is only plugged into pin 2. The way the code works each sensor operates a single addressable led.
I am trying to get the first sensor to operate the first 3leds and the second sensor to operate the following 3 leds and so on.
Here is the amended code in red font.
#include "FastLED.h"
#define NUM_LEDS 45
#define LED_PIN 2
#define COLOR_ORDER GRB
#define brightness 128
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<WS2812, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(brightness);
// Set the 45 proximity sensors pins as inputs, from digital pin 3 to pin 48
for (int pinNo = 0 + 3; pinNo <= 45 + 3; pinNo++) {
pinMode(pinNo, INPUT);
}
}
void loop() {
for (int pinNo = 0; pinNo <= NUM_LEDS-1; pinNo++) {
leds[pinNo] = CRGB( 0, 255, 0); // Set all 45 LEDs to green color
// If an object is detected on top of the particular sensor, turn on the particular led
if ( digitalRead(pinNo + 3) == LOW ) {
[color=#ff0000] leds[pinNo + 0] = CRGB( 0, 0, 255); // Set the reactive LED to bluee[/color]
[color=#ff0000] leds[pinNo + 1] = CRGB( 0, 0, 255); // Set the reactive LED to bluee[/color]
[color=#ff0000] leds[pinNo + 2] = CRGB( 0, 0, 255); // Set the reactive LED to bluee[/color]
}
}
FastLED.show(); // Update the LEDs
delay(20);
}
Have you tried working with the Adafruit Neopixel library? You may find it to be more simple to target a section of led's for activity. FastLED is a great library but having worked with both for some time, I have found there is a good fit for each library depending on the project.
Here's where you would target the starting pixel in the Adafruit library code: simply change the "i" value to your starting led and it will do just that. Keep in mind neopixels start from zero not one.
// For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
for(int i=0;i<NUMPIXELS;i++){
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(0,150,0)); // Moderately bright green color.
pixels.show(); // This sends the updated pixel color to the hardware.