2

I am measuring light, temperature and humidity on a arduino uno, and have programmed the loop to run every minute. Monitoring the values on the serial monitor.

However, what i would like is the code to run once to get the values, then wait, or pause, until one of the values change, and then output this on the serial monitor.

I want to get informed of changes in the sensors immediately, not wait for a minute for the loop to run. Is there a way to do this?

Thanks.

So I would need to add an if/else function to the following code?

int lightPin = A5;

int lightok = 9;

int lighthigh = 10;

  void setup()

{
  Serial.begin(9600);
  pinMode(lightok,OUTPUT);
  pinMode(lighthigh,OUTPUT);
}

    void loop()
{
delay(600000);

   int lightlevel = analogRead(lightPin);

   lightlevel = map(lightlevel, 0, 1023, 0, 255);

   lightlevel = constrain(lightlevel, 0, 255);


   Serial.print("Lightlevel: ");

   Serial.println(lightlevel);

   //led control for light levels

   if (lightlevel < 15 || lightlevel > 125) {

       digitalWrite(lighthigh, HIGH);

       digitalWrite(lightok, LOW);

   }  else {

       digitalWrite(lighthigh, LOW);

       digitalWrite(lightok, HIGH);

   }
}
cjh.red
  • 23
  • 1
  • 4

1 Answers1

3

You have to ways of doing this:

  • you can run your loop faster, say every second or half-second and only output a value to serial if the value is different from the previous one or if the difference is bigger than a specific value
  • you can setup interrupts run code only when something happens. But I need to know the sensor you are using to be more specific on this one.

Hope this helps! :)

EDIT

Okay, I'd do something like that :

int lightSensorPin = A5;
int lightOkPin = 9;
int lightHighPin = 10;

int currentLightLevel = 0;
int previousLightLevel = 0;
int delta = 0;
int deltaValue = 10; // needs to be changed to suit your needs

void setup() {

    Serial.begin(9600);
    pinMode(lightOkPin, OUTPUT);
    pinMode(lightHighPin, OUTPUT);

}
void loop() {

    currentLightLevel = analogRead(lightSensorPin); //read the sensor

    currentLightLevel = map(currentLightLevel, 0, 1023, 0, 255); // map the value
    currentLightLevel = constrain(currentLightLevel, 0, 255); // not sure this is useful

    delta = abs(previousLightLevel - currentLightLevel); // calculate the absolute value of the difference btw privous and current light value

    if (delta >= deltaValue) { // if the difference is higher than a threshold
        Serial.print("currentLightLevel: ");
        Serial.println(currentLightLevel);

        //led control for light levels
        if (currentLightLevel < 15 || currentLightLevel > 125) {
            digitalWrite(lightHighPin, HIGH);
            digitalWrite(lightOkPin, LOW);
        }
        else {
            digitalWrite(lightHighPin, LOW);
            digitalWrite(lightOkPin, HIGH);
        }
    }

    previousLightLevel = currentLightLevel;

    delay(1000);
}
ladislas
  • 3,037
  • 19
  • 27
  • Thanks. I've added a section of code to the question. Do I need to add a if/else function to this? – cjh.red Aug 08 '14 at 07:46
  • @cjh.red can you you show me you whole code with `setup()` and `loop()` please :) – ladislas Aug 08 '14 at 08:34
  • hi, i've updated the to include the loop, is this OK? Sorry if its not best practice :) – cjh.red Aug 08 '14 at 12:42
  • Thanks, and thanks again - one more question - I'm starting to add more sensors (such as temperature) what are the key things to repeat in the code to keep it clean e.g. should I specify the delta for each sensor, such as deltaTemp, deltaHumidity etc.? – cjh.red Aug 08 '14 at 15:39
  • @cjh.red yes, as well as specific `current` and `previous` values such as `currentTemp`, `currentLight`, and so on. You may also want to move repetitive code inside functions. :) and don't forget to mark my answer as *the* answer if it solved your problem ;) – ladislas Aug 09 '14 at 16:28