Photo Resistor to turn on LED Strip with effects (FastLED.H) when ambient light is off

Hi, I'm very new to coding and using Arduino. I've been watching videos and searching online for various suggestions on how I can implement a code to create sort of a nightlight for my son, but instead of just a bright light turning on, I'm trying to implement FastLED.h in order to have it do some crazy effects. I'm currently programming this on an Uno R3, but once i have it sorted out, i will be uploading the code onto a Nano. Currently using the Photoresistor that came with the Elegoo Arduino Complete Kit, 10k Resistor and WS2812B LEDs I got from Amazon.

So far this is what i have for my code:

// FastLED - Version: Latest
#include <FastLED.h>

//Photo Resistor - 100% 1000 - 0% 0.

#define PhotoR A5
#define LED_PIN 3
#define NUM_LEDS 16

CRGB leds[NUM_LEDS];

void setup() {
pinMode(PhotoR, INPUT);
pinMode(LED_PIN, OUTPUT);
FastLED.addLeds<WS2812B, LED_PIN, RGB>(leds, NUM_LEDS);
FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
//FastLED.clear();
//FastLED.show();
Serial.begin(9600);

}

void loop() {
Serial.println(analogRead(PhotoR));

if (analogRead(PhotoR)>191){
  FastLED.setBrightness(0);
  FastLED.clear();
}

else {//(analogRead(PhotoR)<190);{
  //digitalWrite(LED_PIN,HIGH);
  //turn lights from green to blue from left to right
  for (int i=0; i<NUM_LEDS; i++){
    leds[i] = CRGB(0, 255 - 20*i, 20*i);
    FastLED.setBrightness(6*i);
    FastLED.show();
    delay(50);
  }

  //turn lights from blue to magenta from right to left
  for (int i=NUM_LEDS; i>0; i--){
    leds[i] = CRGB(20*i,0 , 255-20*i);
    FastLED.setBrightness(60-2*i);
     FastLED.show();
    delay(50);
  }
}

and this is how it's currently laid out.

My issue is i cannot figure out how to get the LED strip to turn off when the Photo Resistor senses light above my threshold of 190. I found my thresholds where i would like the lights to turn on and off. If i cover the sensor, the LED Strip sweeps back and forth as it should, but as soon as light hits the sensor again, my strip remains at the original state colour (Green to Blue, L to R).

I'm typically a quick learner so any suggestions for my code, or layout, would be greatly appreciated. TIA!

Turn the strip off by setting all pixels to black

if (analogRead(PhotoR)>191)
{
    for (int i= 0; i<NUM_LEDS;i++)
   {
       leds[i] = CRGB::Black;
    }
    FastLED.show();
}

Actually i literally just figured it out 2 mins ago. I moved the 5v power from the LED strip to 6, programmed it as as OUTPUT, then added the digitalWrite codes for IF and ELSE, and it works. I'm in shock right now, my first project!

Thank you for your suggestion though, i definitely appreciate it. This is the final code that works:

// FastLED - Version: Latest
#include <FastLED.h>

//Photo Resistor - 100% 1000 - 0% 0.

#define PhotoR A5
#define LED_PIN 3
#define LED_POWER 6
#define NUM_LEDS 16

CRGB leds[NUM_LEDS];

void setup() {
pinMode(PhotoR, INPUT);
pinMode(LED_POWER, OUTPUT);
pinMode(LED_PIN, OUTPUT);
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
//FastLED.clear();
//FastLED.show();
Serial.begin(9600);

}

void loop() {
Serial.println(analogRead(PhotoR));

if (analogRead(PhotoR)>191){
  digitalWrite(LED_POWER,LOW);
  FastLED.setBrightness(0);
  FastLED.clear();
}

else {//(analogRead(PhotoR)<190);{
  digitalWrite(LED_POWER,HIGH);
  //turn lights from green to blue from left to right
  for (int i=0; i<NUM_LEDS; i++){
    leds[i] = CRGB(0, 255 - 20*i, 20*i);
    FastLED.setBrightness(6*i);
    FastLED.show();
    delay(50);
  }

  //turn lights from blue to magenta from right to left
  for (int i=NUM_LEDS; i>0; i--){
    leds[i] = CRGB(20*i,0 , 255-20*i);
    FastLED.setBrightness(60-2*i);
     FastLED.show();
    delay(50);
  }
}

}

No.

Are you making the neopixels light relative to the LDR?

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