Please read
slam 3likm w ra7mt allah w brkato;
Hi ;
Iam trying to drive dc motor has an encoder with constant speed in certain direction and when reach the end of the sector , the signal on 3,5 pins will be will reversed so the motor will reverse direction.the counter counts the input pulsese from the encoder,so every interrupt on pin 2 , the counter will be increased by 1 or decreased by 1 based on the direction.
When i see the output on an oscilloscope , the signal is right in half and wrong in the other this means that the error in the point where i reverse the direction
Please help thank you
this is the code:
/*driving dc motor in certain sector with varying speed for scanning*/
int on_off=A1; // switch pin
int leftled=A2; // led indicate to left motion
int rightled=A3; // led indicate to right motion
int potspeed=A4; // speed potentiometer PIN
int potsector=A5; // sector potentiometer PIN
int pulse=2; // input pulse from decoder
int c1 =3; // c1 output to drive to left
int c2 =5; // c2 output to drive to right
volatile int speeed=0; // variable carry the speed value
volatile int sector=0; // sector width
volatile byte(counter)=00000000; // antenna assumed to start motion to left
volatile int left_direction=1; // indicator represents motion to left direction
volatile int right_direction=0; // indicator represents motion to right direction
int s=1; // represents the switch state
int store=0;
void setup()
{
pinMode(c1,OUTPUT); // Driver contorl signal for left
pinMode(c2,OUTPUT); // Driver contorl signal for right
pinMode(pulse,INPUT); // input pulses from sensor
pinMode(on_off,INPUT); // switch pin
pinMode(potspeed,INPUT); // speed potentiometerr pin as an input
pinMode(potsector,INPUT); // sector potentiometer pin as an input
pinMode(leftled,OUTPUT); // left led as output
pinMode(rightled,OUTPUT); // right led as output
digitalWrite(leftled,LOW); // initializes the led signals
digitalWrite(rightled,LOW); // initializes the led signals
analogWrite(c1,LOW); //initializes the driving signals
analogWrite(c2,LOW); //initializes the driving signals
speeed=50;
sector=100;
attachInterrupt(0,decoder,RISING); // pin 2 is the input from sensor to make the interrupt to work
}
void loop()
{
attachInterrupt(0,decoder,RISING); // pin 2 is the input from sensor to make the interrupt to work
if(on_off==HIGH) // make S depends on the on_off pin state
{s==1;}
else{s==0;}
left:
attachInterrupt(0,decoder,RISING); // pin 2 is the input from sensor to make the interrupt to work
analogWrite(c1,speeed*s);
analogWrite(c2,LOW);
digitalWrite(leftled,HIGH);
digitalWrite(rightled,LOW);
if(left_direction==1 && right_direction==0)
{
if(counter >= sector)
{
analogWrite(c1,LOW);
analogWrite(c2,speeed*s);
left_direction==0;
right_direction==1*s;
digitalWrite(leftled,LOW);
digitalWrite(rightled,HIGH);
goto right;}
else {goto left;} // if angle does not reach the maximume
}
/*******************************************************************************/
right:
attachInterrupt(0,decoder,RISING); // pin 2 is the input from sensor to make the interrupt to work
analogWrite(c1,LOW);
analogWrite(c2,speeed*s);
digitalWrite(leftled,LOW);
digitalWrite(rightled,HIGH);
if(left_direction==0 && right_direction==1)
{
if(counter==0)
{
analogWrite(c1,speeed*s);
analogWrite(c2,LOW);
left_direction==1*s;
right_direction==0;
digitalWrite(leftled,HIGH);
digitalWrite(rightled,LOW);
}
else{goto right;} // if angle does not reach 0
}
}
//end of the main loop
/*********************************************************************************/
void decoder() // the function that would be perfomed after the interrupt occurs
{
speeed=50;
sector=100;
if(left_direction==1&&right_direction==0) // if motion to left
{
counter--;
} //if motion to right
else
{
counter++;
}
detachInterrupt(0);
}