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
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
//*********************************************************************************
int motorStart = 0; // pushball starting state
const int buttonA = 11; // start button
const int buttonB = 12; // stop button
const int buttonC = 13; // internal button C
const int relayPin1 = 22 ; // forward
const int relayPin2 = 23 ; // backward
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
pinMode(buttonA, INPUT_PULLUP);
pinMode(buttonB, INPUT);
pinMode(buttonC, INPUT_PULLUP);
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
Serial.begin(9600);
}
void loop() {
randomFunction();
joystickFunction ();
pushBallFunction();
}
//***********************************************************************************
void randomFunction() {
unsigned long currentMillis = millis();
int startButtonState = digitalRead(startButtonPin);
int stopRunButtonState = digitalRead(stopRunButtonPin);
int stopReverseButtonState = digitalRead(stopReverseButtonPin);
unsigned long currentMillis2 = millis();
int startButton2State = digitalRead(startButtonPin);
int stopRunButton2State = digitalRead(stopRunButton2Pin);
int stopReverseButton2State = digitalRead(stopReverseButton2Pin);
//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;
}
}
//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;
}
}
}
//********************************************************************************
void joystickFunction ()
{
int x; // x the variable for the position of the jostick on the horizontal axe
x=analogRead(JoyStick_X);
Serial.println(x);
if ((x > middleX + 100) and (digitalRead(stopRunButtonPin)== LOW)) // moving te joystick to the left
{
//Left direction <<<<<
digitalWrite(relay1Pin, HIGH);
digitalWrite(relay2Pin, LOW);
}
else if ((x < middleX - 100) and (digitalRead(stopReverseButtonPin) == LOW)) // moving te joystick to the right
{
// Right direction >>>>>
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, HIGH);
}
//else no action
// else
// {
// digitalWrite(relay1Pin, LOW);
// digitalWrite(relay2Pin, LOW);
// }
int y;
y=analogRead(JoyStick_Y);
Serial.println(y);
if ((y > middleY + 100) and (digitalRead(stopRunButton2Pin)== LOW))
{
//👆 Up direction
digitalWrite(relay3Pin, HIGH);
digitalWrite(relay4Pin, LOW);
//delay(50);
}
else if ((y < middleY - 100) and (digitalRead(stopReverseButton2Pin) == LOW))
{
//👇 Down direction
digitalWrite(relay3Pin, LOW);
digitalWrite(relay4Pin, HIGH);
// delay (50) ;
}
// else no action
// else
// {
// digitalWrite(relay3Pin, LOW);
// digitalWrite(relay4Pin, LOW);
// }
}
//***************************************************************************************
void pushBallFunction()
{
// Pulling the ball
if (digitalRead(buttonA) == LOW && digitalRead(buttonB) == LOW && digitalRead(buttonC) == HIGH) //On a press
//digitalRead(buttonB) == LOW &&
{
// motor running forward
digitalWrite(relayPin1,HIGH);
digitalWrite(relayPin2,LOW);
motorStart = 0;
Serial.println("forward");
}
if (digitalRead(buttonA) == LOW && digitalRead(buttonB) == HIGH && digitalRead(buttonC) == HIGH and (motorStart = 0)) //On a press
//digitalRead(buttonB) == LOW &&
{
// motor running forward
digitalWrite(relayPin1,HIGH);
digitalWrite(relayPin2,LOW);
motorStart = 1;
Serial.println("forward");
}
// coming back
if (digitalRead(buttonA) == HIGH && digitalRead(buttonB) == HIGH && digitalRead(buttonC) == LOW)// switch after serving the ball
//digitalRead(buttonA) == HIGH && digitalRead(buttonB) == HIGH &&
{
// motor running forward
digitalWrite(relayPin1,LOW);
digitalWrite(relayPin2,HIGH);
Serial.println("backward");
}
// Stop
if (digitalRead(buttonA) == HIGH && digitalRead(buttonB) == LOW && digitalRead(buttonC) == HIGH )// switch after serving the ball
//digitalRead(buttonA) == HIGH && digitalRead(buttonB) == LOW && digitalRead(buttonC) == HIGH
{
// motor stops on the way back
digitalWrite(relayPin1,LOW);
digitalWrite(relayPin2,LOW);
Serial.println("Stop");
}
}