Hi this is my first post on the forum so please excuse any mistakes!
I am making a scale model Tiger 1 Tank using 3D printed parts, some motor/servos. Arduino mega, PS2 remote, and other parts. (I will include photos and the code for this project so far and the parts I am using later in the post) I plan for the PS2 remote to have one joystick control the driving and one the movement of the turret. I have been using the bill porter ps2x library fro this project, and so far I have the PS2 programmed to make tank noises and control a servo that will control the up and down motion of the main gun.
At the moment I have come across an issue using a stepper motor with the PS2 remote. Whenever I push the joystick left or right the stepper motor only spins in one direction regardless(it always spins right when I am pushing the joystick left or right). I have tried doing research to adapt what others have done but I am having trouble finding much. I was wondering if anyone had any experience doing anything like this or sees that I am missing anything, so any help would be greatly appreciated!!
PARTS (Parts marked with "/" were used in this post)
/Arduino mega 2560
/3rd party PS2 remote with wireless adapter
/Step motor 28BY-48 with ULN2003 stepper motor driver board
/SG90 Servo motor
-RC tiger chassis purchased from amazon (with built in DC motors and gearbox)
-L298N driver Board (for DC motors in chassis)
-Catalex Serial MP3 Player
-Small aux speaker
CODE
#include <Servo.h> //For driving the ESCs and Servos
#include <Stepper.h>
#include <PS2X_lib.h> // Bill Porter's PS2 Library
PS2X ps2x; //The PS2 Controller Class
Servo GunServo; //Create servo object representing SteeringServo
int PlyStnRStickUpDn = 0; //Value read off the PS2 Right Stick up/down.
int PlyStnRStickLtRt = 0; //Value read off the PS2 Right Stick left/right
int PlyStnLStickUpDn = 0; //Value read off the PS2 Left Stick up/down
int PlyStnLStickLtRt = 0; // Value read off the PS2 Left Stick left/right
int GunServoSetting = 90; //Setting for the Steering Servo
int pos = 0;
int stickX = 128;
const int PPR = 200; // initialize steps per revolution for stepper motors
int speed = 0; // initialize stepper speed, default to 0 speed
int direction = 1; // initialize stepper direction, default to clockwise?
const int coarse = 1000; // initialize coarse speed range for motor speed mapping
Stepper stepperX(PPR, 8, 9, 10, 11); // initialize X-Stepper pins
int senXRangeMax = coarse; // initialize the X-Axis sensitivity range max speed, default to coarse
void setup()
{
ps2x.config_gamepad(17, 15, 16, 14, false, false);
//setup pins and settings: GamePad(clock, command, attention, data, Pressures, Rumble)
GunServo.write(pos);
GunServo.attach(A8);// attaches the Steering Servo to pin 7
}
void loop()
{
ps2x.read_gamepad(); //This needs to be called at least once a second
// to get data from the controller.
//Analogue Stick readings
PlyStnRStickUpDn = ps2x.Analog(PSS_RY); //Right Stick Up and Down
PlyStnRStickLtRt = ps2x.Analog(PSS_RX); //Right Stick Left and Right
PlyStnLStickUpDn = ps2x.Analog(PSS_LY); //left Stick Up and Down
PlyStnLStickLtRt = ps2x.Analog(PSS_LX); //Left Stick Left and Right
//Readings from PS2 Controller Sticks are from 0 to 255
//with the neutral being 128. The zero positions are to
//the left for X-axis movements and up for Y-axis movements.
//Variables to carry the settings for the ESCs and Servos
//The values from the PS2 Sticks are mapped to 0 to 180 degrees
GunServoSetting = map(PlyStnRStickUpDn, 256, -256, 0, 179);
GunServo.write(GunServoSetting);
stickX = ps2x.Analog(PSS_RX); // set the analog value of the right stick X-Axis equal (0-255)
int position = stickX; // set the desired stepper position equal to the X-Axis value (0-255)
speed = 0; // reset speed
direction = 1;
if (position > 130)
{ // if the X-Axis stick position is positive
speed = map(position, 130, 255, 0, senXRangeMax); // map speed based on sensitivity setting
direction = 1; // direction is positive
}
if (position < 126)
{ // if the X-Axis stick position is negative
speed = map(position, 0, 126, senXRangeMax, 0); // map speed based on sensitivity setting
direction = -1; // direction is negative
}
if (speed > 0)
{ // if the X-Axis joystick is not in the center dead zone, move stepper motor
stepperX.setSpeed(speed); // set X-Axis stepper speed
stepperX.step(direction); // go set number of steps in specified direction
}
delay(5);
}
If you have any questions do not be afraid to ask!