int enA = 9; int enB = 10; int in1 = 8; int in2 = 7; int in3 = 6; int in4 = 5; int joystickX = A0; int joystickY = A1; int currentSpeed = 80; void setup() { pinMode(enA, OUTPUT); pinMode(enB, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); } void loop() { int xValue = analogRead(joystickX); int yValue = analogRead(joystickY);
// Map joystick Y value to motor speed int mappedYSpeed = map(yValue, 0, 1023, -currentSpeed, currentSpeed);
// Map joystick X value to motor speed int mappedXSpeed = map(xValue, 0, 1023, -currentSpeed, currentSpeed); // Calculate individual motor speeds based on joystick position int motorSpeedA = mappedYSpeed; int motorSpeedB = mappedYSpeed; // Adjust individual motor speeds based on joystick X position if (xValue < 400) { motorSpeedA = 0; // Stop motor A motorSpeedB += mappedXSpeed; // Motor B rotates faster } else if (xValue > 600) { motorSpeedB = 0; // Stop motor B motorSpeedA -= mappedXSpeed; // Motor A rotates faster } // Set motor speed analogWrite(enA, abs(motorSpeedA)); analogWrite(enB, abs(motorSpeedB)); // Set motor direction based on speed sign digitalWrite(in1, motorSpeedA > 0 ? HIGH : LOW); digitalWrite(in2, motorSpeedA > 0 ? LOW : HIGH); digitalWrite(in3, motorSpeedB > 0 ? HIGH : LOW); digitalWrite(in4, motorSpeedB > 0 ? LOW : HIGH); }
int enA = 9;
int enB = 10;
int in1 = 8;
int in2 = 7;
int in3 = 6;
int in4 = 5;
int joystickX = A0;
int joystickY = A1;
int currentSpeed = 80;
void setup() {
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void loop() {
int xValue = analogRead(joystickX);
int yValue = analogRead(joystickY);
// Map joystick Y value to motor speed
int mappedYSpeed = map(yValue, 0, 1023, -currentSpeed, currentSpeed);
// Map joystick X value to motor speed
int mappedXSpeed = map(xValue, 0, 1023, -currentSpeed, currentSpeed);
// Calculate individual motor speeds based on joystick position
int motorSpeedA = mappedYSpeed;
int motorSpeedB = mappedYSpeed;
// Adjust individual motor speeds based on joystick X position
if (xValue < 400) {
motorSpeedA = 0; // Stop motor A
motorSpeedB += mappedXSpeed; // Motor B rotates faster
} else if (xValue > 600) {
motorSpeedB = 0; // Stop motor B
motorSpeedA -= mappedXSpeed; // Motor A rotates faster
}
// Set motor speed
analogWrite(enA, abs(motorSpeedA));
analogWrite(enB, abs(motorSpeedB));
// Set motor direction based on speed sign
digitalWrite(in1, motorSpeedA > 0 ? HIGH : LOW);
digitalWrite(in2, motorSpeedA > 0 ? LOW : HIGH);
digitalWrite(in3, motorSpeedB > 0 ? HIGH : LOW);
digitalWrite(in4, motorSpeedB > 0 ? LOW : HIGH);
}