So I am currently working on getting a motor moving in all directions, at different speeds with a joystick.
This is what I currently have:
// Hardware
#define SPD_Y A1
#define START 5
#define CCW 4
#define CW 3
// Variables
int wheel_speed;
int SPD_Y_VAL;
void setup() {
pinMode(START, OUTPUT);
pinMode(CCW, OUTPUT);
pinMode(CW, OUTPUT);
pinMode(SPD_Y, INPUT);
Serial.begin(9600);
}
void loop() {
SPD_Y_VAL = analogRead(SPD_Y);
Serial.print(SPD_Y_VAL);
if (SPD_Y_VAL<511) {
wheel_speed=(511-SPD_Y_VAL)/2;
Serial.print(" ");
Serial.println(wheel_speed);
digitalWrite(CCW, HIGH);
digitalWrite(CW, LOW); // this is counterclockwise
analogWrite(START, wheel_speed);
}
if (SPD_Y_VAL>=511){
wheel_speed=(SPD_Y_VAL-512)/2;
Serial.print(" ");
Serial.println(wheel_speed);
digitalWrite(CCW, LOW);
digitalWrite(CW, HIGH); // this is clockwise
analogWrite(START, wheel_speed);
}
}
While I can move the joystick in the y perfectly fine, I am having trouble introducing x into the equation without the motor stopping or going in the wrong direction. I can cover forward and backward, and where the diagonals are, but as x approaches 0 or 1023 I am not sure what to do.
That code contains no attempt to do anything with a joystick X-axis.
You say you have tried to get it to work so post the attempt. Then at least we'll know where it is connected and we might get some hint of what you intended it to do. "getting a motor moving in all directions" isn't helpful. Motors only go round CW and round CCW so I don't know what other directions you're aiming for.
It didn't because at the time I couldn't figure out properly what to do. Now I have code that turns the motor in both the x & y directions, but not at the intended RPM that I want at each point. What I am trying to indicate is that the motor spinning CW and in increasing Y is forward, while the motor spinning CCW and in negative Y is reverse. I am then trying to indicate turns as well. I apologize for the late response, as I had class. Here is my code now:
// Hardware
#define SPD_Y A1
#define DIR_X A0
#define START 5
#define CCW 4
#define CW 3
// Variables
int wheel_speed_y = 0;
int wheel_dir_x = 0;
int SPD_Y_VAL = 511;
int DIR_X_VAL= 511;
void setup() {
pinMode(START, OUTPUT);
pinMode(CCW, OUTPUT);
pinMode(CW, OUTPUT);
pinMode(SPD_Y, INPUT);
pinMode(DIR_X, INPUT);
digitalWrite(START, LOW);
digitalWrite(CCW, LOW);
digitalWrite(CW, HIGH);
Serial.begin(9600);
}
void loop() {
SPD_Y_VAL = analogRead(SPD_Y);
Serial.print(SPD_Y_VAL);
DIR_X_VAL = analogRead(DIR_X);
Serial.print(DIR_X_VAL);
if (SPD_Y_VAL<511) {
wheel_speed_y=(511-SPD_Y_VAL)/2;
Serial.print(" ");
Serial.println(wheel_speed_y);
digitalWrite(CCW, HIGH);
digitalWrite(CW, LOW); // this is counterclockwise
analogWrite(START, wheel_speed_y);
}
else if (SPD_Y_VAL>=511){
wheel_speed_y=(SPD_Y_VAL-512)/2;
Serial.print(" ");
Serial.println(wheel_speed_y);
digitalWrite(CCW, LOW);
digitalWrite(CW, HIGH); // this is clockwise
analogWrite(START, wheel_speed_y);
}
else
{
digitalWrite(CCW, LOW);
digitalWrite(CW, LOW);
}
if (DIR_X_VAL<511) {
wheel_dir_x=(511-DIR_X_VAL)/2;
Serial.print(" ");
Serial.println(wheel_dir_x);
digitalWrite(CCW, HIGH);
digitalWrite(CW, LOW); // this is clockwise
analogWrite(START, wheel_dir_x);
}
else if (DIR_X_VAL>=511) {
wheel_dir_x = (DIR_X_VAL-511)/2;
Serial.print(" ");
Serial.println(wheel_dir_x);
digitalWrite(CCW, HIGH);
digitalWrite(CW, LOW); // this is clockwise
analogWrite(START, wheel_dir_x);
}
else
{
digitalWrite(CCW, LOW);
digitalWrite(CW, LOW);
}
}
There's not much useful information so far. We need to know a lot more about your project. I guessed that you have a robot/car/buggy of some sort. But it only seems to have one motor so I might be wrong?
So how does it turn with one motor and no servo to steer? How many wheels and what does it look like? Or is it something else entirely? More information needed.
The other thing you need to do is to explain exactly what happens with your current code and how that is different from what you want to happen.
Ok, so it is supposed to be simulating movement of a set of wheels, using only one. So it is only the one motor being driven. The idea is that as the joystick rotates, the speed of the fan revolving will change, as will its direction of rotation.
What is currently happening with the project is that the rotation will become slow at the wrong points or stop completely, and I am trying to not have that happen. I want the fan on the motor to rotate continuously as I rotate the joystick, except at the resting position, and at certain points the speed of the fan should change. For example, at x = 1023 & y = 511 (my joystick rest), the fan's rotation speed will be 255 (PWM output). However, when we shift left or right, I expect that fan rotation speed to slow down. Hopefully that gives you a bit more insight into what I am doing.
Ok, so what I am doing is trying to combine two joysticks into one, in essence. Usually, like in a video game, there are two control sticks - one for horizontal, one for vertical. I was going to apply that to this joystick and a motor, simulating movement like that in a video game. Forward and backward are binary, of course, but then you'd have to simulate the different movement too. So that's what I am trying to do, just output into motor control.
I'm not real clear as to what your project is, but if you need motors with forward, reverse, and speed control, you might consider using continuous rotation servos for the motors. Very simple to control them with pot based joysticks.