mardi 2 août 2016

Arduino IDE: Print once inside loop

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);
    }

}

Aucun commentaire:

Enregistrer un commentaire