# control the diving of DC Geared Motor with sensor

Hello ,i am using DC Geared Motor with sensor and Planetary Gear (7 W-2.1 kg.cm). I am trying to programme arduino uno board to control the motion of the motor using L 298 driver. the motor sensor gives a pulse approximately every 1 degree . The sensor pulses must be counted as the motor rotates till reaching sector angle ,then the motion must be reversed by inverting the control signal of the driver. The motor speed is controlled by a potentiometer I tried to write the code attached withe the message but it doesn't reverse the direction of rotation. Note that i used the (external interrupt technique) i.e. every 1 pulse from the sensor to uno pin 2 , the counter will increase and to check if the motor reach the end of the secctor or not

``````/*driving motor in certain sector with varying speed for scanning*/
int pot=A4;      // potentiometer pin connected to pin A4
int speeed=0;    // the variable in which we store the value of he speed
int counter=0; //antenna assumed to start motion to left ((variable stores the input pulses from encoder))
int sector=130;  // rotation of 130 degree then reverse motion
int c1 =3;       //  c1 output to the drive to left
int c2 =5;       //  c2 output to the drive to right
int rl=1;        //  register rl represents motion in left direction
int rr=0;        //  register rr represents motion in right direction
int pulse=2;     // input pulse from decoder on pin 2
void setup()
{
pinMode(pulse,INPUT);         //input pulse from sensor
pinMode(pot,INPUT);
pinMode(c1,OUTPUT);
pinMode(c2,OUTPUT);
analogWrite(c1,LOW);                          //initializes the driving signals
analogWrite(c2,LOW);                          //initializes the driving signals
attachInterrupt(0,decoder,RISING);            // pin 2 is the input from sensor to make the interrupt to work
}
void loop()
{

if(rl==1&&rr==0)
{
speeed=analogRead(pot)/4;  // read the potentiometer value and store it in speeed register
analogWrite(c1,speeed);
analogWrite(c2,LOW);
}
if(rl==0&&rr==1)
{
speeed=analogRead(pot)/4;  // read the potentiometer value and store it in speeed register
analogWrite(c1,LOW);
analogWrite(c2,speeed);
}
}
void decoder()                        // the function that would be perfomed after the interrupt occurs
{
if(rl==1&&rr==0)
{
counter=++counter;
}
if(rl==0&&rr==1)
{
counter=--counter;
}
if(counter==sector)
{
rl==0;
rr==1;
}
if(counter==0)
{
rl==1;
rr==0;
}

}
``````

Firstly you should declare all the variables that are shared by an interrupt routine and the rest of the code as "volaltile" - this prevents the compiler applying certain optimizations that are incorrect in the presence of interrupts.

Secondly don't do`counter = ++counter ;` (its a tautology - the ++ and -- operators already assign a value), simply go

``````counter ++ ;
``````

Thirdly you test for the counter == sector in the wrong circumstance - you'd do better to replace rl and rr by a single state variable:

``````#define STATIONARY 0
#define ROTATE_LEFT 1
#define ROTATE_RIGHT 2
volatile int direction = STATIONARY ;
``````

``````  if (direction == ROTATE_LEFT)