# fixing the baseline zero position of a DC motor with encoder

Hello,

I am very new to arduino.

I am doing it through I2C pogrammig with arduino using SDA & SCL.

motor: rhino RMCS -2201
Encoder: 0.2deg resolution optical encoder integrated on motor output shaft
supports UART/I2C/PPM/Analog signals directly for absolute speed and absolute position control

with this code we are able to control the direction and speed, but not the position.

Please guide me on the following problem:

If we have to move the motor from 0-60 degree and if for some reason it is stopped at 30 degree…
so, whenever it will start for the next time will it take the 30 degree as zero degree (as it has no any memory and has a continuous 360 degree rotation ) and move til 90 degree considering it as 0-60 degree??

Is it possible that it first comes to zero degree (from wherever it has stopped the last time) and then start moving from there itself?

Is there some way to set the encoder at the very start of the code to fix the baseline??

``````#include <Wire.h>
#define Motor_1 0X08
signed int z;
int M_speed=255;
int in_deg=0;
int fn_deg=700;
void setup()
{
Wire.begin();
Serial.begin(9600);
}

void loop()
{

i2cSendSignedInt(Motor_1, M_speed, 0);			//set the maxspeed (0) to 100
delay(10);					        //give a 100ms delay to process the command
i2cSendSignedLong(Motor_1, in_deg, 3);			//set the encoder counter (3) to 0
delay(10);						//give a 100ms delay to process the command

Serial.println("Motor_1");                              // Serial print Motor_1
i2cSendSignedLong(Motor_1, fn_deg, 4);		                //set the absolute go to posuition (4) to 1800 counts

while (z <= 700)                                       // wait till Motor_1 reaches its position
{
delay(10);
Serial.println(z);
}
delay(2000);
i2cSendSignedInt(Motor_1, M_speed, 0);      //set the maxspeed (0) to 100
delay(10);                  //give a 100ms delay to process the command
i2cSendSignedLong(Motor_1, fn_deg, 3);     //set the encoder counter (3) to 0
delay(10);            //give a 100ms delay to process the command

Serial.println("Anti Clockwisse");                              // Serial print Motor_1
i2cSendSignedLong(Motor_1, in_deg, 4);                   //set the absolute go to posuition (4) to 1800 counts
while (z >= 0)                                       // wait till Motor_1 reaches its position
{
delay(10);
Serial.println(z);
}
delay(2000);
}

void i2cSendSignedInt(signed int SlaveAdd , signed int value, unsigned char command)
{
Wire.write(command);
Wire.write(value & 0xFF);
Wire.write(value >> 8);
Wire.endTransmission();
}

void i2cSendSignedLong(signed int SlaveAdd, signed int value, unsigned char command)
{
Wire.write(command);
Wire.write(value & 0xFF);
Wire.write(value >> 8);
Wire.write(0);
Wire.write(0);
Wire.endTransmission();
}