int enA = 5;
int in1 = 8;
int in2 = 7;
int enB = 6;
int in3 = 4;
int in4 = 3;
int Ch1 = 11;
int xAxis = 9;
int yAxis = 10;
int motorA = 0;
int motorB = 0;
// Ch1>11
// Ch3>10
// Ch4>9
void setup() {
Serial.begin(9600);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void loop() {
int Ch1 = pulseIn(11,HIGH);
int xAxis = pulseIn(9,HIGH); // Read Joysticks X-axis
int yAxis = pulseIn(10,HIGH,25000); // Read Joysticks Y-axis
// Y-axis used for forward and backward control
if (yAxis < 1300) {
// Set Motor A backward
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B backward
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
motorA = map(yAxis, 1300, 1000, 0, 255);
motorB = map(yAxis, 1300, 1000, 0, 255);
}
else if (yAxis > 1700) {
// Set Motor A forward
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B forward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
motorA = map(yAxis, 1700, 2000, 0, 255);
motorB = map(yAxis, 1700, 2000, 0, 255);
}
// If joystick stays in middle the motors are not moving
else {
motorA = 0;
motorB = 0;
}
// X-axis used for left and right control
if (xAxis < 1400) {
// Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
int xMapped = map(xAxis, 1400, 1000, 0, 255);
// Move to left - decrease left motor speed, increase right motor speed
motorA = motorA - xMapped;
motorB = motorB + xMapped;
// Confine the range from 0 to 255
if (motorA < 0) {
motorA = 0;
}
if (motorB > 255) {
motorB = 255;
}
}
if (xAxis >= 1600) {
// Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
int xMapped = map(xAxis, 1600, 2000, 0, 255);
// Move right - decrease right motor speed, increase left motor speed
motorA = motorA + xMapped;
motorB = motorB - xMapped;
// Confine the range from 0 to 255
if (motorA > 255) {
motorA = 255;
}
if (motorB < 0) {
motorB = 0;
}
}
if (Ch1 >1600) {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B forward
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
motorA = map(Ch1, 1600, 2000, 0, 255);
motorB = map(Ch1, 1600, 2000, 0, 255);
}
else if (Ch1 <1400) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B forward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
motorA = map(Ch1, 1400, 1000, 0, 255);
motorB = map(Ch1, 1400, 1000, 0, 255);
}
if (xAxis,yAxis,Ch1 <900){
motorA = 0;
motorB = 0;
}
// Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
if (motorA < 70) {
motorA = 0;
}
if (motorB < 70) {
motorB = 0;
}
digitalWrite(enA, motorA); // Send PWM signal to motor A
digitalWrite(enB, motorB); // Send PWM signal to motor B
}
Comments
Please log in or sign up to comment.