0

I am using a LDR to tell me whether there is light or not with an Arduino. My code is pretty simple, but instead of spamming "light light light light" I would like it to say "light" once, and then if the light goes off, for it to say "No light" once. code edited from "readanalogvoltage":

void setup() {
    // initialize serial communication at 9600 bits per second:
    Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
    // read the input on analog pin 0:
    int sensorValue = analogRead(A0);

    // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
    float voltage = sensorValue * (5.0 / 1023.0);

    // print out the value you read:
    if (voltage > 1.5)
    {
        Serial.print("Light");
        Serial.println();
        delay(5000);
    }

    if (voltage < 1.5)
    {
        Serial.print("No Light");
        Serial.println();
        delay(50);
    }

}
0

2 Answers 2

4

Keep a variable that holds the last state:

void setup() {
    // initialize serial communication at 9600 bits per second:
    Serial.begin(9600);
}

int wasLit = 0;

// the loop routine runs over and over again forever:
void loop() {
    // read the input on analog pin 0:
    int sensorValue = analogRead(A0);

    // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
    float voltage = sensorValue * (5.0 / 1023.0);

    // print out the value you read:
    if (!wasLit && voltage > 1.5) {
        wasLit = 1;
        Serial.print("Light");
        Serial.println();
        delay(5000);
    } else if(wasLit && voltage <= 1.5) {
        wasLit = 0;
        Serial.print("No Light");
        Serial.println();
        delay(50);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Okay so using the code above, it keeps spamming "Light" "No light" constantly (of course, obeying the delays) when the light condition is supposed to be met. If I hold my finger over it, however, it stops spamming "Light" "No light" constantly, and just says "No Light" like its supposed to.
0

This sort of test will benefit from use of hysterisis. Especially if you have flourescent light, there will be some flicker. That will cause the sensor reading to vary such that it may not change from <1.5 to >=1.5 in a clean manner.

boolean bLast = false;

const float vTrip = 1.5;
// you find a good value for this by looking at the noise in readings
// use a scratch program to just read in a loop  
// set this value to something much larger than any variation
const float vHyst = 0.1;

float getLight() {
    int sensorValue = analogRead(A0);
   // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
   float voltage = sensorValue * (5.0 / 1023.0);
   return voltage;
}

void setup() {
    // establish the initial state of the light
    float v = getLight();
    bLast = ( v < (vTrip-vHyst) );
}

void loop() {
    float v = getLight();
    if( bLast ) {
        // light was on
        // when looking for decreasing light, test the low limit
        if( v < (vTrip-vHyst) ) {
             bLast = false;
             Serial.print("Dark");
        }
    }
    else {
        // light was off
        // when looking for increasing light, test the high limit
        if( v > (vTrip+vHyst) ) {
            bLast = true;
            Serial.print("Light");
        }
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.