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 */
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;
int pattern35= 0b00101000;
int pattern36= 0b00100100;
int pattern45= 0b00011000;
int pattern46= 0b00010100;
// IN2Pin = QO 595
// IN1Pin = Q1 595
// relayPin1 = Q2 595
// relayPin2 = Q3 595
// relayPin3 = Q4 595
// relayPin4 = Q5 595
// buttonA = D0 165 01111111
// buttonB = D1 165 10111111
// buttonC = D2 165 11011111
// stopLeftButton = D3 165 11101111
// stopRightButton = D4 165 11110111
// stopUpButton = D5 165 11111011
// stopDownButton = D6 165 11111101
// randomButton = D7 165 1111111
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;
int stopLeftArray [] = {B11110111, B11110011, B11110101 };
int stopRightArray [] = {B11101111, B11101101, B11101011 };
int stopUpArray [] = {B11111101, B11101101, B11110101};
int stopDownArray [] = {B11111011, B11101011, B11110011};
bool flag = 0;
int motordown = 0;
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, HIGH);
}
void loop() {
digitalWrite(latchPin165, LOW);
delayMicroseconds(5);
digitalWrite(latchPin165, HIGH);
delayMicroseconds(5);
digitalWrite(clockPin165,HIGH);
digitalWrite(clockEnablePin165, LOW);
byte incoming = shiftIn(dataPin165,clockPin165, LSBFIRST );
digitalWrite(clockEnablePin165,HIGH);
Serial.print("Pin States:\r\n");
Serial.println(incoming, BIN);
delay(300);
// Check the current state of the button connected to the first pin (assuming active low)
bool randombutton = (incoming == B11111110); // 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 stopUpButtonState = (incoming == B1101111);
// check the stop backward button and the random button are pressed at the same time
bool stopDownButtonState = (incoming == B1110111);
if (randombutton && !motorRunning)
{
motorRunning = true;
motorStartTime = millis(); // Record the start time
motorRunDuration = timeArray[random(0, 4)]; // Randomly choose a duration from the array
motor2RunDuration = timeArray2[random(0, 4)]; // Randomly choose a duration from the array
motorDirection = random(0, 2); // Randomly choose the motor direction
motor2Direction = random(0, 2); // Randomly choose the motor direction
// Turn on the motor
digitalWrite(latchPin595, LOW);
if (!motorDirection && motor2Direction)
{
shiftOut(dataPin595, clockPin595, LSBFIRST, pattern35); // Pattern for reverse
}
if (motorDirection && !motor2Direction)
{
shiftOut(dataPin595, clockPin595, LSBFIRST, pattern36); // Pattern for forward
}
if (motorDirection && motor2Direction)
{
shiftOut(dataPin595, clockPin595, LSBFIRST, pattern46); // Pattern for reverse
}
if (!motorDirection && !motor2Direction)
{
shiftOut(dataPin595, clockPin595, LSBFIRST, pattern45); // Pattern for forward
}
digitalWrite(latchPin595, HIGH);
}
// Check if the motors should be turned off after the running duration
if (motorRunning && millis() - motorStartTime >= max(motorRunDuration, motor2RunDuration))
{
motorRunning = false; // Turn off the motors
digitalWrite(latchPin595, LOW);
shiftOut(dataPin595, clockPin595, LSBFIRST, pattern0);
digitalWrite(latchPin595, HIGH);
}
}
// void loop()
// {
// // Load the parallel data from 74HC165
// digitalWrite(latchPin165, LOW);
// delayMicroseconds(5);
// digitalWrite(latchPin165, HIGH);
// delayMicroseconds(5);
// digitalWrite(clockPin165, HIGH);
// digitalWrite(clockEnablePin165, LOW);
// byte buttonStates = shiftIn(dataPin165, clockPin165, LSBFIRST);
// digitalWrite(clockEnablePin165, HIGH);
// Serial.print("Pin States:\r\n");
// Serial.println(buttonStates, BIN);
// delay(300);
// // Check the current state of the start button (assuming active low, connected to D0)
// bool randomButton = !(buttonStates & B00000001);
// // Check the current state of the stop button for forward direction (assuming active low, connected to D1)
// bool stopForwardButtonState = !(buttonStates & B00000010);
// // Check the current state of the stop button for reverse direction (assuming active low, connected to D2)
// bool stopReverseButtonState = !(buttonStates & B00000100);
// // Start Motor 1 if the start button is pressed and Motor 1 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 first array
// motorDirection = random(0, 2); // Randomly choose the motor direction
// // Set Motor 1 direction and turn it on
// 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);
// }
// }
// // Start Motor 2 if Motor 1 has finished running
// if (motorRunning && millis() - motorStartTime >= motorRunDuration)
// {
// motorRunning = false; // Turn off Motor 1
// digitalWrite(latchPin595, LOW);
// shiftOut(dataPin595, clockPin595, LSBFIRST, 0);
// digitalWrite(latchPin595, HIGH); // Start Motor 2
// motor2Running = true;
// motor2StartTime = millis(); // Record the start time
// motor2RunDuration = timeArray2[random(0, 4)]; // Randomly choose a duration from the second array
// motor2Direction = random(0, 2); // Randomly choose the motor direction
// // Set Motor 2 direction and turn it on
// digitalWrite(latchPin595, LOW);
// if (motor2Direction)
// {
// shiftOut(dataPin595, clockPin595, LSBFIRST, pattern5); // Pattern for reverse
// }
// else
// {
// shiftOut(dataPin595, clockPin595, LSBFIRST, pattern6); // Pattern for forward
// }
// digitalWrite(latchPin595, HIGH);
// }
// // Check if Motor 2 should be turned off after the running duration
// if (motor2Running && millis() - motor2StartTime >= motor2RunDuration)
// {
// motor2Running = false; // Turn off Motor 2
// digitalWrite(latchPin595, LOW); shiftOut(dataPin595, clockPin595, LSBFIRST, pattern0);
// digitalWrite(latchPin595, HIGH);
// }
// // // if (randomButton && !motorRunning && stopleft )
// // if (B11110110 || B11110011, B11110101)
// // {
// // {
// // motorRunning = true;
// // motorStartTime = millis(); // Record the start time
// // motorRunDuration = timeArray[random(0, 4)]; // Randomly choose a duration from the first array
// // motorDirection = random(0, 2); // Randomly choose the motor direction
// // // Set Motor 1 direction and turn it on
// // digitalWrite(latchPin595, LOW);
// // if (motorDirection)
// // {
// // shiftOut(dataPin595, clockPin595, LSBFIRST, 0b00100000); // Pattern for reverse
// // }
// // else
// // {
// // shiftOut(dataPin595, clockPin595, LSBFIRST, 0b00010000); // Pattern for forward
// // }
// // digitalWrite(latchPin595, HIGH);
// // }
// // }
// }