Hi there everyone!!!
I use an Arduino Mega to do these 2 things
1.Driving a DC 24V Motor via a Power-Mosfet H-bridge and controling speed and direction via analogWrite [PWM]&digitalWrite.
2.Feedback the motor's position via a Wheel Disk and photointerrupter,reading the photointerrupter's signal RISING as a attachInterrupt ISR.
the 24VDC H-bridge is like this
ABCD pins inside the above image , is process by two logic IC ,XOR&AND ,
to use only two pins to control the motor(Forward/Backward/Stop/Brake).
the photointerrupter encoder and wheel disk are like this
the encoder signal connect to pin 20 (intterupt 4 in MEGA)
void setup(){
Serial.begin(9600);
attachInterrupt(4, change_encoder, RISING);
}
change_encoder() is the ISR function to accumulate the encoder's value
volatile int encoder,encoder_last;
void change_encoder(){
encoder++;
}
Then I print out the encoder's value using serial port,
and control the motor using analogWrite&digitalWrite
void loop(){
if(encoder!=encoder_last) //print only when encoder change
Serial<<encoder<<endl;
encolder_last=encoder;
analogWrite(4,50);
digitalWrite(5,LOW);
}
pin4&5 are the control pins of H-bridge.
if i change the '1''s code from digitalWrite(4/5,HIGH) to analogWrite(4,5,0-255)
it will be the PWM speed control of Motor
pin4 pin5
1 0 Forward
0 1 Backward
0 0 Stop
1 1 Brake
NOW here comes the problem!!!
when I use digitalWrite with both motor control pins (motor full speed)
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
the encoder works well like this(serial monitor)
1->2->3->4->5->6.........
everytime encoder moves one step , encoder acc one
but when i use analogWrite with motor contorl pins (motor PWM speed)
analogWrite(4,50);
digitalWrite(5,LOW)
1->4->7->8->9->10->11->12->14->16......
everytime encoder moves one step, encoder acc unstable
maybe 1 jump to 11 12 jump to 16 everystep
I dont know if it is the electronic problem or programming problem.
hope some one can help!!