I recently purchased an Arduino Uno R3 board with a Arduino Motor Shield Rev3 as well as a joystick breakout board. My aim is to use the thumb joystick to control the direction of the stepper motor on the X-axis, left to right as well as the speed. So far I've been looking for good tutorials on the net with regards to the subject but had no luck so far.

Attached to this article are two photos showing how I connected my joystick to the motor shield board as well as a closeup view of the specific joystick I bought. Here is a link to the joystick as well:

Below is some sample code I downloaded from the net which rotates the shaft from one direction to the other with a delay. How can I modify this code to work with my setup and control the motor via the joystick:

could you give more info on how exactly you one to control the motor with the joystick.what i mean is what does pushing the joystick to the right do to the motor and what know, please explain so we could help you more

Well, when the joystick is in the center and not moving, the motor shouldn't do anything and stop. When the joystick is pressed to the left or right, the motor must move in the respective direction and the further the joystick moves from the center, the faster the speed of the motor must be. So basically left and right movement via the joystick as well as speed adjusting as the joystick moves further from the center pin.

The purpose of this setup is to slide a camera left and right, faster and slower on a rail of plus minus 1.5m in length. Attached to the stepper motor's shaft will be a pulley and on the other side just a normal shaft enclosed in a bearing unit also containing a pulley. I'll then use timing belt to slide the camera left and right.

Here are my stepper motor specs:

2 Phase Hybrid (Model: 23HS6430)3ABipolar4 Wires1.8 degree step angle

I'm really not very clued up with prototyping and therefore if you can help me to amend the code to work with my setup, I'll really be happy since I've been battling and struggling to get the project going. But looking at my connection setup, is everything connected to the right pins etc?

well what you need to do is to divide the value of a pot into halfso i would say that 507 until 517 is a good middle value.then take the value between 517 until 1023 and map it with the min and max of the stepper speed.after that take between 0 to 507 value of pot n map it with the min n max of the stepper speed.third is that determine which side of the pot make the stepper move clockwise. what i give here should give you an idea on how to do it

In theory I know what you're saying and it makes sense to me. However, how can I practically amend this code and incorporate everything that you have said? I'm not a code guru and this is all new to me.

this part is the part which dictate how much step the stepper would take to complete a rotation.i would recommend to you to put the total number of step it take for your set up form its start position on the far left to the far right or vice versa. How ever this is not something that is really must needed and you could actually make it variable.... maybe by making use of the other axis of your joystick?

this is the part that call for the speed of how fast the stepper rotate. if my understanding of your project is correct, this part need to be implemented in the loop function and now the setup function cause it need to be variable. However before you impliment any of the changes that i suggest, i would recommend that you play with the value 75 that is in the code with something that is higher up until the point your motor stall. This would mean, that the value you got there is the max speed that the stepper could go. one more thing is would also suggest that you do this with the full load of your setup. this is to make sure that you get the most accurate value for your setup and not just something that is calculated or theoretical.

ok now for the pseudo code ( only the loop part but i thing you would get the idea well)

void loop(){ PVal=analogRead(Pot1); if (PVal >= 507 || PVal <= 517) { // fill in with the one you need to make it brake } if (PVal > 517 ) { int K= map (Pval, 518,1023,0,MAX); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/ stepper.setSpeed(K); this set the speed of the stepper motor stepper.step(spr); this set the direction and the number of step it would take } if (PVal < 507 ) { int K= map (Pval, 0,506,0,MAX); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/ stepper.setSpeed(K); //this set the speed of the stepper motor stepper.step(-spr); //this set the direction and the number of step it would take }

this should help you with your project anyway keep us posted and if you be kind enough to send us a link to a you tube showing how it work would be great .

Btw i would also like to suggest to you that, you pun 2 home sensor, this could be a simple limit switch or the type that use light sensor to detect your carriage on both end of the travel. this would help if let say your carriage at it end but you keep giving it the signal to go forward. although for a stepper this will not cause lots of trouble since if the motor will stall if it cant move the carriage but it might or could shorten the life of the motor... so putting the sensor will save it n give the motor a much longer life and reliability

void loop(){ int PVal=analogRead(Pot1); if (PVal >= 507 || PVal <= 517) { // fill in with the one you need to make it brake } if (PVal > 517 ) { int K= map (PVal, 518,1023,0,75); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/ stepper.setSpeed(K); //this set the speed of the stepper motor stepper.step(spr); //this set the direction and the number of step it would take } if (PVal < 507 ) { int K= map (PVal, 0,506,0,75); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/ stepper.setSpeed(K); //this set the speed of the stepper motor stepper.step(-spr); //this set the direction and the number of step it would take }}

Nothing seems to be happening...

Sorry, I know that I come forth as someone with no brains for this stuff, but I'm no guru. I will gladly post a video if I can get it working.

} if (PVal > 517 ) { int K= map (PVal, 518,1023,0,75); // max is the value that you get from your experiment with the max speed of the stepper before stalling stepper.setSpeed(K); //this set the speed of the stepper motor stepper.step(spr); //this set the direction and the number of step it would take } if (PVal < 507 ) { int K= map (PVal, 0,506,0,75); // max is the value that you get from your experiment with the max speed of the stepper before stalling stepper.setSpeed(K); //this set the speed of the stepper motor stepper.step(-spr); //this set the direction and the number of step it would take }}

For some reason nothing is happening.

But thanks anyway for your help. I'm sure I'll manage somehow somewhere eventually while searching the net. I need a solution quite urgent and can't struggle like this for too long.

void loop(){ int PVal=analogRead(Pot1); if (PVal >= 507 || PVal <= 517) { // fill in with the one you need to make it brake } if (PVal > 517 ) { int K= map (PVal, 518,1023,0,400); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/ int S=map (K, 0,400,0,num_step); stepper.setSpeed(K); //this set the speed of the stepper motor stepper.step(S); //this set the direction and the number of step it would take Serial.print("Forward, K="); Serial.print(K); Serial.print(" PVAL="); Serial.println(PVal); } if (PVal < 507 ) { int K= map (PVal, 0,506,400,0); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/ int S=map (K, 0,400,0,num_step); stepper.setSpeed(K); //this set the speed of the stepper motor stepper.step(-S); //this set the direction and the number of step it would take Serial.print("Backward, K="); Serial.print(K); Serial.print(" PVAL="); Serial.println(PVal); }

// This next bit just outputs the pot position to a servo val = map(PVal, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180) myservo.write(val); // sets the servo position according to the scaled value delay(15); }