Adding LDR to sketch

My friend I've been at this 12hrs now. My head is starting to hurt and I need some food. Time to step away. I will look into your new drawing in the morning

Have a great sleep !

:sleeping_bed:


Similar to this, however, Anodes are soldered as Common Anodes to V+ ?


don't know about the drawing but by word description the answer is yes.......remember this all works fine until LDR was introduced

We need to know what voltage is measured on A0 (use a voltmeter)
i.e. A0 to GND ?

  • When the LDR is in the light A0 voltmeter reads =
  • When the LDR is in the dark voltmeter reads =

dark +approx 0.5v
light =approx 2v

Get to bed.

:scream:


When you get up, show us in focus images of different views of your wiring.


Don't have the hardware here to test.

Made some very minor changes to @Delta_G sketch.

#include "Adafruit_TLC5947.h"
// How many boards do you have chained?

#define NUM_TLC5947            1
#define data                   4
#define clock                  5
#define latch                  6
#define oe                     -1  // set to -1 to not use the enable pin (its optional)
#define BRIGHT                 4095
#define PRETTY_BRIGHT          700
#define DIM                    100
#define OFF                    0
#define NUM_LEDS               20
#define Threshold_light        400
#define Threshold_dark         200

const byte LDRpin            = A0;
int LDRValue                 = 0;

Adafruit_TLC5947 tlc         = Adafruit_TLC5947(NUM_TLC5947, clock, data, latch);


//********************************************^************************************************
void setup()
{
  Serial.begin(9600);

  LDRValue = analogRead(LDRpin);
  //pinMode(LDRpin, INPUT);

  Serial.println("TLC5947 test");

  tlc.begin();

} //END of   setup()


//********************************************^************************************************
void loop()
{
  LDRValue = analogRead(LDRpin);
  Serial.println(LDRValue);

  //******************************
  if (LDRValue <= 200)
  {
    for (int i = 0; i < NUM_LEDS; i++)
    {
      tlc.setPWM(i, BRIGHT);
      tlc.setPWM((i + NUM_LEDS - 3) % NUM_LEDS, 0);
      tlc.setPWM((i + NUM_LEDS - 2) % NUM_LEDS, DIM);
      tlc.setPWM((i + NUM_LEDS - 1) % NUM_LEDS, PRETTY_BRIGHT);
      tlc.setPWM((i + NUM_LEDS + 1) % NUM_LEDS, PRETTY_BRIGHT);
      tlc.setPWM((i + NUM_LEDS + 2) % NUM_LEDS, DIM);
      tlc.setPWM((i + NUM_LEDS + 3) % NUM_LEDS, 0);

      tlc.write();

      delay(100);
    }
  }

  //******************************
  else
  {
    // this calls the stop function we created.
    stop();
  }

} //END of   loop()

//********************************************^************************************************
// This creates a new function called stop()
void stop()
{
  for (int i = 0; i < NUM_LEDS; i++)
  {
    tlc.setPWM(i, 0);
  }

  tlc.write();

} //END of   stop()


//********************************************^************************************************

That worked yeah!
Time to chill Thank you so much. Tomorrow (or the next day) I can try and get the timer part going . Once that is done I can focus on the actual build and solar charging part.
Hpefully that will go much easier .

GOODNIGHT :slight_smile:

Ask questions.

  • With TIMERs, to be tested with your hardware.
//********************************************^************************************************
//  https://forum.arduino.cc/t/adding-ldr-to-sketch/1177715
//
//
//
//  Version    YY/MM/DD    Comments
//  =======    ========    ====================================================================
//  1.00       23/10/12    Running code
//
//
//

#include "Adafruit_TLC5947.h"

#define LEDon                  HIGH
#define LEDoff                 LOW

#define NUM_TLC5947            1
#define oe                     -1  // set to -1 to not use the enable pin (its optional)

#define BRIGHT                 4095
#define PRETTY_BRIGHT          700
#define DIM                    100
#define OFF                    0

#define NUM_LEDS               20
#define Threshold_light        300
#define Threshold_dark         200

//********************************************^************************************************
//GPIOs
const byte LDRpin            = A0;

const byte data              = 4;
const byte clock             = 5;
const byte latch             = 6;

const byte testLED           = 12;
const byte heartbeatLED      = 13;

enum FLAGS : byte {ENABLED, FINISHED, DISABLED};
FLAGS fourHourFlag           = DISABLED;
FLAGS modulationFlag         = DISABLED;

int LDRValue                 = 0;
int index                    = 0;

Adafruit_TLC5947 tlc         = Adafruit_TLC5947(NUM_TLC5947, clock, data, latch);

//timing stuff
unsigned long heartbeatTime;
unsigned long modulationTime;
unsigned long fourHourTime;
unsigned long readLdrTime;

const unsigned long modulationInterval = 100ul;

//const unsigned long fourHourInterval = 4ul * 60 * 60 * 1000; //4 hours     <-----<<<<<<  4  Hours
const unsigned long fourHourInterval   = 10ul * 1000;          //10 seconds, <-----<<<<<<  10 Seconds


//                                       s e t u p ( )
//********************************************^************************************************
void setup()
{
  Serial.begin(9600);

  Serial.println("TLC5947 Test");

  pinMode(heartbeatLED, OUTPUT);
  pinMode(testLED, OUTPUT);

  tlc.begin();

} //END of   setup()


//                                       l o o p ( )
//********************************************^************************************************
void loop()
{
  //************************************************              T I M E R  heartbeatLED
  //is it time to toggle the heartbeat LED ?
  if (millis() - heartbeatTime >= 500ul)
  {
    //restart this TIMER
    heartbeatTime = millis();

    //toggle the heartbeat LED
    if (digitalRead(heartbeatLED) == LEDon) digitalWrite(heartbeatLED, LEDoff);
    else digitalWrite(heartbeatLED, LEDon);
  }

  //************************************************              T I M E R  readLdr
  //is it time to read the LDR ?
  if (millis() - readLdrTime >= 100ul)
  {
    //restart this TIMER
    readLdrTime = millis();

    readLDR();
  }

  //************************************************              T I M E R  modulation
  //if this TIMER is enabled, is it time to re-modulate the LEDs ?
  if (modulationFlag == ENABLED && millis() - modulationTime >= modulationInterval)
  {
    //Serial.print("index = ");
    //Serial.println(index);

    //restart this TIMER
    modulationTime = millis();

    tlc.setPWM(index, BRIGHT);
    tlc.setPWM((index + NUM_LEDS - 3) % NUM_LEDS, 0);
    tlc.setPWM((index + NUM_LEDS - 2) % NUM_LEDS, DIM);
    tlc.setPWM((index + NUM_LEDS - 1) % NUM_LEDS, PRETTY_BRIGHT);
    tlc.setPWM((index + NUM_LEDS + 1) % NUM_LEDS, PRETTY_BRIGHT);
    tlc.setPWM((index + NUM_LEDS + 2) % NUM_LEDS, DIM);
    tlc.setPWM((index + NUM_LEDS + 3) % NUM_LEDS, 0);
    tlc.write();

    index++;

    //do not go past the last LED
    if (index >= NUM_LEDS)
    {
      //start over
      index = 0;
    }
  }

  //************************************************              T I M E R  fourHour
  //if this TIMER is enabled, has 4 hours expired ?
  if (fourHourFlag == ENABLED && millis() - fourHourTime >= fourHourInterval)
  {
    Serial.println("4 hour TIMER ended");

    //we are finished with the 4 hour TIMER
    fourHourFlag = FINISHED;

    //timer has expired, turn OFF the LEDs
    stop();
  }

} //END of   loop()


//                                      r e a d L D R ( )
//********************************************^************************************************
void readLDR()
{
  //************************************************
  LDRValue = analogRead(LDRpin);
  Serial.println(LDRValue);

  //******************************
  //if we are not timing, has the light been reduced to the trigger point ?
  if (fourHourFlag == DISABLED && LDRValue <= Threshold_dark)
  {
    Serial.println("Threshold dark reached");

    digitalWrite(testLED, LEDon);

    //******************************
    //enable the 4 hour TIMER
    fourHourFlag = ENABLED;

    //restart the 4 hour TIMER
    fourHourTime = millis();

    Serial.println("4 hour TIMER started");

    //******************************
    //allow the LED modulation
    modulationFlag = ENABLED;

    //restart the modulation TIMER
    modulationTime = millis();

    //start at the first LED
    index = 0;
  }

  //******************************
  //if we are timing or finished timing, has light been detected by the LDR
  else if ((fourHourFlag == ENABLED || fourHourFlag == FINISHED) && LDRValue >= Threshold_light)
  {
    Serial.println("Threshold light reached");

    //get ready for the next sequence
    fourHourFlag = DISABLED;

    //light has returned, turn OFF the LEDs
    stop();
  }

} //END of   readLDR()


//                                        s t o p ( )
//********************************************^************************************************
void stop()
{
  digitalWrite(testLED, LEDoff);

  //stop LED modulation
  modulationFlag = DISABLED;

  for (int i = 0; i < NUM_LEDS; i++)
  {
    tlc.setPWM(i, 0);
  }

  tlc.write();

} //END of   stop()


//********************************************^************************************************

That's great . I'll upload it and let you know in 4 hours

OK it uploaded ... It's 6:10 my time and I have to be up pretty early so you may not hear back untill tomorrow afternoon

hmmmm It ran for about 5 seconds

For 4 hour timing these need to be modified:

From:

//const unsigned long fourHourInterval = 4ul * 60 * 60 * 1000; //4 hours     <-----<<<<<<  4  Hours
const unsigned long fourHourInterval   = 10ul * 1000;          //10 seconds, <-----<<<<<<  10 Seconds

Change to:

const unsigned long fourHourInterval = 4ul * 60 * 60 * 1000; //4 hours     <-----<<<<<<  4  Hours
//const unsigned long fourHourInterval   = 10ul * 1000;          //10 seconds, <-----<<<<<<  10 Seconds

ok it's running now ....
I changed the 4ul to 1ul I'm presuming that means 1hour now instead of 4 shorter test and easy to change back

4 * 60 * 60 * 1000 = 14,400,000 milliseconds = 4 hours.

i got it .. I just know I won't be awake in 4 hours ...lol

it will be good for me to know how to edit the timer from 4 to 1 or 6 hours .
4 hours seemed like a good general place to start but thinking about that today I also realized that now that fall is here it gets darker much earlier so 5 or 6 hours might be more appropriate.
Generalizing that 12 midnight might be a good time to stop the light show.
Where I am dark can be 5:30 in late fall and winter and 9:30 in the summer

Also I'm curious as to the reason for the LED on pin12 and is it needed?

4ul * 60 * 60 * 1000 = 4 hours

1ul * 60 * 60 * 1000 = 1 hour

6ul * 60 * 60 * 1000 = 6 hours

10ul * 1000 = 10 seconds

10ul * 60 * 1000 = 10 minutes

1 Like

I don’t have your TLC5947 hardware so I just used a LED on pin 12 to give me a visual indication.

Also, the heartbeat LED tells us if there is any programming blocking, it should toggle every 500ms.

understood. So I don't need it. I saw it in the code but I'm guessing I don't have to touch the code. Just disconnect the LED