mamsoudi
mamsoudi

Reputation: 4059

Send two different kinds of sensor data using Arduino

I'm trying to config an Arduino with a PIR motion detector, to send motion detector data and some randomly generated temperature to the gateway.

I want to make it send "MO/1" once motion is detected and still keep sending temperature every 20 seconds like "T/26".

I've used this code but no success:

void loop() {
    if (motion == HIGH) {
       // Motion Detected
       // Send to Gateway
    }

    while (1) {
       temp = random(1,5) + 28;
       // Send to Gateway
       delay(20000);
    }
}

As you may notice, once Arduino enters while it won't pay attention to the if block! Since I'm new to Arduino and programming them, I thought someone could help with this.

Upvotes: 0

Views: 77

Answers (1)

dda
dda

Reputation: 6203

This won't work as you noticed.

You need to use a variable, to calculate the time elapsed since last check.

unsigned long t1;
void setup() {
  ...
  t1=millis();
}

void loop() {
  if (motion == HIGH) {
     // Motion Detected
     // Send to Gateway
  }
  if(millis()-t1>20000) {
    temp = random(1, 5) + 28;
    // Send to Gateway
    t1=millis();
  }
}

Upvotes: 3

Related Questions