Components and supplies
SparkFun Full-Bridge Motor Driver Breakout - L298N
Arduino UNO
Project description
Code
RC_LASER_Tank.ino
arduino
Control all your systems
1/* This code is for RC LASER tank / Self propelled LASER Turret 2 * Refer to www.SurtrTech.com or SurtrTech YouTube channel for more details 3 */ 4 5#include <PS2X_lib.h> //Libraries needed PS2 Receiver and Servo 6#include <Servo.h> 7 8Servo Servo1; //Declaring cradle servos, Servo1 is the base 9Servo Servo2; 10 11PS2X ps2x; //Declaring the PS2X controller 12 13int error = 0; //PS2X stuff to detect errors and type of controller 14byte type = 0; 15byte vibrate = 0; //vibration of the controller 16 17int a,b; 18 19//Motor A Right motor for my project 20int in1 = 2; 21int in2 = 4; 22int EnA = 3; //I'm controlling the speed also 23 24//Motor B 25int in3 = 6; 26int in4 = 7; 27int EnB = 5; 28 29void setup(){ 30 31 Serial.begin(9600); //Serial monitor will help you to see values and know if your controls are detected or not 32 error = ps2x.config_gamepad(13,11,8,12, true, true); //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error 33 type = ps2x.readType(); 34 pinMode(in1, OUTPUT); 35 pinMode(in2, OUTPUT); 36 pinMode(EnA, OUTPUT); 37 pinMode(in1, OUTPUT); 38 pinMode(in2, OUTPUT); 39 pinMode(EnB, OUTPUT); 40 pinMode(A0,OUTPUT); //Those lines concern the LASER, I ran out of pins and I put A0 as GND 41 pinMode(A1,OUTPUT); //And the A1 provides 5v if I press the X button 42 digitalWrite(A0,LOW); 43 Servo1.attach(9); 44 Servo2.attach(10); 45 46} 47 48void loop(){ 49 a=Servo1.read(); //Reading the previous servos positions is an important step so we can know where they should position next 50 b=Servo2.read(); 51 52 ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed 53 vibrate = ps2x.Analog(PSAB_BLUE); //this will set the large motor vibrate speed based on 54 digitalWrite(A1,vibrate); //if the vibrate takes a value > threshold it will set the A1 (LASER) to high 55 Stop(); //As long as no stick is pushed we stop, otherwise move accordingly 56 57 //Forward sequence 58 if(255-ps2x.Analog(PSS_LY) > 130){ //This is how I detect that the stick is pushed up the value gotten should be 130-255 59 Serial.println("Forward"); //And the 255-... before is just to arrange values because it was inverted 60 Forward(); 61 } 62 63 //Backward sequence 64 if(255-ps2x.Analog(PSS_LY) < 120){ 65 Serial.println("Backward"); 66 Backward(); 67 } 68 69 //Right sequence 70 if(ps2x.Analog(PSS_LX) > 130){ //X Axis wasn't inverted that's why you don't see the 255-... 71 Serial.println("Right"); 72 Right(); 73 } 74 75 //Left sequence 76 if(ps2x.Analog(PSS_LX) < 120){ 77 Serial.println("Left"); 78 Left(); 79 } 80 81 //Cradle Right 82 if(255-ps2x.Analog(PSS_RX) < 120){ //If the stick is pointed to the right 83 Serial.println("C Right"); //We read the current servo position (a=Servo1.read()) 84 Serial.println(ps2x.Analog(PSS_RX)); //And we decrement '1' 85 a=a-1; //Then we write the new position which is a-1 you can change the '1' to higher to make it turn fast 86 Servo1.write(a); 87 88 //Cradle Left 89 } 90 if(255-ps2x.Analog(PSS_RX) > 130){ 91 Serial.println("C Left"); 92 Serial.println(ps2x.Analog(PSS_RX)); 93 a=a+1; 94 Servo1.write(a); 95 } 96 97 //Cradle Up 98 if(255-ps2x.Analog(PSS_RY) < 120){ 99 Serial.println("C Up"); 100 Serial.println(ps2x.Analog(PSS_RY)); 101 b=b+1; 102 Servo2.write(b); 103 104 //Cradle Down 105 } 106 if(255-ps2x.Analog(PSS_RY) > 130){ 107 Serial.println("C Down"); 108 Serial.println(ps2x.Analog(PSS_RY)); 109 b=b-1; 110 Servo2.write(b); 111 } 112 113 delay(50); 114} 115 116void Forward(){ 117 int F1 = map(255-ps2x.Analog(PSS_LY), 130, 255, 0, 255); //Changing the signal received from 130-255 to 0-255 to be able to write that value as PWM signal 118 digitalWrite(in1, LOW); //To control the speed 119 digitalWrite(in2, HIGH); 120 analogWrite(EnA,F1); 121 digitalWrite(in3, HIGH); 122 digitalWrite(in4, LOW); 123 analogWrite(EnB,F1); 124 Serial.println(F1); 125 126 127} 128 129 130void Backward(){ 131 int F2 = map(255-ps2x.Analog(PSS_LY), 120, 0, 0, 255); 132 digitalWrite(in1, HIGH); 133 digitalWrite(in2, LOW); 134 analogWrite(EnA,F2); 135 digitalWrite(in3, LOW); 136 digitalWrite(in4, HIGH); 137 analogWrite(EnB,F2); 138 Serial.println(F2); 139} 140 141 142void Right(){ 143 int F3 = map(255-ps2x.Analog(PSS_LX), 120, 0, 0, 255); 144 digitalWrite(in1, LOW); //Turning sequence that suited me I stop on track and turn the other 145 digitalWrite(in2, LOW); //You can turn them on different sides to make it turn fast 146 analogWrite(EnA,F3); 147 digitalWrite(in3, HIGH); 148 digitalWrite(in4, LOW); 149 analogWrite(EnB,F3); 150 Serial.println(F3); 151 152} 153 154 155void Left(){ 156 int F4 = map(255-ps2x.Analog(PSS_LX), 130, 255, 0, 255); 157 digitalWrite(in1, LOW); 158 digitalWrite(in2, HIGH); 159 analogWrite(EnA,F4); 160 digitalWrite(in3, LOW); 161 digitalWrite(in4, LOW); 162 analogWrite(EnB,0); 163 Serial.println(F4); 164 165} 166 167void Stop(){ 168 digitalWrite(in1, LOW); 169 digitalWrite(in2, LOW); 170 analogWrite(EnA,0); 171 digitalWrite(in3, LOW); 172 digitalWrite(in4, LOW); 173 analogWrite(EnB,0); 174 175} 176
RC_LASER_Tank.ino
arduino
Control all your systems
1/* This code is for RC LASER tank / Self propelled LASER Turret 2 * Refer to www.SurtrTech.com or SurtrTech YouTube channel for more details 3 */ 4 5#include <PS2X_lib.h> //Libraries needed PS2 Receiver and Servo 6#include <Servo.h> 7 8Servo Servo1; //Declaring cradle servos, Servo1 is the base 9Servo Servo2; 10 11PS2X ps2x; //Declaring the PS2X controller 12 13int error = 0; //PS2X stuff to detect errors and type of controller 14byte type = 0; 15byte vibrate = 0; //vibration of the controller 16 17int a,b; 18 19//Motor A Right motor for my project 20int in1 = 2; 21int in2 = 4; 22int EnA = 3; //I'm controlling the speed also 23 24//Motor B 25int in3 = 6; 26int in4 = 7; 27int EnB = 5; 28 29void setup(){ 30 31 Serial.begin(9600); //Serial monitor will help you to see values and know if your controls are detected or not 32 error = ps2x.config_gamepad(13,11,8,12, true, true); //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error 33 type = ps2x.readType(); 34 pinMode(in1, OUTPUT); 35 pinMode(in2, OUTPUT); 36 pinMode(EnA, OUTPUT); 37 pinMode(in1, OUTPUT); 38 pinMode(in2, OUTPUT); 39 pinMode(EnB, OUTPUT); 40 pinMode(A0,OUTPUT); //Those lines concern the LASER, I ran out of pins and I put A0 as GND 41 pinMode(A1,OUTPUT); //And the A1 provides 5v if I press the X button 42 digitalWrite(A0,LOW); 43 Servo1.attach(9); 44 Servo2.attach(10); 45 46} 47 48void loop(){ 49 a=Servo1.read(); //Reading the previous servos positions is an important step so we can know where they should position next 50 b=Servo2.read(); 51 52 ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed 53 vibrate = ps2x.Analog(PSAB_BLUE); //this will set the large motor vibrate speed based on 54 digitalWrite(A1,vibrate); //if the vibrate takes a value > threshold it will set the A1 (LASER) to high 55 Stop(); //As long as no stick is pushed we stop, otherwise move accordingly 56 57 //Forward sequence 58 if(255-ps2x.Analog(PSS_LY) > 130){ //This is how I detect that the stick is pushed up the value gotten should be 130-255 59 Serial.println("Forward"); //And the 255-... before is just to arrange values because it was inverted 60 Forward(); 61 } 62 63 //Backward sequence 64 if(255-ps2x.Analog(PSS_LY) < 120){ 65 Serial.println("Backward"); 66 Backward(); 67 } 68 69 //Right sequence 70 if(ps2x.Analog(PSS_LX) > 130){ //X Axis wasn't inverted that's why you don't see the 255-... 71 Serial.println("Right"); 72 Right(); 73 } 74 75 //Left sequence 76 if(ps2x.Analog(PSS_LX) < 120){ 77 Serial.println("Left"); 78 Left(); 79 } 80 81 //Cradle Right 82 if(255-ps2x.Analog(PSS_RX) < 120){ //If the stick is pointed to the right 83 Serial.println("C Right"); //We read the current servo position (a=Servo1.read()) 84 Serial.println(ps2x.Analog(PSS_RX)); //And we decrement '1' 85 a=a-1; //Then we write the new position which is a-1 you can change the '1' to higher to make it turn fast 86 Servo1.write(a); 87 88 //Cradle Left 89 } 90 if(255-ps2x.Analog(PSS_RX) > 130){ 91 Serial.println("C Left"); 92 Serial.println(ps2x.Analog(PSS_RX)); 93 a=a+1; 94 Servo1.write(a); 95 } 96 97 //Cradle Up 98 if(255-ps2x.Analog(PSS_RY) < 120){ 99 Serial.println("C Up"); 100 Serial.println(ps2x.Analog(PSS_RY)); 101 b=b+1; 102 Servo2.write(b); 103 104 //Cradle Down 105 } 106 if(255-ps2x.Analog(PSS_RY) > 130){ 107 Serial.println("C Down"); 108 Serial.println(ps2x.Analog(PSS_RY)); 109 b=b-1; 110 Servo2.write(b); 111 } 112 113 delay(50); 114} 115 116void Forward(){ 117 int F1 = map(255-ps2x.Analog(PSS_LY), 130, 255, 0, 255); //Changing the signal received from 130-255 to 0-255 to be able to write that value as PWM signal 118 digitalWrite(in1, LOW); //To control the speed 119 digitalWrite(in2, HIGH); 120 analogWrite(EnA,F1); 121 digitalWrite(in3, HIGH); 122 digitalWrite(in4, LOW); 123 analogWrite(EnB,F1); 124 Serial.println(F1); 125 126 127} 128 129 130void Backward(){ 131 int F2 = map(255-ps2x.Analog(PSS_LY), 120, 0, 0, 255); 132 digitalWrite(in1, HIGH); 133 digitalWrite(in2, LOW); 134 analogWrite(EnA,F2); 135 digitalWrite(in3, LOW); 136 digitalWrite(in4, HIGH); 137 analogWrite(EnB,F2); 138 Serial.println(F2); 139} 140 141 142void Right(){ 143 int F3 = map(255-ps2x.Analog(PSS_LX), 120, 0, 0, 255); 144 digitalWrite(in1, LOW); //Turning sequence that suited me I stop on track and turn the other 145 digitalWrite(in2, LOW); //You can turn them on different sides to make it turn fast 146 analogWrite(EnA,F3); 147 digitalWrite(in3, HIGH); 148 digitalWrite(in4, LOW); 149 analogWrite(EnB,F3); 150 Serial.println(F3); 151 152} 153 154 155void Left(){ 156 int F4 = map(255-ps2x.Analog(PSS_LX), 130, 255, 0, 255); 157 digitalWrite(in1, LOW); 158 digitalWrite(in2, HIGH); 159 analogWrite(EnA,F4); 160 digitalWrite(in3, LOW); 161 digitalWrite(in4, LOW); 162 analogWrite(EnB,0); 163 Serial.println(F4); 164 165} 166 167void Stop(){ 168 digitalWrite(in1, LOW); 169 digitalWrite(in2, LOW); 170 analogWrite(EnA,0); 171 digitalWrite(in3, LOW); 172 digitalWrite(in4, LOW); 173 analogWrite(EnB,0); 174 175} 176
PS2X Library
Library for your PS2 wireless controller
Downloadable files
Wiring for all things
Don't forget that the l298n is powerd by 7.4V Li battery through +12V/Gnd and provides 5V for the rest of the system including Arduino
Wiring for all things
Wiring for all things
Don't forget that the l298n is powerd by 7.4V Li battery through +12V/Gnd and provides 5V for the rest of the system including Arduino
Wiring for all things
Comments
Only logged in users can leave comments
SurtrTech
0 Followers
•0 Projects
Table of contents
Intro
19
0