const int dataPin595 = 5; /* DS */
const int clockPin595 = 6; /* SHCP */
const int latchPin595 = 7; /* STCP */
const int dataPin165 = 10; /* Q7 */
const int clockPin165 = 9; /* CP */
const int latchPin165 = 8; /* PL */
const int clockEnablePin165 = 11; /* CE */
//const int randomButtonPin = 13; /* random buttom */
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 pattern0 = 0b00000000;
int pattern1 = 0b10000000;
int pattern2 = 0b01000000;
int pattern3 = 0b00100000;
int pattern4 = 0b00010000;
int pattern5 = 0b00001000;
int pattern6 = 0b00000100;
int pattern7 = 0b00000010;
int pattern8 = 0b00000001;
// IN2Pin = QO 595
// IN1Pin = Q1 595
// relayPin1 = Q2 595
// relayPin2 = Q3 595
// relayPin3 = Q4 595
// relayPin4 = Q5 595
// stopLeftButton = D3 165 11101111
// stopRightButton = D4 165 11110111
// stopUpButton = D5 165 11111011
// stopDownButton = D6 165 11111101
// randomButton = D7 165 1111111
// D
unsigned long timeArray[4] = {5000, 7000, 8000, 10000}; // Array of 4 time durations (in milliseconds)
unsigned long timeArray2[4] = {1000, 2000, 3000, 4000}; // motor2 Array of 4 time durations
bool motorRunning = false; // State of the motor
bool motor2Running = false; // State of the motor2
unsigned long motorStartTime = 0; // Time when the motor was started
unsigned long motor2StartTime = 0; // Time when the motor2 was started
unsigned long motorRunDuration = 0; // Duration to run the motor
unsigned long motor2RunDuration = 0; // Duration to run the motor2
bool motorDirection = false;
bool motor2Direction = false;
void setup() {
pinMode(dataPin595, OUTPUT);
pinMode(clockPin595, OUTPUT);
pinMode(latchPin595, OUTPUT);
pinMode(dataPin165, INPUT);
pinMode(clockPin165, OUTPUT);
pinMode(latchPin165, OUTPUT);
pinMode(clockEnablePin165, OUTPUT);
//pinMode(randomButtonPin, INPUT);
randomSeed(analogRead(0)); // Seed random number generator
Serial.begin(9600);
// Start with the motors off
digitalWrite(latchPin595, LOW);
shiftOut(dataPin595, clockPin595, LSBFIRST, 0);
digitalWrite(latchPin595,
}
void loop() {
digitalWrite(latchPin165, LOW);
delayMicroseconds(5);
digitalWrite(latchPin165, HIGH);
delayMicroseconds(5);
digitalWrite(clockPin165,HIGH);
digitalWrite(clockEnablePin165, LOW);
byte incoming = shiftIn(dataPin165,clockPin165, MSBFIRST );
digitalWrite(clockEnablePin165,HIGH);
Serial.print("Pin States:\r\n");
Serial.println(incoming, BIN);
delay(300);
//unsigned long currentMillis = millis();
// Check the current state of the button connected to the first pin (assuming active low)
bool randombutton = (incoming == B1111111); // Invert logic if active low
// Check the current state of the stop button for forward direction (assuming active low, connected to D1)
bool stopForwardButtonState = (incoming == B11101111);
// Check the current state of the stop button for reverse direction (assuming active low, connected to D2)
bool stopReverseButtonState = (incoming == B11110111);
// check the stop forward button and the random button are pressed at the same time
bool randomSstopForwardButtonState = (incoming == B1101111);
// check the stop backward button and the random button are pressed at the same time
bool randomSstopReverseButtonState = (incoming == B1110111);
// Check if the button is pressed and motor is not running
if (randombutton && !motorRunning)
{
motorRunning = true;
motorStartTime = millis(); // Record the start time
motorRunDuration = timeArray[random(0, 4)]; // Randomly choose a duration from the array
motorDirection = random(0, 2); // Randomly choose the motor direction
// Turn on the motor
digitalWrite(latchPin595, LOW);
if (motorDirection)
{
shiftOut(dataPin595, clockPin595, LSBFIRST, pattern3); // Pattern for reverse
}
else
{
shiftOut(dataPin595, clockPin595, LSBFIRST, pattern4); // Pattern for forward
}
digitalWrite(latchPin595, HIGH);
}
// check the stop forward button and the random button
if (randomSstopForwardButtonState && !motorRunning)
{
motorRunning = true;
motorStartTime = millis(); // Record the new start time
motorRunDuration = timeArray[random(0, 4)]; // Randomly choose a new duration from the array
// Set motor direction to reverse
// motorDirection = true;
digitalWrite(latchPin595, LOW);
shiftOut(dataPin595, clockPin595, LSBFIRST, pattern4); // Pattern for reverse
digitalWrite(latchPin595, HIGH);
}
// check the stop backward button and the random button
if (randomSstopReverseButtonState && !motorRunning)
{
motorRunning = true;
motorStartTime = millis(); // Record the new start time
motorRunDuration = timeArray[random(0, 4)]; // Randomly choose a new duration from the array
// Set motor direction to forward
// motorDirection = true;
digitalWrite(latchPin595, LOW);
shiftOut(dataPin595, clockPin595, LSBFIRST, pattern3); // Pattern for forward
digitalWrite(latchPin595, HIGH);
}
// Check if the motor should be turned off
if (motorRunning && millis() - motorStartTime >= motorRunDuration) {
motorRunning = false;
// Turn off the motor
digitalWrite(latchPin595, LOW);
shiftOut(dataPin595, clockPin595, LSBFIRST, pattern0);
digitalWrite(latchPin595, HIGH);
}
//*************************************************************************
}