# Find a tip to inform the position to the stepper motor

**URL:** https://discourse.processing.org/t/find-a-tip-to-inform-the-position-to-the-stepper-motor/17644
**Category:** Electronics (Arduino, etc.)
**Created:** [February 8, 2020, 5:05pm UTC](https://discourse.processing.org/t/find-a-tip-to-inform-the-position-to-the-stepper-motor/17644 "2020-02-08T17:05:30Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [February 8, 2020, 10:18pm UTC](https://discourse.processing.org/t/find-a-tip-to-inform-the-position-to-the-stepper-motor/17644/2 "2020-02-08T22:18:30Z")

</div>

Hello,

You have a related post here already:

> [@Position / rotation conversion](https://discourse.processing.org/t/position-rotation-conversion/17610):
>
> Hello, I run motors in Processing with position data from 0 to 4096 for a revolution. I have a stepper motor in Arduino which follows its positions very well. grinning The problem is when the simulated motor arrives at its starting point, the program sends the data 0, then 2048 when it arrives at the middle of the second rotation. While to properly inform my engine in the Arduino, it would be necessary that at the start of the second lap, I send the data 4096 + 1 when it starts the second …

I have worked with stepper motors.

I provided a minimal example of an approach I took to this:

- Processing is sending integer “steps” and direction (this can be positive or negative) to the Arduino
- “Position” can be translated to steps and direction; you will have to do this correctly relative to the current initial and final position. I did not do this in this example.
- The Processing “simulation” should follow the expected motion of the stepper motor from the instructions you are sending.
- The motor must complete its motion (code is blocking on Arduino side) so that must also be given consideration. This is not in this code example.

I did this simulation (rotating line) using:

- A integer counter for steps.
- Drawing a line that was rotated by steps\*angle/step
- Using modulo operator for the zero-crossing (0/360/720 deg… etc.) to determine the rotation and plot the line.  
Examples:  
4096%4096 = 0  
(4096 + 1024)%4096 = 1024  
And so on…

I left this uncommented and removed variable names so you can work through it:

```auto
float a;
int b, c;

void setup() 
  {
  size(400, 400, P2D);
  b = 32;
  }

void draw() 
  {
  background(64); 
  translate(width/2, height/2);    
  
  float d = TWO_PI/b;   
  a = (c%b)*d;
  rotate(a);
  strokeWeight(3);
  stroke(255, 0, 0);
  line(0, 0, 150, 0);
  
  println(a, degrees(a));
  }
  
void mousePressed()
  {
  if (mouseButton == LEFT) c++;
  if (mouseButton == RIGHT) c--;
  }

```

I used the modulo operator because floating point errors accumulated if I rotated the motor a few hundred thousand times; this kept the rotations to between 0 and 2π.

References:

> **[% (modulo) / Reference](https://processing.org/reference/modulo.html)**
>
> Calculates the remainder when one number is divided by another. For example, when 52.1 is divided by 10, the divisor (10) goes into the dividend (52.1) five times (5 \* 10 == 50), and there is a remain…

’ 🙂 ’

---

_[View the full topic](https://discourse.processing.org/t/find-a-tip-to-inform-the-position-to-the-stepper-motor/17644)._
