Do something based on ON/OFF status of LEDs

Hi,

I have 2 questions related to each other, well at least in my case :slight_smile: -

  1. How can I read the status of an LED to check of it is ON or OFF, or may be Black or White?

  2. How can I put 2 or 3 conditions using if else if to read the status of LEDs and execute the code accordingly?
    For example -

if LED 1 is ON and LED 10 is ON, do something
if LED 1 is ON and LED 10 is OFF, do something else
if LED 1 is OFF and LED 10 is OFF, do nothing

I am trying the code below but no luck.

#include <FastLED.h>

#define NUM_LEDS 60 

#define DATA_PIN 12

CRGB leds[NUM_LEDS];

const CRGB Test_Color = CRGB::White;

Void setup() {

  FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
  FastLED.clear();
  FastLED.show();

void buttonThree() {

  Serial.println("Button 3 has been pressed");    

   if (leds[10] == Test_Color && leds[30] == Test_Color){

  for (int k = 40, j = NUM_LEDS-40; k < NUM_LEDS- 40, j > 0 ; j--, k++){    // Lights_ON from 2/3
    
   for(int whiteLed = 0; whiteLed < NUM_LEDS-k; whiteLed = whiteLed + 1) {   // Move a single led
    
   leds[whiteLed] = CRGB::White;                                             // Turn our current led on to white, then show the leds
    
   FastLED.show();                                                           // Show the leds (only one of which is set to white, from above)
 
   delay(10);                                                                // Wait a little bit

   if ( whiteLed != j-1){                                                    // Turn our current led back to black for the next loop around
      
   leds[whiteLed] = CRGB::Black;
 
    }}}}

  else if {
  do stuff 
  }
}

Hardware - Freetronics EtherTen with WS2812

Thanks,

“ How can I read the status of an LED to check of it is ON or OFF, or may be Black or White? ”

If you make a LED turn ON then don’t you know it’s ON ?

Oh wow! why didn't I think about that. Thanks Brattain Member for your reply to a Newbie.

The problem is that I know when it is ON but Arduino can't see unfortunately, at least mine can't. :slight_smile:
So like I explained in my original post, I want to get the ON/OFF status of LEDs after last code execution and based on that, decide which set of code to run when a button is pressed. If you need more information, here you go -

4 button for 4 light presets which are -

  1. 1/3 of total LEDs ON
  2. 2/3 of total LEDs ON
  3. All LEDs ON
  4. All LEDs OFF

To explain further for better understanding, there are different set of codes for going to preset 1 from preset 2,3 and 4

“ I want to get the ON/OFF status of LEDs after last code execution”

I think you are missing the point.

If the code turns ON a LED then the code knows it has turned on the LED.

When your code turns ON or OFF an output you can just as easily set a variable to 1 or 0.

All you have to do is examine that variable (as needed) to see if it is 1 (LED ON) or 0 (LED OFF).


The same is true for LEDs on a LED strip.

These LEDs have an associated variable array element number.

Let’s say a Neo-Pixel array element (example leds[20]) has the value 0xFFFFFF.

This element can be read by your code; when it reads the RGB value, it finds red is FF green is FF and blue is FF.

If array element was 0x000000 this would mean the red green and blue LEDs are turned off.


BTW

Thank you for your sarcastic comments.

The same is true for LEDs on a LED strip.

These LEDs have an associated variable array element number.

Let's say a Neo-Pixel array element (example leds[20]) has the value 0xFFFFFF.

This element can be read by your code; when it reads the RGB value, it finds red is FF green is FF and blue is FF.

If array element was 0x000000 this would mean the red green and blue LEDs are turned off.

I am trying to do something like this in my code. I defined White as a test color and check for true/false values using the code below. But can't figure out why it is not working.

 if (leds[10] == Test_Color && leds[30] == Test_Color){
code goes here
}

My question is, what am I doing wrong here.

BTW

Thank you for your sarcastic comments.

It realized it was totally uncalled for. My sincere apologies.

What does this sketch do ?

#include<FastLED.h>

#define NUM_LEDS 8

CRGBArray<NUM_LEDS> leds;

const CRGB Test_Color1 = CRGB::White;
const CRGB Test_Color2 = CRGB::Red;
const CRGB Test_Color3 = CRGB::Blue;
const CRGB Test_Color4 = CRGB::Green;
const CRGB Test_Color5 = CRGB::Yellow;
const CRGB Test_Color6 = CRGB::Violet;
const CRGB Test_Color7 = CRGB::Black;
const CRGB Test_Color8 = CRGB::Magenta;

void setup()
{
  FastLED.addLeds<NEOPIXEL, 6>(leds, NUM_LEDS);

} //END of setup()

void loop()
{
  leds[0] = Test_Color1;
  leds[1] = Test_Color2;
  leds[2] = Test_Color3;
  leds[3] = Test_Color4;
  leds[4] = Test_Color5;
  leds[5] = Test_Color6;
  leds[6] = Test_Color7;
  leds[7] = Test_Color8;

  FastLED.show();
  delay(1000);

  if (leds[6] == Test_Color7 && leds[0] == Test_Color1)
  {
    for (byte x = 0; x < NUM_LEDS; x++)
    {
      leds[x] = 0x101010;
    }

    FastLED.show();
  }

  delay(1000);

} //END of loop()

Note:

Pin 6 goes to the Data In pin on your strip.

What kind of LED strip do you have ?

Hardware - . . . with WS2812

The code from post #5 should work then.

Thanks larryd. It worked finally.

Code was correct, I wasn't assigning the LED number right. :confused:

Thanks so much.

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