const int dataPin595 = 5; /* DS */
const int clockPin595 = 6; /* SHCP */
const int latchPin595 = 7; /* STCP */
//IN1Pin = Q0 595
//IN2Pin = Q1 595
//relay1Pin = Q2 595 bleu
//relay2Pin = Q3 595 yellow
//relay3Pin = Q4 595 purple
//relay4Pin = Q5 595 orange
const int dataPin165 = 10; /* Q7 */
const int clockPin165 = 9; /* CP */
const int latchPin165 = 8; /* PL */
const int clockEnablePin165 = 11; /* CE */
// ButtonA = DO 165 B11111110
// ButtonB = D1 165 B11111101
// ButtonC = D2 165 B11111011
// stopLeftButton = D3 165 B11110111
// stopRightButton = D4 165 B11101111
// stopUpButton = D5 165 B11011111
// stopDownButton = D6 165 B10111111
int pattern0 = 0b00000000;
int pattern1 = 0b10000000;
int pattern2 = 0b01000000;
int pattern3 = 0b00100000;
int pattern4 = 0b00010000;
int pattern5 = 0b00001000;
int pattern6 = 0b00000100;
// joystick............
int leftStopArray [] = {B11111111, B11101111, B11011111, B10111111, B10101111, B11001111};
int rightStopArray [] = {B11111111, B11110111, B11011111, B10111111, B10110111, B11010111};
int upStopArray [] = {B11111111, B10111111, B11110111, B11101111, B10110111, B10101111};
int downStopArray [] = {B11111111, B11011111, B11101111, B11110111, B11001111, B11010111};
int JoyStick_X = A1; //x
const int middleX = 520; // the center value of the joystickX
int JoyStick_Y = A2; //y
const int middleY = 520; // the center value of the joystickY
// PushBall..........
bool motorOff = true;
bool motorBack = false;
bool motorForward = false;
// Random................
// stopLeftButton = D3 165 11101111
// stopRightButton = D4 165 11110111
// stopUpButton = D5 165 11111011
// stopDownButton = D6 165 11111101
// randomButton = D7 165 1111111
const int startButton = 12; /* random buttom */
unsigned long timeArray[4] = {5000, 7000, 8000, 10000}; // Array of 4 time durations (in milliseconds)
unsigned long timeArray2[4] = {9000, 6000, 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(JoyStick_X, INPUT_PULLUP);
pinMode(JoyStick_Y, INPUT_PULLUP);
pinMode(dataPin595, OUTPUT);
pinMode(clockPin595, OUTPUT);
pinMode(latchPin595, OUTPUT);
pinMode(dataPin165, INPUT);
pinMode(clockPin165, OUTPUT);
pinMode(latchPin165, OUTPUT);
pinMode(clockEnablePin165, OUTPUT);
pinMode(startButton, INPUT_PULLUP);
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()
{
pushBallFunction ();
joystickFunction ();
randomFunction();
}
void randomFunction()
{
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(30);
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
// MOTOR1
// Check the current state of the stop button for forward direction (assuming active low, connected to D1)
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 == B11110111);
// Check the current state of the stop button for reverse direction (assuming active low, connected to D2)
bool stopReverseButtonState = (incoming == B11101111);
// check the stop forward button and the random button are pressed at the same time
bool randomSstopForwardButtonState = (incoming == B11110111);
// check the stop backward button and the random button are pressed at the same time
bool randomSstopReverseButtonState = (incoming == B11101111);
// MOTOR2
// Check the current state of the stop button for forward direction (assuming active low, connected to D1)
bool stopUpwardButtonState = (incoming == B11011111);
// Check the current state of the stop button for forward direction (assuming active low, connected to D1)
bool stopDwnwardButtonState = (incoming == B10111111);
// check the stop forward button and the random button are pressed at the same time
bool randomSstopUpwardButtonState = (incoming == B11011111);
// Check the current state of the stop button for forward direction (assuming active low, connected to D1)
bool randomSstopDownwardButtonState = (incoming == B10111111);
// Check if the button is pressed and motor is not running
//if (randombutton && !motorRunning)
if (digitalRead(startButton)== LOW && !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);
Serial.println("done");
//*************************************************************************
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
// Turn on the motor
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 the stop backward button and the random button
if (randomSstopUpwardButtonState && motor2Running)
{
//Serial.println("done1");
motor2Running = true;
motor2StartTime = millis(); // Record the new start time
motor2RunDuration = timeArray2[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, pattern6); // Pattern for forward
digitalWrite(latchPin595, HIGH);
Serial.println("done1");
}
// check the stop backward button and the random button
if (randomSstopDownwardButtonState && motor2Running)
{
motor2Running = true;
motor2StartTime = millis(); // Record the new start time
motor2RunDuration = timeArray2[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, pattern5); // Pattern for forward
digitalWrite(latchPin595, HIGH);
Serial.println("done2");
}
if (motor2Running && millis() - motor2StartTime >= motor2RunDuration) {
motor2Running = false;
// Turn off the motor
digitalWrite(latchPin595, LOW);
shiftOut(dataPin595, clockPin595, LSBFIRST, pattern0);
digitalWrite(latchPin595, HIGH);
Serial.println("done");
}
}
//*********************************************************
void pushBallFunction () {
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(50);
// while the motor still resting on ButtonB (B11111101),
if((incoming == B11111100)&& !motorForward)
{ // press the buttonA when the motor is stopMode
digitalWrite(latchPin595, LOW);
shiftOut(dataPin595, clockPin595, LSBFIRST, pattern1); // motor start pushing 0b10000000
digitalWrite(latchPin595, HIGH);
motorForward = true;
motorBack = false;
Serial.print("Start pushing");
}
// after the release of buttonA keep pushing (B11111111),
if((incoming == B11111111)&& motorForward)
{ // after release of Button A and B
digitalWrite(latchPin595, LOW);
shiftOut(dataPin595, clockPin595, LSBFIRST, pattern1); // keep pushing 0b10000000
digitalWrite(latchPin595, HIGH);
motorForward = true;
motorBack = false;
Serial.print("Pushing");
}
// Reverse after heating buttonC (B11111011),
if((incoming == B11111011)&& !motorBack && motorForward){
digitalWrite(latchPin595, LOW);
shiftOut(dataPin595, clockPin595, LSBFIRST, pattern2); // motor start pulling
digitalWrite(latchPin595, HIGH);
motorOff = false;
motorForward = false;
motorBack = true;
Serial.print("Start pulling");
}
// after the release of buttonC keep pulling (B11111111),
if((incoming == B11111111)&& motorBack && !motorForward){
digitalWrite(latchPin595, LOW);
shiftOut(dataPin595, clockPin595, LSBFIRST, pattern2); // motor start pulling
digitalWrite(latchPin595, HIGH);
motorOff = false;
motorBack = true;
motorForward = false;
Serial.print("Start pulling");
}
// on a touch with buttonC Stop (B11111101),
if((incoming == B11111101)&& motorBack && !motorForward && !motorOff){
digitalWrite(latchPin595, LOW);
shiftOut(dataPin595, clockPin595, LSBFIRST, pattern0); // motor start pulling
digitalWrite(latchPin595, HIGH);
motorOff = true;
motorForward = false;
motorBack = false;
Serial.print("Stop");
}
}
//**********************************************************
void joystickFunction ()
{
int y; // y the variable for the position of the jostick on the vertical axe
int x; // x the variable for the position of the jostick on the horizontal axe
x=analogRead(JoyStick_X);
y=analogRead(JoyStick_Y);
Serial.println(x);
Serial.println(y);
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(50);
// left and right joystick control
for (int i=0; i<7; i++){
if ((x > middleX + 100) and (incoming == leftStopArray [i])) // moving te joystick to the left
{
// Left direction <<<<<
digitalWrite(latchPin595, LOW);
shiftOut(dataPin595, clockPin595, LSBFIRST, pattern3);
digitalWrite(latchPin595, HIGH);
Serial.println("Left");
}
}
for (int i=0; i<7; i++){
if ((x < middleX - 100) and (incoming == rightStopArray [i])) // moving te joystick to the right
{
// Right direction >>>>>
digitalWrite(latchPin595, LOW);
shiftOut(dataPin595, clockPin595, LSBFIRST, pattern4);
digitalWrite(latchPin595, HIGH);
Serial.println("Right");
}
}
//else no action
if (x = middleX)
{
digitalWrite(latchPin595, LOW);
shiftOut(dataPin595, clockPin595, LSBFIRST, pattern0);
digitalWrite(latchPin595, HIGH);
Serial.println("Stop X");
}
// Up and down joystick control
for (int i=0; i<7; i++){
if ((y > middleY + 100) and (incoming == upStopArray [i])) // moving te joystick up
{
// Left direction <<<<<
digitalWrite(latchPin595, LOW);
shiftOut(dataPin595, clockPin595, LSBFIRST, pattern5);
digitalWrite(latchPin595, HIGH);
Serial.println("Up");
}
}
for (int i=0; i<7; i++){
if ((y < middleY - 100) and (incoming == downStopArray [i])) // moving te joystick down
{
// Right direction >>>>>
digitalWrite(latchPin595, LOW);
shiftOut(dataPin595, clockPin595, LSBFIRST, pattern6);
digitalWrite(latchPin595, HIGH);
Serial.println("Down");
}
}
//else no action
if (y = middleY)
{
digitalWrite(latchPin595, LOW);
shiftOut(dataPin595, clockPin595, LSBFIRST, pattern0);
digitalWrite(latchPin595, HIGH);
Serial.println("Stop Y");
}
}