absolute newb question (run once when condition met)

Hey all, I know this has probably been answered in once way or another but I can't seem to find the answer I am after (or i'm incapable of my puny brain comprehending it) - Bare in mind my experience with coding is little to none.

I have a project where it will tweet whether the lights are on or off in a room measured by a photo resistor (analog pin 0). I have it working except that the loop will constantly print the line. I only want the code "lightMonitorOn" and "lightMonitorOff" to run only when the condition is met in my loop.

int ledPin = 9;
int lightSensor = 0;
int threshold = 300;

void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}

void loop() {
while (analogRead(lightSensor) > threshold) {
lightMonitorOn();
}
while (analogRead(lightSensor) < threshold) {
lightMonitorOff();
}
}

void lightMonitorOn() {
// update the LED pin
digitalWrite(ledPin, LOW);
// send the string lights! back to the computer followed by a newline
Serial.println("lights are On!");
delay (3000);
}

void lightMonitorOff() {
// update the LED pin
digitalWrite(ledPin, HIGH);
// send the string lights! back to the computer followed by a newline
Serial.println("lights are off!");
delay (3000);
}

I hope this makes sense and if this has been explained once or 1000 times, feel free to give me crap and point me in the right direction where I can find the answer.

Thanks in advance!
James

I only want the code "lightMonitorOn" and "lightMonitorOff" to run only when the condition is met in my loop.

Then, printing the message in a while loop is inappropriate.

feel free to give me crap

All right. I'll remember this.

You need to keep track of whether you have sent each message, or not.

bool messOnSent = false;
bool messOffSent = false;

void loop()
{
   if (analogRead(lightSensor) > threshold)
   {
     if(!messOnSent)
     {
       lightMonitorOn();
       messOffSent = false;
     }
     messOnSent = true;
   }

   if (analogRead(lightSensor) < threshold)
   {
     if(!messOffSent)
     {
       lightMonitorOff();
       messOnSent = false;
    }
    messOffSent = true;
   }
}

Reading the sensor twice is unnecessary. The value read is either above the threshold, or it isn't. The second if() should be an else.

It's okay, I remembered about the boolean function! This is what I ended up with:

int lightPin = 0;
int ledPin = 9;
boolean trigger=false;

void setup()
{
 pinMode(ledPin, OUTPUT);
 pinMode(lightPin, INPUT);
 Serial.begin(9600);
}

void loop() {
   if (analogRead(lightPin) > 300 && !trigger) {     
      digitalWrite(ledPin, LOW);
      Serial.println("lights are On!");
      delay(2000);
      trigger=true;
        }
   if (analogRead(lightPin) < 300 && trigger) {
      digitalWrite(ledPin, HIGH);
      Serial.println("lights are Off!");
      delay(2000);
      trigger=false;
        }
  }

PaulS:

I only want the code "lightMonitorOn" and "lightMonitorOff" to run only when the condition is met in my loop.

Then, printing the message in a while loop is inappropriate.

feel free to give me crap

All right. I'll remember this.

You need to keep track of whether you have sent each message, or not.

bool messOnSent = false;

bool messOffSent = false;

void loop()
{
   if (analogRead(lightSensor) > threshold)
   {
     if(!messOnSent)
     {
       lightMonitorOn();
       messOffSent = false;
     }
     messOnSent = true;
   }

if (analogRead(lightSensor) < threshold)
   {
     if(!messOffSent)
     {
       lightMonitorOff();
       messOnSent = false;
    }
    messOffSent = true;
   }
}



Reading the sensor twice is unnecessary. The value read is either above the threshold, or it isn't. The second if() should be an else.

Ahhh this makes even more sense to me. I prefer to understand it as opposed to punching in the code in a brainless fashion. For this I thank you kind sir.

I prefer to understand it as opposed to punching in the code in a brainless fashion.

Great attitude. It will make this:

feel free to give me crap

a rare occurrence.

PaulS:

I prefer to understand it as opposed to punching in the code in a brainless fashion.

Great attitude. It will make this:

feel free to give me crap

a rare occurrence.

This is good news, I get enough crap from my wife anyway!

Thanks for all your help!
James