I want to rotate a stepper motor with a joystick as you see it on the picture.
With the speed potentiometer (multiplied with the joystickvalue), you can set up the speed of the roation.
This works fine this this code:
#include <Arduino.h>
#include <AccelStepper.h>
// Define a stepper and the pins it will use
AccelStepper stepper (1, 7, 6);
int potPin_daemping = 3; // Damping Input Pin
int potPin_speed = 4; // Speed Input Pin
int joyLR = 5; // Joystick Input Pin
float wert_joyLR = 0;
float wert_speed = 0;
void setup()
{
stepper.setMaxSpeed(10000);
}
void loop()
{
wert_joyLR = joystick(joyLR);// from -512 to 512
wert_speed = potentiometer(potPin_speed); // from 0 to 1
stepper.setSpeed(wert_joyLR*wert_speed);
stepper.runSpeed();
}
float joystick(int joystickvalue)
{
float wert = (analogRead(joystickvalue));
if (wert<=517 && wert>=507) wert=0.000;
else wert = wert-512.000;// from -512 to 512
return wert;
}
float potentiometer(int potvalue)
{
float wert = (analogRead(potvalue));
wert = wert/1023;
return wert;
}
Now I also want to set a damping value with the second potentiometer.
If the joystick goes back to its home position fast, the motor should stop smooth.
And if the joystick goes to the max postiton fast, the motor also should run on smooth.
This damping should be increased or decreased with the "damping potentiometer".
I have no idea, how to implement this function in the actual code.
I'm not sure if this would suit you but it worked for me in another situation.
Assume you have a variable holding the current position
Assume another has a value representing the amount of change in position per interval of time.
Assume you have another variable reflecting the desired position as set by the joystick.
If there is a big gap between the current position and the desired position the change per interval would be large.
For the damping you could have another variable which holds the max permitted change per interval and if your code detects that the change demanded by joystick is is above the permitted max change it would use the max.
You could use the potentiometer to vary the value of maxChange
I have made a few changes in the code below. You were certainly pointed in the right direction.
This code probably won't actually work because the values that come from analogRead() are unlikely to be suitable and will need to be scaled appropriately.
Also, you don't seem to be concerned with the number of steps the motor makes.
As you are using the Accelstepper library, you could simply use the potentiometer to set the value for acceleration and leave the library to do the hard work. I was not thinking of that when I made my original suggestion - and I have not tried to incorporate that into this code.
#include <Arduino.h>
#include <AccelStepper.h>
AccelStepper stepper (1, 7, 6);
int currentpos;
int currentSpeed;
int changeamount;
int joystickSpeed;
int maxChange;
int i = 0;
int JoystickPin = 5;
int DampingPin = 3;
void setup() {
joystickpos = analogRead(JoystickPin);
currentpos = joystickpos;
}
void loop() {
joystickSpeed = analogRead(JoystickPin);
maxChange = analogRead(DampingPin);
spdChg = 1
if (joystickSpeed < currentSpeed){
spdChg = -1
}
changeamount = abs(joystickSpeed - currentSpeed);
if (changeamount > maxChange) {
changeamount = maxChange;
}
currentSpeed += (changeamount * spdChg);
stepper.setSpeed(currentSpeed);
stepper.runSpeed();
}
Hi! Thanks for your code, but I have problems to impement it on the right way.
The stepper motor do not slow down or speed up smooth.
If the joystick value goes from max ( eg. 500) to 0, the motor should slow down from its actual rotation speed smoothly to 0 (eg. in 2 seconds). With the damping potentiometer the time should be set.