int JoyStick_X = A1; //x
// const int relayPin1 = 8; // Relay 1 control pin
// const int relayPin2 = 7; // Relay 2 control pin
// const int stopLeftButton = 2; // Stop forward button pin
// const int stopRightButton = 3; // Stop reverse button pin
const int middleX = 520; // the center value of the joystickX
int JoyStick_Y = A2; //y
// const int relayPin3 = 10; // Relay 3 control pin
// const int relayPin4 = 9; // Relay 4 control pin
// const int stopUpButton = 4; // Stop upward button pin
// const int stopDownButton = 13; // Stop downward button pin
const int middleY = 520; // the center value of the joystickY
const int dataPin595 = 5; /* DS */
const int clockPin595 = 6; /* SHCP */
const int latchPin595 = 7; /* STCP */
//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 */
// stopLeftButton = D3 165 0B11110111
// stopRightButton = D4 165 0B11101111
// stopUpButton = D5 165 0B11011111
// stopDownButton = D6 165 0B10111111
int pattern0 = 0b00000000;
int pattern3 = 0b00100000;
int pattern4 = 0b00010000;
int pattern5 = 0b00001000;
int pattern6 = 0b00000100;
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};
void setup()
{
pinMode(JoyStick_X, INPUT_PULLUP);
// pinMode(relayPin1, OUTPUT);
// pinMode(relayPin2, OUTPUT);
// pinMode(stopLeftButton, INPUT_PULLUP);
// pinMode(stopRightButton, INPUT_PULLUP);
Serial.begin(9600);
pinMode(JoyStick_Y, INPUT_PULLUP);
// pinMode(relayPin3, OUTPUT);
// pinMode(relayPin4, OUTPUT);
// pinMode(stopUpButton, INPUT_PULLUP);
// pinMode(stopDownButton, INPUT_PULLUP);
pinMode(dataPin595, OUTPUT);
pinMode(clockPin595, OUTPUT);
pinMode(latchPin595, OUTPUT);
pinMode(dataPin165, INPUT);
pinMode(clockPin165, OUTPUT);
pinMode(latchPin165, OUTPUT);
pinMode(clockEnablePin165, OUTPUT);
}
void loop()
{
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(300);
// 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");
}
}
// y=analogRead(JoyStick_Y);
// Serial.println(y);
// // if ((y > middleY + 100) and (digitalRead(stopUpButton)== LOW))
// if ((y > middleY + 100) and (incoming == B11111111))
// {
// // 👆 Up direction
// // digitalWrite(relayPin3, HIGH);
// // digitalWrite(relayPin4, LOW);
// // digitalWrite(latchPin, LOW);
// digitalWrite(latchPin595, LOW);
// shiftOut(dataPin595, clockPin595, LSBFIRST, pattern5);
// digitalWrite(latchPin595, HIGH);
// Serial.println("Up");
// }
// // else if ((y < middleY - 100) and (digitalRead(stopDownButton) == LOW))
// else if ((y < middleY - 100) and (incoming == B11111111))
// {
// //👇 Down direction
// // digitalWrite(relayPin3, LOW);
// digitalWrite(relayPin4, HIGH);
// digitalWrite(latchPin595, LOW);
// shiftOut(dataPin595, clockPin595, LSBFIRST, pattern6);
// digitalWrite(latchPin595, HIGH);
// Serial.println("Down");
// }
// else no action
// else {
// digitalWrite(relayPin3, LOW);
// digitalWrite(relayPin4, LOW);
// // delay(250);
// }
// digitalWrite(latchPin, LOW);
// shiftOut(dataPin, clockPin, LSBFIRST, pattern2);
// digitalWrite(latchPin, HIGH);
// Serial.println("backward");