Shop OBEX P1 Docs P2 Docs Learn Events
LDR with the WS2812 B LED's. Question as to how to use with the dreaded Arduino(I know, wrong place) — Parallax Forums

LDR with the WS2812 B LED's. Question as to how to use with the dreaded Arduino(I know, wrong place)

I know this is the Parallax forum but trying to get an answer from the Arduino forum has been futile at best. I posted a question 2 weeks ago and still no answer. So, I thought I would ask the brainiacs here if you can get me started. I am trying to get an LDR (Light Dependent Resistor) to work with the WS2812B LED's. I have them working independently of each other but I can not figure out how to get them to work together. Basically I am just trying to use an "If" statement. if (sensorReading<100)) then start the WS2812 program. If anyone can give me a hint as to how to get this started I would be ever so grateful. It does not need to be anything fancy, all I need is a starting point. Thanks in advance for the help.

Comments

  • So that's where you've been. We could have you up and running on the Propeller in minutes.

    I think it should look like this.

    if (sensorReading < 100)
    {
    // WS code
    }
  • NWCCTVNWCCTV Posts: 3,629
    edited 2015-09-19 02:43
    No, I have actually been doing a LOT of work in my yard and house. (I am only using an Arduino because I got it for $1.50 and it fits perfectly for the project I am working on. Prop was just way over kill for just lighting a 8 LED's in a container with clear rocks. It's something I am making out of an old Hummingbird feeder for my wife to hang in her garden.)
  • xanadu wrote: »

    if (sensorReading < 100)
    {
    // WS code
    }

    I had that part figured out. It was the WS code I had issues with but I resolved it. Pretty basic stuff really but I have been away for a while!!!

  • Mike GreenMike Green Posts: 23,101
    edited 2015-09-20 04:27
    First you have to read the LDR value. Usually you make a voltage divider using a fixed resistor. I would use an Ohmmeter to find the LDR resistance with a light level roughly the same as what you want for your light threshold and use that value for your fixed resistor. If your Arduino runs at 5V, use that for the voltage divider. If the LDR is on the high side of the voltage divider, the voltage will be 1/2 Vcc at the threshold, below that for less light and above that for more light. Follow the Arduino examples for using the built-in ADC
  • The LDR circuit will be something like this:
    VCC
     |
     >
     <     LDR / photoresistor
     >
     |
     +---  to ADC
     |
     >
     <     resistor (10K, possibly)
     >
     |
    GND
    
  • An alternate might be:
    VCC -+
         |
         <
         > --- LDR
         <
         |
         +-<
    pot    > --- ADC
         +-<
         |
    GND -+
    
    So you can trim the light level.
  • This is what I use for the ping sensor same idea.
    What are u using for the ws2812 control. fastled or the adafruit library?


    #include <FastLED.h>

    const int pingPin = 7; // ping in

    #define NUM_LEDS 6 // How many leds in your strip?
    #define DATA_PIN 2 // What pin is the NeoPixel's data line connected to?
    CRGB leds[NUM_LEDS]; // Define the array of leds


    void setup() {
    // initialize serial communication:
    Serial.begin(9600);
    FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
    }

    void loop()
    {
    // establish variables for duration of the ping,
    // and the distance result in inches and centimeters:
    long duration, inches, cm;

    // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
    // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
    pinMode(pingPin, OUTPUT);
    digitalWrite(pingPin, LOW);
    delayMicroseconds(2);
    digitalWrite(pingPin, HIGH);
    delayMicroseconds(5);
    digitalWrite(pingPin, LOW);

    // The same pin is used to read the signal from the PING))): a HIGH
    // pulse whose duration is the time (in microseconds) from the sending
    // of the ping to the reception of its echo off of an object.
    pinMode(pingPin, INPUT);
    duration = pulseIn(pingPin, HIGH);

    // convert the time into a distance
    inches = microsecondsToInches(duration);
    cm = microsecondsToCentimeters(duration);

    Serial.print(inches);
    Serial.print("in, ");
    Serial.print(cm);
    Serial.print("cm");
    Serial.println();

    if (inches <= 20) {fill_solid( &(leds[0]), NUM_LEDS /*number of leds*/, CRGB::Red); //{whitestrobe(30);
    FastLED.show();

    }
    else if (inches >= 21) {fill_solid( &(leds[0]), NUM_LEDS /*number of leds*/, CRGB::Green);
    FastLED.show();
    }
    delay(100);
    }

    long microsecondsToInches(long microseconds)
    {
    return microseconds / 74 / 2;
    }

    long microsecondsToCentimeters(long microseconds)
    {
    return microseconds / 29 / 2;
    }
Sign In or Register to comment.