// Developed By Johnny Huang
// CAR - JOYSTICK (Two Joysticks)
// UTK 2019 - Nuestra Senora de la Reconcillicion
// INPUT PIN VARIABLES
int yPin = A0;
int xPin = A1;
// OUTPUT PIN VARIABLES
int motorPinLeft = 5;
int motorPinRight = 6;
// COMPUTED VARIABLES
int xPosition = 0;
int yPosition = 0;
int motorSpeedLeft = 0;
int motorSpeedRight = 0;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// Set Up Pins
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(motorPinLeft, OUTPUT);
pinMode(motorPinRight, OUTPUT);
}
void loop() {
// READ JOYSTICK POSITIONS
xPosition = analogRead(xPin);
yPosition = analogRead(yPin);
// PRINT JOYSTICK POSITIONS TO SERIAL MONITOR
Serial.print("X: ");
Serial.print(xPosition);
Serial.print(" | Y: ");
Serial.println(yPosition);
// COMPUTE MOTOR SPEEDS FROM 8 to 4 BITS
if(xPosition < 400)
{
//Serial.print("What the Heck");
motorSpeedRight = map(xPosition, 600, 1023, 0, 255);
}
else if(xPosition > 650)
{
motorSpeedLeft = map(xPosition, 480, 0, 0, 255);
}
else if(yPosition > 650 && xPosition < 650 && xPosition > 350){
motorSpeedRight = map(yPosition, 490, 1023, 0, 255);
motorSpeedLeft = map(yPosition, 490, 1023, 0, 255);
}
else{
motorSpeedRight = 0;
motorSpeedLeft = 0;
}
// PRINT MOTOR SPEED TO SERIAL MONITOR
Serial.print("LEFT SPEED: ");
Serial.print(motorSpeedLeft);
Serial.print(" | RIGHT SPEED: ");
Serial.println(motorSpeedRight);
// WRITE MOTOR SPEEDS
analogWrite(motorPinLeft, motorSpeedLeft);
analogWrite(motorPinRight, motorSpeedRight);
delay(300);
}