Run the code if the condition is true for 5 seconds

im working a project with MPU6050 sensor and i want the buzzer to turn on if the gyroscope detects no motion for 5 second

..in which case, "LEDs and multiplexing" is an odd choice of forum section.

which section should i post on?

Review: https://github.com/mjs513/MPU6050-Motion-Detection/blob/master/MPU6050_MotionDetect.ino

From what I understand, the motion interrupt is experimental and has caveats, but should be explored.

In the above, if you are getting the motion signal, just keep clearing a long variable which is being updated often in loop() by millis() ... if the variable is > 5000, 5 seconds have expired.

If the interrupt fails, you will need to capture the sensor variables with shadow variables and a timestamp. Then on each read, compare variables to the shadow, if not changed that means no motion... continue to read and update the timestamp until it is >= 5000.

Look at the Blink Without Delay example in the IDE (File->examples->02.digital->Blink Without Delay) to get some knowledge about how to deal with elapsed time.

For your program, if the sensor detects movement, set some variable and make sure the buzzer is off

lastMovementTime = millis();
// turn buzzer off

and then, every time through loop(), test to see how much time has elapsed and turn on the buzzer

if ( millis() - lastMovementTime >= noMotionLimit ) {
// turn on buzzer
}

You may want to also track when you turned the buzzer on so you can turn if off after some time (like only buzz for 3 seconds or something)

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