Track Vehicle Controller Tx to Rx
Code and STL file for remote controlled Arduino track vehicle and track links.
Components and supplies
1
Arduino Mega 2560
1
Jumper wires (generic)
1
SparkFun Full-Bridge Motor Driver Breakout - L298N
Project description
Code
Code for remote controlled track vehicle
arduino
1int enA = 5; 2int in1 = 8; 3int in2 = 7; 4int enB = 6; 5int in3 = 4; 6int in4 = 3; 7int Ch1 = 11; 8int xAxis = 9; 9int yAxis = 10; 10 11int motorA = 0; 12int motorB = 0; 13// Ch1>11 14// Ch3>10 15// Ch4>9 16void setup() { 17 Serial.begin(9600); 18 pinMode(enA, OUTPUT); 19 pinMode(enB, OUTPUT); 20 pinMode(in1, OUTPUT); 21 pinMode(in2, OUTPUT); 22 pinMode(in3, OUTPUT); 23 pinMode(in4, OUTPUT); 24 25} 26void loop() { 27 int Ch1 = pulseIn(11,HIGH); 28 int xAxis = pulseIn(9,HIGH); // Read Joysticks X-axis 29 int yAxis = pulseIn(10,HIGH,25000); // Read Joysticks Y-axis 30 31// Y-axis used for forward and backward control 32 if (yAxis < 1300) { 33 // Set Motor A backward 34 digitalWrite(in1, HIGH); 35 digitalWrite(in2, LOW); 36 // Set Motor B backward 37 digitalWrite(in3, HIGH); 38 digitalWrite(in4, LOW); 39 // 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 40 motorA = map(yAxis, 1300, 1000, 0, 255); 41 motorB = map(yAxis, 1300, 1000, 0, 255); 42 } 43 else if (yAxis > 1700) { 44 // Set Motor A forward 45 digitalWrite(in1, LOW); 46 digitalWrite(in2, HIGH); 47 // Set Motor B forward 48 digitalWrite(in3, LOW); 49 digitalWrite(in4, HIGH); 50 // 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 51 motorA = map(yAxis, 1700, 2000, 0, 255); 52 motorB = map(yAxis, 1700, 2000, 0, 255); 53 } 54 // If joystick stays in middle the motors are not moving 55 else { 56 motorA = 0; 57 motorB = 0; 58 } 59 // X-axis used for left and right control 60 if (xAxis < 1400) { 61 // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value 62 int xMapped = map(xAxis, 1400, 1000, 0, 255); 63 // Move to left - decrease left motor speed, increase right motor speed 64 motorA = motorA - xMapped; 65 motorB = motorB + xMapped; 66 // Confine the range from 0 to 255 67 if (motorA < 0) { 68 motorA = 0; 69 } 70 if (motorB > 255) { 71 motorB = 255; 72 } 73 } 74 if (xAxis >= 1600) { 75 // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value 76 int xMapped = map(xAxis, 1600, 2000, 0, 255); 77 // Move right - decrease right motor speed, increase left motor speed 78 motorA = motorA + xMapped; 79 motorB = motorB - xMapped; 80 // Confine the range from 0 to 255 81 if (motorA > 255) { 82 motorA = 255; 83 } 84 if (motorB < 0) { 85 motorB = 0; 86 } 87 } 88 if (Ch1 >1600) { 89 digitalWrite(in1, LOW); 90 digitalWrite(in2, HIGH); 91 // Set Motor B forward 92 digitalWrite(in3, HIGH); 93 digitalWrite(in4, LOW); 94 // 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 95 motorA = map(Ch1, 1600, 2000, 0, 255); 96 motorB = map(Ch1, 1600, 2000, 0, 255); 97 } 98 else if (Ch1 <1400) { 99 digitalWrite(in1, HIGH); 100 digitalWrite(in2, LOW); 101 // Set Motor B forward 102 digitalWrite(in3, LOW); 103 digitalWrite(in4, HIGH); 104 // 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 105 motorA = map(Ch1, 1400, 1000, 0, 255); 106 motorB = map(Ch1, 1400, 1000, 0, 255); 107 } 108 if (xAxis,yAxis,Ch1 <900){ 109 motorA = 0; 110 motorB = 0; 111 } 112 // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70) 113 if (motorA < 70) { 114 motorA = 0; 115 } 116 if (motorB < 70) { 117 motorB = 0; 118 } 119 digitalWrite(enA, motorA); // Send PWM signal to motor A 120 digitalWrite(enB, motorB); // Send PWM signal to motor B 121 122 123}
Code for remote controlled track vehicle
arduino
1int enA = 5; 2int in1 = 8; 3int in2 = 7; 4int enB = 6; 5int in3 = 4; 6int in4 = 3; 7int Ch1 = 11; 8int xAxis = 9; 9int yAxis = 10; 10 11int motorA = 0; 12int motorB = 0; 13// Ch1>11 14// Ch3>10 15// Ch4>9 16void setup() { 17 Serial.begin(9600); 18 pinMode(enA, OUTPUT); 19 pinMode(enB, OUTPUT); 20 pinMode(in1, OUTPUT); 21 pinMode(in2, OUTPUT); 22 pinMode(in3, OUTPUT); 23 pinMode(in4, OUTPUT); 24 25} 26void loop() { 27 int Ch1 = pulseIn(11,HIGH); 28 int xAxis = pulseIn(9,HIGH); // Read Joysticks X-axis 29 int yAxis = pulseIn(10,HIGH,25000); // Read Joysticks Y-axis 30 31// Y-axis used for forward and backward control 32 if (yAxis < 1300) { 33 // Set Motor A backward 34 digitalWrite(in1, HIGH); 35 digitalWrite(in2, LOW); 36 // Set Motor B backward 37 digitalWrite(in3, HIGH); 38 digitalWrite(in4, LOW); 39 // 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 40 motorA = map(yAxis, 1300, 1000, 0, 255); 41 motorB = map(yAxis, 1300, 1000, 0, 255); 42 } 43 else if (yAxis > 1700) { 44 // Set Motor A forward 45 digitalWrite(in1, LOW); 46 digitalWrite(in2, HIGH); 47 // Set Motor B forward 48 digitalWrite(in3, LOW); 49 digitalWrite(in4, HIGH); 50 // 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 51 motorA = map(yAxis, 1700, 2000, 0, 255); 52 motorB = map(yAxis, 1700, 2000, 0, 255); 53 } 54 // If joystick stays in middle the motors are not moving 55 else { 56 motorA = 0; 57 motorB = 0; 58 } 59 // X-axis used for left and right control 60 if (xAxis < 1400) { 61 // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value 62 int xMapped = map(xAxis, 1400, 1000, 0, 255); 63 // Move to left - decrease left motor speed, increase right motor speed 64 motorA = motorA - xMapped; 65 motorB = motorB + xMapped; 66 // Confine the range from 0 to 255 67 if (motorA < 0) { 68 motorA = 0; 69 } 70 if (motorB > 255) { 71 motorB = 255; 72 } 73 } 74 if (xAxis >= 1600) { 75 // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value 76 int xMapped = map(xAxis, 1600, 2000, 0, 255); 77 // Move right - decrease right motor speed, increase left motor speed 78 motorA = motorA + xMapped; 79 motorB = motorB - xMapped; 80 // Confine the range from 0 to 255 81 if (motorA > 255) { 82 motorA = 255; 83 } 84 if (motorB < 0) { 85 motorB = 0; 86 } 87 } 88 if (Ch1 >1600) { 89 digitalWrite(in1, LOW); 90 digitalWrite(in2, HIGH); 91 // Set Motor B forward 92 digitalWrite(in3, HIGH); 93 digitalWrite(in4, LOW); 94 // 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 95 motorA = map(Ch1, 1600, 2000, 0, 255); 96 motorB = map(Ch1, 1600, 2000, 0, 255); 97 } 98 else if (Ch1 <1400) { 99 digitalWrite(in1, HIGH); 100 digitalWrite(in2, LOW); 101 // Set Motor B forward 102 digitalWrite(in3, LOW); 103 digitalWrite(in4, HIGH); 104 // 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 105 motorA = map(Ch1, 1400, 1000, 0, 255); 106 motorB = map(Ch1, 1400, 1000, 0, 255); 107 } 108 if (xAxis,yAxis,Ch1 <900){ 109 motorA = 0; 110 motorB = 0; 111 } 112 // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70) 113 if (motorA < 70) { 114 motorA = 0; 115 } 116 if (motorB < 70) { 117 motorB = 0; 118 } 119 digitalWrite(enA, motorA); // Send PWM signal to motor A 120 digitalWrite(enB, motorB); // Send PWM signal to motor B 121 122 123}
Downloadable files
Wiring
All wiring info is in the code
Wiring
Wiring
All wiring info is in the code
Wiring
Documentation
Track links
STL file for 3D printing your own track links.
Track links
Comments
Only logged in users can leave comments