Hello,
I am trying to get help with activating a continuous motor (American Robotic Supply MG-360) via PIR sensor and Arduino Uno. I am trying to get it setup so that when the PIR sensor detects motion, it will activate the motor for 30 seconds and then shuts off the motor. I have been trying to get this accomplished for a while now and I am stuck. Everything I try results in the motor staying on or not turning on at all. I am at the point where I am willing to pay someone for help!
It sounds like what you are looking for is a form of One-Shot Timer. I have an article with code showing how to do it here. Connect your PIR sensor output at pin 2 to trigger it, but you may need to change the code to trigger on HIGH instead of LOW.
If you need further help, send me a PM.
KULIK121:
Hello,
I am trying to get help with activating a continuous motor (American Robotic Supply MG-360) via PIR sensor and Arduino Uno. I am trying to get it setup so that when the PIR sensor detects motion, it will activate the motor for 30 seconds and then shuts off the motor. I have been trying to get this accomplished for a while now and I am stuck. Everything I try results in the motor staying on or not turning on at all. I am at the point where I am willing to pay someone for help!
Hello
you can use the delay() function or a homebrewed timer() function using the Arduino millis() function to control your servo via the PIR input.
The usage of the millis function is always the best way to generate a block-free execution for further function extension of the sketch.
See this forum post from just this evening. Pretty much exactly what you are looking for. Just change the timer value from 10 sec to 30 seconds.
https://forum.arduino.cc/index.php?topic=721242.0
-jim lee
You can use a simple state machine.
#define RUN_TIME_MS 30000
#define TIMEOUT_MS 1000
enum CurrentMotorTaskState {
READY,
RUNNING,
TIMEOUT
};
static uint8_t trigger = 0;
void motorTask() {
static enum CurrentMotorTaskState currentState = READY;
static uint32_t t = 0;
switch (currentState) {
case READY:
if (trigger) {
t = millis();
// Start the motor here
currentState = RUNNING;
}
break;
case RUNNING:
if (millis() - t >= RUN_TIME_MS) {
// Stop the motor here
t += RUN_TIME_MS;
currentState = TIMEOUT;
}
break;
case TIMEOUT:
if (millis() - t >= TIMEOUT_MS) {
currentState = READY;
}
break;
}
}
void sensorTask() {
// Read the sensor value and set the trigger variable
trigger = 1;
}
void setup() {
// Initialize stuff here
}
void loop() {
sensorTask();
motorTask();
}
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.