const int buttonPin = 10; // Pin connected to the button
const int motor1ForwardPin = 0; // Pin input for Motor 1 forward
const int motor1BackwardPin = 3; // Pin input for Motor 1 backward
const int motor2ForwardPin = 6; // Pin input for Motor 2 forward
const int motor2BackwardPin = 7; // Pin input for Motor 2 backward
const int stop1Pin = 4; // pin input stop motor 1 forward
const int stop2Pin = 5; // pin input stop motor 1 backward
const int stop3Pin = 8; // pin input stop motor 2 forward
const int stop4Pin = 9; // pin input stop motor 2 backward
unsigned long timeArray1[4] = {1000, 2000, 3000, 4000}; // Array of 4 time durations for Motor 1 (in milliseconds)
unsigned long timeArray2[4] = {1500, 2500, 3500, 4500}; // Array of 4 time durations for Motor 2 (in milliseconds)
bool motor1Running = false; // State of Motor 1
bool motor2Running = false; // State of Motor 2
unsigned long motor1StartTime = 0;// Time when Motor 1 was started
unsigned long motor2StartTime = 0;// Time when Motor 2 was started
unsigned long motor1RunDuration = 0;// Duration to run Motor 1
unsigned long motor2RunDuration = 0;// Duration to run Motor 2
bool motor1Direction = false; // Motor 1 direction: false = forward, true = backward
bool motor2Direction = false; // Motor 2 direction: false = forward, true = backward
unsigned long lastButtonPressTime = 0; // Last time the button was pressed
unsigned long previousMillis1 = 0; // store the last time motor 1 was updated
unsigned long previousMillis2 = 0; // store the last time motor 2 was updated
bool flag=0;// if have motor down
int motor1down=0;// motordown=0 motordown=1 motordown=2
bool flag2=0;// if have motor2 down
int motor2down=0;// motor2down=0 motor2down=1 motor2down=2
void setup() {
pinMode(buttonPin, INPUT); // Use the internal pull-up resistor
pinMode(motor1ForwardPin, OUTPUT);
pinMode(motor1BackwardPin, OUTPUT);
pinMode(motor2ForwardPin, OUTPUT);
pinMode(motor2BackwardPin, OUTPUT);
pinMode(stop1Pin, INPUT);
pinMode(stop2Pin, INPUT);
pinMode(stop3Pin, INPUT);
pinMode(stop4Pin, INPUT);
randomSeed(analogRead(0)); // Seed the random generator with a noise source
}
void loop() {
unsigned long currentMillis1 = millis();
unsigned long currentMillis2 = millis();
// Read the button state
int buttonState = digitalRead(buttonPin);
// Debounce the button
if (buttonState == HIGH ){
if ((currentMillis1 - lastButtonPressTime) > 200) {
lastButtonPressTime = currentMillis1; // Update the last button press time
// Check if the motors are not running
if (flag==0 && motor1down==0 ) {
// Motor 1
motor1Running = true;
//motor1StartTime = currentMillis1(); // Record the start time
motor1RunDuration = timeArray1[random(0, 4)]; // Randomly choose a duration from the array
motor1Direction = random(0, 2); // Randomly choose the motor direction
// Set Motor 1 direction and turn it on
if (motor1Direction) {
digitalWrite(motor1ForwardPin, LOW);
digitalWrite(motor1BackwardPin, HIGH); // Run Motor 1 backward
} else {
digitalWrite(motor1ForwardPin, HIGH); // Run Motor 1 forward
digitalWrite(motor1BackwardPin, LOW);
}
previousMillis1 = currentMillis1;
}
if (flag==1 && motor1down==1 ){
// Motor 1
motor1down = 0;
motor1Running = true;
//motor1StartTime = millis(); // Record the start time
motor1RunDuration = timeArray1[random(0, 4)]; // Randomly choose a duration from the array
motor1Direction = false; // motor direction forward
digitalWrite(motor1ForwardPin, LOW); // Stop Motor 1 forward
digitalWrite(motor1BackwardPin, HIGH); // Run Motor 1 backward
previousMillis1 = currentMillis1;
}
if (flag==1 && motor1down==0 ){
// Motor 1
motor1down = 0;
motor1Running = true;
//motor1StartTime = millis(); // Record the start time
motor1RunDuration = timeArray1[random(0, 4)]; // Randomly choose a duration from the array
motor1Direction = true; // motor1 direction backward
digitalWrite(motor1ForwardPin, HIGH); // Run Motor 1 forward
digitalWrite(motor1BackwardPin, LOW); // Stop Motor 1 backward
previousMillis1 = currentMillis1;
}
if (flag2==0 && motor2down==0){
// Motor 2
motor2Running = true;
//motor2StartTime = millis(); // Record the start time
motor2RunDuration = timeArray2[random(0, 4)]; // Randomly choose a duration from the array
motor2Direction = random(0, 2); // Randomly choose the motor direction
// Set Motor 2 direction and turn it on
if (motor2Direction) {
digitalWrite(motor2ForwardPin, LOW);
digitalWrite(motor2BackwardPin, HIGH); // Run Motor 2 backward
} else {
digitalWrite(motor2ForwardPin, HIGH); // Run Motor 2 forward
digitalWrite(motor2BackwardPin, LOW);
}
previousMillis2 = currentMillis2;
}
if (flag2==1 && motor2down==1 ){
// Motor 2
motor2down=0;
motor2Running = true;
//motor2StartTime = millis(); // Record the start time
motor2RunDuration = timeArray2[random(0, 4)]; // Randomly choose a duration from the array
motor2Direction = false; // motor2 direction forward
digitalWrite(motor2ForwardPin, LOW); // Stop Motor 2 forward
digitalWrite(motor2BackwardPin, HIGH); // Run Motor 2 backward
previousMillis2 = currentMillis2;
}
if (flag2==1 && motor1down==2 ){
// Motor 2
motor2down=0;
motor2Running = true;
//motor2StartTime = millis(); // Record the start time
motor2RunDuration = timeArray2[random(0, 4)]; // Randomly choose a duration from the array
motor2Direction = true; // motor direction backward
digitalWrite(motor2ForwardPin, HIGH); // Run Motor 2 forward
digitalWrite(motor2BackwardPin, LOW); // Stop Motor 2 backward
previousMillis2 = currentMillis2;
}
}
}
// Check if Motor 1 should be turned off due to stop1Pin
if (digitalRead(stop1Pin) == HIGH) {
motor1Running = false;
// Turn off Motor 1 forward direction
digitalWrite(motor1ForwardPin, LOW);
flag=1;
motor1down=1;
}
// Check if Motor 1 should be turned off due to stop2Pin
if (digitalRead(stop2Pin) == HIGH) {
motor1Running = false;
// Turn off Motor 1 forward direction
digitalWrite(motor1BackwardPin, LOW);
flag=1;
motor1down=2;
}
// Check if Motor 2 should be turned off due to stop3Pin
if (digitalRead(stop3Pin) == HIGH) {
motor2Running = false;
// Turn off Motor 2 forward direction
digitalWrite(motor2ForwardPin, LOW);
flag2=1;
motor2down=0;
}
// Check if Motor 2 should be turned off due to stop4Pin
if (digitalRead(stop4Pin) == HIGH) {
motor2Running = false;
// Turn off Motor 2 forward direction
digitalWrite(motor2BackwardPin, LOW);
flag2=1;
motor2down=0;
}
// Check if Motor 1 should be turned off based on duration
if (motor1Running ){
if(currentMillis1 - previousMillis1 >= motor1RunDuration)
{
motor1Running = false;
// Turn off Motor 1
digitalWrite(motor1ForwardPin, LOW);
digitalWrite(motor1BackwardPin, LOW);
}
}
// Check if Motor 2 should be turned off
if (motor2Running){
if (currentMillis2 - previousMillis2 >= motor2RunDuration)
{
motor2Running = false;
// Turn off Motor 2
digitalWrite(motor2ForwardPin, LOW);
digitalWrite(motor2BackwardPin, LOW);
}
}
}