Hi I am looking for a way to control a DC and a servo motor on a joystick. So the dc motor wood control the speed... so up and down on my joystick and the servo motor would control the direction so... left and right. Is there any code or easy way of doing this I can't figure it out since I'm just getting started.
That can be done. What is the DC motor, type, voltage, curent? The same for the stepper.
Is the motor supposed to run both forwards and backwards? If so You need a H-bridge for the DC motor.
For the stepper You would do fine with s stepper driver board.
Use the magnifier symbol up to the right and search for Arduin + DC motor. Search for Arduino + stepper.
I suggest you break your learning into two parts with two separate short programs - one to use the fore and aft motion to control the motor and another using the left right motion to control a servo.
...R
Robin2:
I suggest you break your learning into two parts with two separate short programs - one to use the fore and aft motion to control the motor and another using the left right motion to control a servo....R
I have already but I need t find a way to include the servo code with the motor code in the same code.
This is the code for the motor... I am using a L293D with a joystick.
#define motorfwd 3
#define motorbwd 4
#define enable 5
int motorspeed = 0;
int xspeed = 0;
void setup() {
pinMode (motorfwd, OUTPUT);
pinMode (motorbwd, OUTPUT);
pinMode (enable, OUTPUT);
digitalWrite (enable, LOW);
Serial.begin (9600);
}
void loop() {
int joy_x = analogRead(A0); // put your main code here, to run repeatedly:
Serial.print("X-axis: ");
Serial.print(joy_x);
Serial.print("/n/n");
if (joy_x < 450) {
digitalWrite(motorfwd, HIGH);
digitalWrite(motorbwd, LOW);
motorspeed = map(joy_x, 450, 0, 0, 255);
}
else if (joy_x > 550){
digitalWrite(motorfwd, LOW);
digitalWrite(motorbwd, HIGH);
motorspeed= map(joy_x, 550, 1023, 0, 255);
}
else(motorspeed= 0);
if (motorspeed < 70)
motorspeed = 0;
analogWrite (enable, motorspeed);
delay(200);
}
Railroader:
That can be done. What is the DC motor, type, voltage, curent? The same for the stepper.
Is the motor supposed to run both forwards and backwards? If so You need a H-bridge for the DC motor.
For the stepper You would do fine with s stepper driver board.
Use the magnifier symbol up to the right and search for Arduin + DC motor. Search for Arduino + stepper.
I am using a L293D and a joystick to control the motor up and down. I am using X on my joystick to do that. Now I need a way to add my SG92R servo to the code so that it can control left and right (Y) on my joystick.
This is the code I have for the motor up and down I just need to include the servo code for left and right...
#define motorfwd 3
#define motorbwd 4
#define enable 5
int motorspeed = 0;
int xspeed = 0;
void setup() {
pinMode (motorfwd, OUTPUT);
pinMode (motorbwd, OUTPUT);
pinMode (enable, OUTPUT);
digitalWrite (enable, LOW);
Serial.begin (9600);
}
void loop() {
int joy_x = analogRead(A0); // put your main code here, to run repeatedly:
Serial.print("X-axis: ");
Serial.print(joy_x);
Serial.print("/n/n");
if (joy_x < 450) {
digitalWrite(motorfwd, HIGH);
digitalWrite(motorbwd, LOW);
motorspeed = map(joy_x, 450, 0, 0, 255);
}
else if (joy_x > 550){
digitalWrite(motorfwd, LOW);
digitalWrite(motorbwd, HIGH);
motorspeed= map(joy_x, 550, 1023, 0, 255);
}
else(motorspeed= 0);
if (motorspeed < 70)
motorspeed = 0;
analogWrite (enable, motorspeed);
delay(200);
}
Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.
Continued cross posting could result in a time out from the forum.
Could you take a few moments to Learn How To Use The Forum.
It will help you get the best out of the forum in the future.
Other general help and troubleshooting advice can be found here.
What is working soo faar? DC motor? Servo?
You said you already had separate programs, one for the motor and another for the servo. You seem to have posted the motor code twice but I can't see any attempt to control a servo.
Searching for "Arduino servo joystick" will show you that loads of people have done that part before and published their code for you to learn from (or just plain re-use). Give it a try.
Steve