// https://forum.arduino.cc/t/need-help-writing-code-for-project-after-using-ai-bots-didnt-work/1277420/
/*
- If either sensor is triggered for at least 3 seconds
- the motor starts moving in direction 1 (for a set duration of time).
- During movement in direction 1, the sensors do not monitor
- Once the initial movement is complete, sensors monitor activity within 1.5 feet.
The motor will not move in direction 2 (opposite dir.) for the same set amount of time until the path is clear for at least 3 seconds.
While the motor is moving in direction 2:
Continuously monitor the sensors.
If the sensors detect an object within 1.5 feet, stop the motor and reverse direction back to direction 1.
Move in direction 1 for the same amount of time it was moving in direction 2
Once the movement back to direction 1 is complete, monitor the sensors again.
If no activity is detected for at least 3 seconds, the motor will attempt to move in direction 2 again.
*/
byte sensorArray[] = {2, 3}; // two sensors
byte ledArray[] = {7, 8}; // two LEDs
unsigned long timeoutArray[] = {3000, 3000}; // two durations
unsigned long sensorTimer[2];
void setup() {
Serial.begin(115200);
for (byte pin = 0; pin < 2; pin++) {
pinMode(ledArray[pin], OUTPUT); // LEDs for the buttons
}
for (byte pin = 0; pin < 2; pin++) {
pinMode(sensorArray[pin], INPUT_PULLUP); // sensor pin >> N.O. button pins >> ground
}
}
void loop() {
for (byte pin = 0; pin < 2; pin++) { // reading both input pins
if (!digitalRead(sensorArray[pin])) { // if button is held
if (millis() - sensorTimer[pin] > timeoutArray[pin]) { // and timeout is reached
movement(pin); // move the motor using [pin]
}
} else { // button was released
digitalWrite(ledArray[pin], LOW); // LED off
sensorTimer[pin] = millis(); // reset timer
}
}
}
void movement(byte pin) { // use this function for motor movement (open AND close)
digitalWrite(ledArray[pin], HIGH); // LED on
}
____HOLD____
(three seconds)
(start motors)