Here is what I'm trying to achieve, basically I want to move objects up & down at a long distance.
So, I'm using an Arduino Uno with a Stepper Motor and communicate through OSC.
For now, it's working OK forward.
I send OSC values between 0 to 10.24 and the stepper motor goes from 0 to 2048 steps.
It even works with value from 0 to 20.48 for 2 full rotations for example.
But, I'm struggling for days with the Backward movement.
The stepper motor interprets each additional step in the backward direction as a forward movement, which is the expected behaviour I guess. (values sent from 10.24 to 0 is interpreted as a new full rotation forward)
Here is the OSC track I'm trying to achieve :
2 full rotations forward and then 2 full rotations backward.
For now, this track is interpreted as 4 full rotations forward.
How could I indicate for example that after a full cycle up to 2048 steps, a step to 2047 is a backward step not an additional step forward ?
Could anyone point me in the right direction ? I'm completely out of my depth there...
The stepper motor receives integral values (val1) from 0 to "whatever positive int", and a full rotation moves from 0 to 2048. I don't know yet how many full rotations I will use, but it already seems to work ok with multiples rotations forward (int 4096 and up), as, I guess, the OSC track is constantly broadcasting the current position the stepper motor should be at.
My problem, I guess, is to code "when val1 is < the current position, speed has to be negative" & the opposite "when val1 is > the current position, speed has to be positive"
And I failed miserably at that, because speed can't be <0 for one, and secondly the stepper motor doesn't know what its current position is...
Hi,
Can I suggest you forget about OSC etc and write some code JUST to control the stepper.
Get it working in both directions then integrate it into your OSC code.
Look at the examples for AccelStepper library in the IDE to see how to change direction.
Hi Tom,
Yes, I totally agree, I went back to the "bounce example" which works ok.
But I can't figure out how to code something like that :
"when val1 is < the current position, speed has to be negative" & the opposite "when val1 is > the current position, speed has to be positive"
Sorry Robin, I'm stupid.
I wasn't checking the "bounce code" with the same pin configuration... (the switch between 7&8 I forgot about)
Sorry about that.
Anyway, now I've managed to obtain the expected behaviour, but I had to modify the second ">" otherwise it stops after the first movement.
I've checked with "Serial.println(stepper1.currentPosition());" this code sends the right values.
So, this works : 160 forward, 80 backward, 160 forward.
#include <AccelStepper.h>
AccelStepper stepper1(4,6,8,7,9);
void setup(){
Serial.begin(9600);
stepper1.setMaxSpeed(300.0);
stepper1.setAcceleration(20.0);
stepper1.moveTo(160);
Serial.println("Moving forward to 160");
while (stepper1.distanceToGo() > 0) {
stepper1.run();
}
delay(1000);
stepper1.moveTo(80);
Serial.println("Moving back to 80");
while (stepper1.distanceToGo() < 0) {
stepper1.run();
}
delay(1000);
stepper1.moveTo(160);
Serial.println("Moving forward to 160");
while (stepper1.distanceToGo() > 0) {
stepper1.run();
}
}
void loop(){
}
Without "<0", it doesn't print any values after the first movement:
159
159
159
160
Moving back to 80
Moving forward to 160
What do you think would be next ? [edit] I’m stupid again, that should work fine now with the OSC, as values were correct. (I’m on a train without an RJ45 cable...) I’ll let you know tomorrow..
Arduino Uno Pin 6 to 9 connected to Stepper Driver Pin 1 to 4
Power Supply is USB port for now.
I could provide a circuit diagram if you think there's still something fishy there. (beyond my previous pin misconfiguration )
amy_bl:
, but I had to modify the second ">" otherwise it stops after the first movement.
I acknowledged that I had that mistake.
I've checked with "Serial.println(stepper1.currentPosition());" this code sends the right values.
So, this works : 160 forward, 80 backward, 160 forward.
Does that mean everything is now working properly?
Yes, I believe so.
I have to wait until tomorrow to check with the OSC part.
I hope it is just my pin misconfiguration... :’(
Thanks a lot, I’ll let you know