Hi All,
Quick History:
I've built various epoxy resin items such as a stool and small table over the last few years, and for this years summer project I thought I would do the same but with some flashy LEDS embedded. After a quick trip through youtube and some craft forums I stumbled upon Arduino to control the LEDS. I have an IT back ground with some scripting and VBA programming thrown in. AKA: search on google for the answer and crowbar it in to what I want to to do :-), so I thought this would be fun.
As a way of controlling the leds I came across this example: Under this section: Interactive LED Coffee Table using the WS2812B LEDs
[https://howtomechatronics.com/tutorials/arduino/how-to-control-ws2812b-individually-addressable-leds-using-arduino/](https://How To Control WS2812B Individually Addressable LEDs using Arduino)
As this is a summer project I don't have any of the major items yet however I do have some small time to tinker with the code before the actual construction
Original code from the website:
#include "FastLED.h"
#define NUM_LEDS 45
#define LED_PIN 2
#define COLOR_ORDER GRB
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);
}
The example above just turns one (addressable) LED from Green to Blue if an item is placed/removed from the IR Proximity sensor.
I want to turn on multiple LEDS and have them fade on/off (just to white for the moment) when an item is placed/removed from the sensor. So I amended the above code.
My thoughts, and please feel free to correct, were to control the LEDS from a set of arrays rather than directly from the board input so I could have them fading out (rather then just turning off) when the proximity sensor is not longer activated.
I've created two arrays, one to hold the relative brightness of the LEDS and one to hold the time when the brightness was altered:
SENSOR_STATE[]
SENSOR_TIME[]
The relative brightness of the LEDS is set from the SENSOR_STATE array, depending on whether the sensor is on when the code will increase the value to 255 or down to 0 if the sensor is inactive.
The SENSOR_TIME array is used so that the SENSOR_STATE values are not increase/decreased to quickly. I'm am checking the last update time against the current time and only changing the value if this is greater the 0.02 of a second.
My idea was to have the first part of the loop fading out any LEDS that have been on. The main array SENSOR_STATE() by decrementing the relative brightness (255 down to 0) by 5 points every 0.02 of a second if the sensor is not active.
The second part increments the brightness by 5 points every 0.02 of second if the proximity sensor is enabled.
The loops should stop updating the leds array if the brightness count reaches 0 or 255 so those don't over run or if 0.02 seconds hasn't passed..
This code compiles on the Arduino site, which is a complete surprise!
I was wondering if some-one could take a look at the code and see if it will actually do as I would like it too and any other comments on any error conditions that might arise (I did note that millis() will over run after about 50 days) will be most welcome as this is my first foray into electronics since my degree over 25 years ago...
#include "FastLED.h"
#define NUM_LEDS 180
#define NUM_LEDS_ON 4
#define LED_PIN 2
#define COLOR_ORDER GRB
#define brightness 255
unsigned long myTime;
int SENSOR_STATE[45];
unsigned long SENSOR_TIME[45];
CRGB leds[NUM_LEDS];
void setup() {
myTime = millis();
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, set the SENSOR State to 0 (OFF) and SENSOR Time to the current time
for (int pinNo = 0 + 3; pinNo <= 45 + 3; pinNo++) {
pinMode(pinNo, INPUT);
SENSOR_STATE[pinNo] = 0;
SENSOR_TIME[pinNo] = myTime;
}
}
void loop() {
// For statement to dim the all leds under these three conditions:
// 1st condition: Sesnor state is above 5, As the sensor state is being used to control the brightness this can't go below 0
// 2nd condition: Time delay for the incremental fade, 0.02 of a second
// 3rd condition: Touch sensor not active
for (int pinNo = 0; pinNo <= (NUM_LEDS/NUM_LEDS_ON)-1; pinNo++) {
if ( (SENSOR_STATE[pinNo] >= '5') and (millis() > (SENSOR_TIME[pinNo] + 0.02)) and ( digitalRead(pinNo + 3) == HIGH ) ) {
SENSOR_STATE[pinNo] = SENSOR_STATE[pinNo] - 5; // decrement the brightness
SENSOR_TIME[pinNo] = millis(); // reset to current time
// loop to populate the required leds
for ( int led_block = 0; led_block <= (NUM_LEDS_ON - 1) ; led_block++ ){
leds[(pinNo * NUM_LEDS_ON + led_block)] = CRGB( SENSOR_STATE[pinNo], SENSOR_STATE[pinNo], SENSOR_STATE[pinNo]);
}
}
// For statement to increment all leds to a maximum brightness under these three conditions:
// 1st condition: Sesnor state is below 255, As the sensor state is being used to control the brightness this can't go above 255
// 2nd condition: Time delay for the incremental brightness increase, 0.02 of a second
// 3rd condition: Touch sensor is active
if ( (SENSOR_STATE[pinNo] <= '255') and (millis() > (SENSOR_TIME[pinNo] + 0.02)) and ( digitalRead(pinNo + 3) == LOW ) ) {
SENSOR_STATE[pinNo] = SENSOR_STATE[pinNo] + 5; // increase the brightness
SENSOR_TIME[pinNo] = millis(); // reset to current time
// loop to populate the required leds
for ( int led_block = 0; led_block <= (NUM_LEDS_ON - 1) ; led_block++ ){
leds[(pinNo * NUM_LEDS_ON + led_block)] = CRGB( SENSOR_STATE[pinNo], SENSOR_STATE[pinNo], SENSOR_STATE[pinNo]);
}
}
}
FastLED.show(); // Update the LEDs
delay(5);
}
Again, thank you for reading so far down.