To do the speed thing on another motor, just double up the code and adjust the pins and variables.
Something like this: untested, ymmv
int joy1 = 0;
int motor = 3;
int joy2 = 1;
int motor2 = 5; // pin 4 isn't pwm so use 5
void setup(){
pinMode(motor,OUTPUT);
pinMode(joy1,INPUT);
pinMode(motor2,OUTPUT);
pinMode(joy2,INPUT);
}
void loop(){
int ppp;
int ppp2;
ppp = analogRead(joy1);
ppp = map(ppp, 0, 1023, -255, 255);
analogWrite(motor,ppp);
ppp2 = analogRead(joy2);
ppp2 = map(ppp2, 0, 1023, -255, 255);
analogWrite(motor,ppp2);
}
For direction, you will need to give details about the connections to the motor. Direction control needs an h-bridge to reverse the polarity on the motor. Can you post a circuit diagram?
The code seems to use analogWrite with negative values. If you read the arduino reference you will see that you can only write values form 0 to 255 http://arduino.cc/es/Reference/AnalogWrite
JimboZA i've tested the code ,but its not working to me. i decided to use the l293d motor driver for speed and direction control.I'm
using a tip41c to drive the motor.here is my current conections:motor joystick_bb
roter45 can you post images of your robot?do you made this? Sorry if i don't write wright, i am from Brazil. Thanks!
Looks to me that you have the motor on the wrong side of the transistor. With an npn, the load should be above the collector ie between + and collector, with the emitter to ground. You have the collector to +ve and the load below the emitter.
Have a look here. You also need a diode across the motor as shown in that link. Also probably not wise to supply the motor's power from the Arduino.
Also, worth checking that the power rails on your breadboard go all the way from one end to the other: on some makes there is a break in the middle.
EDIT...
The code seems to use analogWrite with negative values
Well spotted, which makes for a bit of complication since OP presumably wants the joysticks centred to be 0 speed and then left to tuen one way, right the other. Need to think about the mapping for that....
MORE edit... something along the lines of if the stick reads 0 (full left), thats still a 255 PWM on the 293's enable pin and so is 1023, full right. Stick reading of 512 must be pwm of 0? But if the stick is <512 then set the 293's inputs on one channel to be hi/lo and >512, lo/high, so as to reverse the direction between the left and right side of the stick.
Can you understand Spanish ? Here you have a good tutorial, with this type of controller and with a code. Google translator it's a good way too.
Then any question that you might have ask here.
Well spotted, which makes for a bit of complication since OP presumably wants the joysticks centred to be 0 speed and then left to tuen one way, right the other. Need to think about the mapping for that....
The reason of using 700 and 400 it's for my experience. In real live the middle isn't 512. This way you have enough sensitive and not to much sensitive
Well spotted, which makes for a bit of complication since OP presumably wants the joysticks centred to be 0 speed and then left to tuen one way, right the other. Need to think about the mapping for that....
MORE edit... something along the lines of if the stick reads 0 (full left), thats still a 255 PWM on the 293's enable pin and so is 1023, full right. Stick reading of 512 must be pwm of 0? But if the stick is <512 then set the 293's inputs on one channel to be hi/lo and >512, lo/high, so as to reverse the direction between the left and right side of the stick.
Yes, i need the joystick center to be 0 .But how i change direction when the joystick is full right or full left?
Somenthing like this:
May this work? when the stick is in one side , an signal goes to l293's to change the motor direction , in that case is more easy to map the sitck value.
what is more dificult,make the codes or making the mechanical parts and connections?
Here it's the final code for one motor
Controlling two motors will be a bit different.
I recommed you to not start using the motor at 512. Because the motor will not be full stop never. The middle in joysticks it's not 512. Depending the alienation I have some with 456 the center and some with 598 the center . My limits are the result of hours trying different limits.
// 2,3,4 are the pins that controls the motor
// In some motor drivers I1 =2 ; I2=4; EA(Enablepin) = 3
sensorRead = analogRead(0);
if(sensorRead > 700){
speed = map(sensorRead,512,1023,0,255
digitalWrite(2,HIGH);
digitalWrite(4,LOW));
analogWrite(3,speed);}
if(sensorRead < 400){
speed = map(sensorRead,0,512,255,0);
digitalWrite(4,HIGH);
digitalWrite(2,LOW));
analogWrite(3,speed);}
I will test the code, but first i have to buy the motor driver because i don't have one.It will take near a week, because i'm buying from internet. When i get the driver and test the code, i'll post the results. Thanks for your colaboration.
I alredy get the l293d and test the code, it really works.I needed to make some adjustments for the center of the joystick to be 0,but now it works perfectly.The middle of my joystick is 502,i tested with the analogread example in arduino IDE.now i need the code for two motors,i tried to double the code but it not works,what do I do now?
I can post a video.
thanks for the code, but i think i not explain what i'm doing with the joystick thing.
my robot is much like this one in the video , however i want only to drill the PCB.I want to control it manually too,this is the joystick service.
does your code work for this purpose?
In this case, duplicated the code but changing the variables.
i duplicated the code, changed the variables and it working now. But one motor is not working properly, its not going backward,i think the problem is on the driver output. I'll make some tests and when my robot is working, i will post a video.
Thanks for the help.