LDR to call up function

I would like to use an LDR to trigger a function vs a pin. Is that possible? I would like the function to run when the light is off and then quite when the light comes back on. It seems easy to do but I can't figure it out. Thanks!

More information please.

I understand how to use the LDR to turn on/off an LED per the measurement but in the examples tab of arduino I found a dynamic sound for a speaker that makes the sounds of a cricket. I understand how to make the speaker go off when the light is off/on but it just buzzes per examples of an alarm system. I like the dynamic and randomness of the function but can seem to connect the dots on how to play this function when the LDR range drops. Not sure if that helps.

Most examples I have looked up only trigger a constant high/low.

Show us the sketch that generates the cricket sound.


Is this correct ?

  • You have code that generates a cricket sound.

  • You want to enable/disable this sound using an LDR.

I used the copy to forum outlined in the guidelines. This is the original code. I know I need to manipulate the loop. I figure I drop my LDR read code here and "call up" chirpfade(). Below the chirp code I have pasted the LDR code I was working with. I was thinking I could just change out "ledPin" with the defined "speakerPin" but found it wasn't that easy. I'm just learning and thought this to be fun. I would like to "merge" the two but feel I'm miss something so simple. Thank you for your time!

#include "Volume3.h"
#define speakerPin 9

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
  chirpFade();
  delay(random(100,5000));
}

void chirpFade() {
  uint16_t f = 3900;

  uint8_t times = random(1,3);
  float master = 1.0;
  uint16_t v = 0;
  uint8_t vDir = 1;
  float vb = 0;

  while (times > 0) {
    while (vb < 1.0) {
      if (v < 1023 && vDir == 1) {
        v += 16;
      }
      else {
        vDir = 0;
      }

      if (v > 0 && vDir == 0) {
        v -= 16;
      }
      else {
        vDir = 1;
      }

      vol.tone(speakerPin, f, v * constrain(vb, 0.0, 1.0)*master);
      delayMicroseconds(50);
      vb += 0.003;
    }
    while (vb > 0.0) {
      if (v < 1023 && vDir == 1) {
        v += 16;
      }
      else {
        vDir = 0;
      }

      if (v > 0 && vDir == 0) {
        v -= 16;
      }
      else {
        vDir = 1;
      }

      vol.tone(speakerPin, f, v * constrain(vb, 0.0, 1.0)*master);
      delayMicroseconds(50);
      vb -= 0.001;
    }
    times--;
    master -= 0.75;
  }
  vol.noTone();
}

const int ledPin = 9;

const int ldrPin = A0;

void setup() {

Serial.begin(9600);

pinMode(ledPin, OUTPUT);

pinMode(ldrPin, INPUT);

}

void loop() {

int ldrStatus = analogRead(ldrPin);

if (ldrStatus <= 200) {

digitalWrite(ledPin, HIGH);

Serial.print("LED ON");

Serial.println(ldrStatus);

} else {

digitalWrite(ledPin, LOW);

Serial.print(LED OFF);

Serial.println(ldrStatus);

}

}

You didn't use words that reveal what actually happened. Of course it's easy in the text editor...

What we would like to see here, is your attempt at the integration of the two sketches, along with a detailed description of any problems with it.

Unlike the LED being ON or OFF, the 'chirp' is not something you 'turn on' or 'turn off'. It doesn't happen if you don't call the function. Decide if you want the chirps when the LED would be on:
digitalWrite(ledPin, HIGH);
or off:
digitalWrite(ledPin, LOW);

Put the chirp code there:

  chirpFade();
  delay(random(100,5000));

I can do that, I'll posted that here shortly. I have a few versions.

Thank you, I'll give that a shot!

Here is my attempted after looking at it again and the suggestions. I have defined my pins. Completed my setup. In the loop I feel I am missing a command to make the chirpFade() run, this is because I just don't know what it would be but feel it's something simple. I would think the run command but that doesn't work either. I'm confused because in the original it doesn't have anything in front of it to run, so I'm not sure why it doesn't run here. The digitalWrite doesn't work because from what I understand isn't defined up at the top as a pin. Currently it says not defined in this scope. I'm not sure the command to run/define it. I appreciate the help. Currently trying to understand the commands and can follow but looking to learn more in just writing it...

I also think turning it off will not work either per my code currently but I'm stuck here and not sure how to check the rest of the code without fixing this portion.

#include "Volume3.h"
#define speakerPin 9
const int ldrPin = A0;

void setup() {

Serial.begin(9600);

pinMode(ldrPin, INPUT);

pinMode(speakerPin, OUTPUT);

}

void loop() {

int ldrStatus = analogRead(ldrPin);

if (ldrStatus <= 200) {

  chirpFade();  // ***Not declared in scope****
  delay(random(100,5000));

Serial.print("Speaker Chirp ON");

Serial.println(ldrStatus);

} 

else {

digitalWrite(speakerPin,LOW);

Serial.print("Speaker Chirp OFF");

Serial.println(ldrStatus);

}

void chirpFade() {
  uint16_t f = 3900;

  uint8_t times = random(1,3);
  float master = 1.0;
  uint16_t v = 0;
  uint8_t vDir = 1;
  float vb = 0;

  while (times > 0) {
    while (vb < 1.0) {
      if (v < 1023 && vDir == 1) {
        v += 16;
      }
      else {
        vDir = 0;
      }

      if (v > 0 && vDir == 0) {
        v -= 16;
      }
      else {
        vDir = 1;
      }

      vol.tone(speakerPin, f, v * constrain(vb, 0.0, 1.0)*master);
      delayMicroseconds(50);
      vb += 0.003;
    }
    while (vb > 0.0) {
      if (v < 1023 && vDir == 1) {
        v += 16;
      }
      else {
        vDir = 0;
      }

      if (v > 0 && vDir == 0) {
        v -= 16;
      }
      else {
        vDir = 1;
      }

      vol.tone(speakerPin, f, v * constrain(vb, 0.0, 1.0)*master);
      delayMicroseconds(50);
      vb -= 0.001;
    }
    times--;
    master -= 0.75;
  }
  vol.noTone();
}

If it doesn't compile without errors, please copy and paste the entire error listing verbatim. Make sure the listing belongs to the exact same code you posted.

You left out a '}' in loop() which explains why chirpFade() is not defined. Fix that missing '}'.

Since a 'chirp' only happens briefly, there is no need to 'turn it off'. I think the line digitalWrite(speakerPin, LOW); is doing more harm than good and I would remove it.

Thank you, solved the problem! I dropped the else statement all together. Took me a minute to figure out the } but works! I'm still trying to wrap my head around it but awesome, just awesome. I appreciate your and everyone else's time for such a simple thing.

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