Arduino Babyphone

Hi community!

I'm unsure about programming and couldn't find any similar topic. If I'm wrong about the similar topic, correct and redirect me please.

What I want to do: I have a PIR sensor that controls via Arduino a relais module. The relais "presses the PTT button" on a PMR radio. That means when my little boy moves in his bed, the PIR sensor goes to high and the relais closes the PTT circuit on the radio and I can hear him moan or cry on the other station. (Yeah, I know, there are babyphones for sale for a few bugs, but the range is too weak for my purpose. And all babyphone apps for smartphones have problems when one station is not in my WIFI.)

All in all, the app works already: When the sensor detects a motion, the relais connects through. But by now this happens immediately, but I want Arduino to connect through only when motion is detected for more than 10 seconds. "DELAY" is no option, I want the program to control the sensor, even when waiting for this 10 second delay.

This is what I have so far. Can this work? I have a hunch I made an error in reasoning:

int relaisPin = 4;              // choose the pin for the relais
int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
// int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
unsigned long myTime;           // variable for counting time

void setup() {
  pinMode(ledPin, OUTPUT);      //declare LED Pin as output
  pinMode(inputPin, INPUT);     //declare sensor signal pin as input
  pinMode(relaisPin, OUTPUT);   //declare relais signal pin as OUTPUT

//  Serial.begin(9600);
}

void loop() {
  val = digitalRead(inputPin);  // read input value
  myTime = millis();            // set count
  if (val == HIGH) {            // check if the input is HIGH
//    if (millis() > myTime + 10000) {
    while ((val == HIGH) && (millis() > myTime + 10000) {
      digitalWrite(ledPin, HIGH);  // turn LED ON
      digitalWrite(relaisPin, HIGH); //turn relais ON
    }
  } else {
    digitalWrite(ledPin, LOW); // turn LED OFF
    digitalWrite(relaisPin, LOW); //turn relais OFF
  }
}

Can someone tell me, if this will work as assumed or what I have to change?

Greets, Oliver

You are at the point where what we call "testing" can begin.
Paul

Note it is usually better to use

(millis() - myTime > 10000)

rather than

(millis() > myTime + 10000)

to allow for roll over of the[tt] milis() value.[/tt]

[tt][color=#000000]I believe you have an error of logic in that you obtain the value for mytime just before you test it in the while loop. As a consequence I don't believe you will ever enter the while loop and as a result never turn on the relay.[/color]

[/tt]

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.