int JoyStick_X = A1; //x
const int relay1Pin = 7; // Pin for relay to control right direction
const int relay2Pin = 8; // Pin for relay to control left direction
const int stopRunButtonPin = 2; // Pin for the stop run button
const int stopReverseButtonPin = 3; // Pin for the stop reverse button
const int middleX = 520; // the center value of the joystickX
int JoyStick_Y = A2; //y
const int relay3Pin = 9; // Pin for relay to control right direction
const int relay4Pin = 10; // Pin for relay to control left direction
const int stopRunButton2Pin = 4; // Pin for the stop run button
const int stopReverseButton2Pin = 5; // Pin for the stop reverse button
const int middleY = 520; // the center value of the joystickY
const int startButtonPin = 6; // Pin for the activation button
unsigned long times[] = {1000, 2000, 3000, 4000, 5000}; // Array of times in milliseconds
int numTimes = sizeof(times) / sizeof(times[0]);
unsigned long times2[] = {1000, 2000, 3000, 4000, 5000}; // Array of times in milliseconds
int numTimes2 = sizeof(times2) / sizeof(times2[0]);
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
unsigned long interval = 0;
unsigned long interval2 = 0;
bool motorRunning = false;
bool motorRunning2 = false;
int currentDirection = 0;
int currentDirection2 = 0;
int previousDirection = 0;
unsigned long lastButtonPressTime = 0;
// 定义按钮去抖时间(毫秒)
const unsigned long debounceDelay = 50;
bool flag=0;// if have motor down
int motordown=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(JoyStick_X, INPUT_PULLUP);
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);
pinMode(stopRunButtonPin, INPUT_PULLUP); // Use internal pull-up resistor
pinMode(stopReverseButtonPin, INPUT_PULLUP); // Use internal pull-up resistor
pinMode(JoyStick_Y, INPUT_PULLUP);
pinMode(relay3Pin, OUTPUT);
pinMode(relay4Pin, OUTPUT);
pinMode(stopRunButton2Pin, INPUT_PULLUP); // Use internal pull-up resistor
pinMode(stopReverseButton2Pin, INPUT_PULLUP); // Use internal pull-up resistor
pinMode(startButtonPin, INPUT_PULLUP); // Use internal pull-up resistor
randomSeed(analogRead(0)); // Seed random number generator
}
void loop() {
unsigned long currentMillis = millis();
int startButtonState = digitalRead(startButtonPin);
int stopRunButtonState = digitalRead(stopRunButtonPin);
int stopReverseButtonState = digitalRead(stopReverseButtonPin);
//if(startButtonState==LOW) {
if (currentMillis - lastButtonPressTime > debounceDelay) {
// 重置上次按钮按下时间
lastButtonPressTime = currentMillis;
if(startButtonState==LOW) {
if(flag==0 and motordown==0)// no led down
{
int randomIndex = random( numTimes);
interval = times[randomIndex];
currentDirection = random(0, 2);
//Start motor in chosen direction
if (currentDirection == 0) {
digitalWrite(relay1Pin, HIGH); // Move motor right
digitalWrite(relay2Pin, LOW);
} else if (currentDirection == 1)
{
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, HIGH); // Move motor left
}
previousMillis = currentMillis;
motorRunning = true;
}
if(flag=1 and motordown== 1)
{
int randomIndex = random( numTimes);
interval = times[randomIndex];
currentDirection = 1;
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, HIGH); // Move motor left
previousMillis = currentMillis;
motorRunning = true;
motordown=0;
flag=0;
}
if(flag=1 and motordown== 2)
{
int randomIndex = random( numTimes);
interval = times[randomIndex];
currentDirection = 0;
digitalWrite(relay1Pin, HIGH); // Move motor right
digitalWrite(relay2Pin, LOW);
previousMillis = currentMillis;
motorRunning = true;
motordown=0;
flag=0;
}
}
}
if( stopReverseButtonState == 1)
{
digitalWrite(relay1Pin, LOW);
motordown=1;
flag=1;
}
if( stopRunButtonState == 1)
{
digitalWrite(relay2Pin, LOW);
motordown=2;
flag=1;
}
if (motorRunning) {
// Check if interval has passed
if (currentMillis - previousMillis >= interval) {
//Stop motor
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, LOW);
motorRunning = false;
}
}
unsigned long currentMillis2 = millis();
int startButton2State = digitalRead(startButtonPin);
int stopRunButton2State = digitalRead(stopRunButton2Pin);
int stopReverseButton2State = digitalRead(stopReverseButton2Pin);
//if(startButtonState==LOW) {
if (currentMillis2 - lastButtonPressTime > debounceDelay) {
// 重置上次按钮按下时间
lastButtonPressTime = currentMillis2;
if(startButtonState==LOW) {
if(flag2==0 and motor2down==0)// no led down
{
int random2Index = random( numTimes2);
interval2 = times2[random2Index];
currentDirection2 = random(0, 2);
//Start motor in chosen direction
if (currentDirection2 == 0) {
digitalWrite(relay3Pin, HIGH); // Move motor right
digitalWrite(relay4Pin, LOW);
} else if (currentDirection2 == 1)
{
digitalWrite(relay3Pin, LOW);
digitalWrite(relay4Pin, HIGH); // Move motor left
}
previousMillis2 = currentMillis2;
motorRunning2 = true;
}
if(flag2=1 and motor2down== 1)
{
int random2Index = random( numTimes2);
interval2 = times2[random2Index];
currentDirection2 = 1;
digitalWrite(relay3Pin, LOW);
digitalWrite(relay4Pin, HIGH); // Move motor left
previousMillis2 = currentMillis2;
motorRunning2 = true;
motor2down=0;
flag2=0;
}
if(flag2=1 and motor2down== 2)
{
int random2Index = random( numTimes2);
interval2 = times2[random2Index];
currentDirection2 = 0;
digitalWrite(relay3Pin, HIGH); // Move motor right
digitalWrite(relay4Pin, LOW);
previousMillis2 = currentMillis2;
motorRunning2 = true;
motor2down=0;
flag2=0;
}
}
}
if( stopReverseButton2State == 1)
{
digitalWrite(relay3Pin, LOW);
motor2down=1;
flag2=1;
}
if( stopRunButton2State == 1)
{
digitalWrite(relay4Pin, LOW);
motor2down=2;
flag2=1;
}
if (motorRunning2) {
// Check if interval2 has passed
if (currentMillis2 - previousMillis2 >= interval2) {
//Stop motor
digitalWrite(relay3Pin, LOW);
digitalWrite(relay4Pin, LOW);
motorRunning2 = false;
}
}
}