Function arguments

Clear all

# Function arguments

79 Posts
8 Users
12 Likes
767 Views
(@will)
Famed Member
Joined: 2 years ago
Posts: 2218

I don't know if it's related but in the segment of loop () ...

```  while (x == revCount) {
//Change direction at the limits
if (stepper1.distanceToGo() == 0) {
revCount++;
stepper1.setCurrentPosition(0);
stepper2.setCurrentPosition(0);
stepper1.moveTo(-stepper1.currentPosition());
stepper2.moveTo(-stepper2.currentPosition());
stepper1.setCurrentPosition(0);
stepper2.setCurrentPosition(0);
}

```

First, you set both stepper's current position to zero and then you reset each stepper to the new target of negative of its current position (that is, negative zero which is what you just set as the new current position) and then you set the current position back to its current value of zero.

I suspect that you may want to get rid of the first stepperX.setCurrentPosition(0), set the new target equal to the negative of the current position and only then set the current stepper positions to zero.

Experience is what you get when you don't get what you want.

(@will)
Famed Member
Joined: 2 years ago
Posts: 2218

Understood, the purpose of the run() command in the loop is to let each stepper take a step and then is supposed to let the other do so as well.

So, each pass through the while statement will allow both steppers to take 1 step and so they should be seen to move in unison.

I don't understand why that's not happening, it worries me 🙂

Experience is what you get when you don't get what you want.

(@barrie)
Estimable Member
Joined: 5 months ago
Posts: 86
Topic starter

@zander I have tried many combinations of the + and - signs. The negative MoveTo statement sends the stepper CCW. But AccelStepper info says negative speed commands are used for reverse directions. Yes I have tried countless combinations of + and - . The one combo I missed is sure to be the correct one.

(@zander)
Famed Member
Joined: 2 years ago
Posts: 3915

@barrie You don't get it. Goodbye, if I could unsubscribe I would.

Arduino says and I agree, in general, the const keyword is preferred for defining constants and should be used instead of #define
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon
My experience hours are >75,000 and I stopped counting in 2004.
Major Languages - 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting

(@barrie)
Estimable Member
Joined: 5 months ago
Posts: 86
Topic starter

@will

setCurrentPosition(0) is important. Until yoyo, the current position is anywhere. yoyo takes its reference from current position. I tried it without setCurrentPosition(0) and that is when I got peculiar results. 🙄

(@barrie)
Estimable Member
Joined: 5 months ago
Posts: 86
Topic starter

@zander  Hmmm! Which "get it" don't I get? I don't get it. 🤔

(@will)
Famed Member
Joined: 2 years ago
Posts: 2218

I think you're missing my point. I'm saying that you're setting the current position to zero and then immediately setting the new target location (via moveTo) to be the current position which you just set to zero.

In other words, setting the current position to zero is causing both stepper1 and stepper2 to be given the new target location of zero.

So, all you need to do is just remove the FIRST stepper1.setCurrentPosition(0); and the FIRST stepper2.setCurrentPosition(0);

Then leave stepper1.moveto(-stepper1.currentPosition()) and stepper2.moveto(-stepper2.currentPosition())

And THEN do the stepper1.setCurrentPosition(0); and stepper2.setCurrentPosition(0);

Experience is what you get when you don't get what you want.

(@barrie)
Estimable Member
Joined: 5 months ago
Posts: 86
Topic starter

That is an interesting thought. Here is what I think I am setting up. I am setting the current position to zero and telling the stepper to move to a target that is i/8 of a revolution from zero. Stepper1 does exactly that and stepper2 does the same in the opposite direction........ as expected. Unfortunately stepper1 moves first. stepper2 moves after stepper1 stops. The problem is that the steppers don't move together. In retrospect this is the expected behaviour because runToPosition() is a blocker. I previously thought it would only block the main loop. I will try playing with ".run" again.......... on the morrow.

Thanks for helping me figure this out.

(@will)
Famed Member
Joined: 2 years ago
Posts: 2218

Apparently, you didn't notice that I was no longer talking about the yoyo module, having said my piece about it already.

The last comments I made are regarding the code that follows your incomplete switch statement inside the main loop() and not the yoyo module.

Experience is what you get when you don't get what you want.

(@robotbuilder)
Noble Member
Joined: 4 years ago
Posts: 1647

Posted by: @barrie

@robotbuilder Yes they do run simultaneously. I used similar code. If you see the video you will see the problem.

I will have another look at the video later today.  I just started reading this thread so the problem is not clear to me at this stage.  I have two little stepper motors I can check the code with except I see the spirals share a common center like the big and little hands of a clock so I will just run two spirals separately.  By then you will probably have figured it out yourself anyway 🙂

(@barrie)
Estimable Member
Joined: 5 months ago
Posts: 86
Topic starter

@robotbuilder Thank you. If you look at the action in the video you will see the problem. It happens when the yellow LED lights up. You will see one stepper rotates left then stops. (That is how it should respond.) After it stops the other stepper rotates to the right and stops. That is also the correct motion. I want both steppers to move at the same time so the rotors move towards each other then apart from each other (like a yoyo). The reason they don't do it together is because the runToPosition() blocks all other code until it stops. (This is all happening in the yoyo function.)  I tried replacing runToPosition() with .run but it didn't work. The actions in the video suggest that the code outside yoyo is OK in principle but I'm sure it could be improved.

I will have another go tomorrow.

Noble Member
Joined: 4 years ago
Posts: 1506

@will

Posted by: @will

@barrie

``` for (byte gpio = Rled; gpio == Wled; gpio++) {
pinMode(gpio, OUTPUT);
}
```

Did you mean gpio<=Wled ?

Yes, thanks for picking that up.

(@robotbuilder)
Noble Member
Joined: 4 years ago
Posts: 1647

I see you are using pins 14,15,16,17,18 for your LEDs. What Arduino are you using?

(@barrie)
Estimable Member
Joined: 5 months ago
Posts: 86
Topic starter

Nano

(@barrie)
Estimable Member
Joined: 5 months ago
Posts: 86
Topic starter

@robotbuilder

@will  Your .run loop didn't work because moveTo recalculates speed. The solution is to set the speed after moveTo..... and it works!

```void yoyo(int segment) {
long target = FullStepsPerRev / segment;
stepper1.setCurrentPosition(0);
stepper2.setCurrentPosition(0);
stepper1.moveTo(target);
stepper2.moveTo(-target);
stepper1.setSpeed(100);
stepper2.setSpeed(100);
while (stepper1.distanceToGo()>0) {
stepper1.run();
stepper2.run();
}```
This post was modified 4 months ago by barrie

Page 5 / 6