Hi everyone!!
I am using this motor with encoder which gives 8400 counts per revolution of the output shaft of the motor.
I have written simple program to stop the motor after one revolution of the shaft. Below is the code:
#define encoder0PinA 2
#define encoder0PinB 3
int EnablePin = 8;
int PWMPin1 = 11;
int PWMPin2 = 10;
volatile signed int encoder0Pos = 0;
void setup()
{
pinMode(encoder0PinA, INPUT);
pinMode(encoder0PinB, INPUT);
pinMode(EnablePin, OUTPUT);
pinMode(PWMPin1, OUTPUT);
pinMode(PWMPin2, OUTPUT);
attachInterrupt(0, doEncoderA, CHANGE);
attachInterrupt(1, doEncoderB, CHANGE);
Serial.begin(115200);
}
void rotateCCW()
{
digitalWrite(EnablePin, HIGH);
analogWrite(PWMPin2,0);
analogWrite(PWMPin1,50);
delay(20);
}
void rotateCW()
{
digitalWrite(EnablePin, HIGH);
analogWrite(PWMPin2,10);
analogWrite(PWMPin1,0);
delay(20);
}
void motorSTOP()
{
digitalWrite(EnablePin, LOW);
delay(20);
}
void doEncoderA(){
// look for a low-to-high on channel A
if (digitalRead(encoder0PinA) == HIGH) {
// check channel B to see which way encoder is turning
if (digitalRead(encoder0PinB) == LOW) {
encoder0Pos++; // CW
}
else {
encoder0Pos--; // CCW
}
}
else // must be a high-to-low edge on channel A
{
// check channel B to see which way encoder is turning
if (digitalRead(encoder0PinB) == HIGH) {
encoder0Pos++; // CW
}
else {
encoder0Pos--; // CCW
}
}
}
void doEncoderB()
{
// look for a low-to-high on channel B
if (digitalRead(encoder0PinB) == HIGH) {
// check channel A to see which way encoder is turning
if (digitalRead(encoder0PinA) == HIGH) {
encoder0Pos++; // CW
}
else {
encoder0Pos--;; // CCW
}
}
// Look for a high-to-low on channel B
else {
// check channel B to see which way encoder is turning
if (digitalRead(encoder0PinA) == LOW) {
encoder0Pos++; // CW
}
else {
encoder0Pos--; // CCW
}
}
}
void loop()
{
if(encoder0Pos<=8400)
{
rotateCCW();
doEncoderA();
doEncoderB();
Serial.println(encoder0Pos);
}
else
{
motorSTOP();
}
}
After running the code motor does stop after one revolution. The problem is with the encoder reading. Below is the reading I received:
9
34
67
104
144
184
225
266
307
349
391
432
474
516
558
600
642
684
726
767
810
852
895
937
980
1022
1064
1106
1151
1193
1236
1278
1320
1363
1404
1446
1488
1530
1572
1614
1656
1698
1740
1782
1825
1867
1909
1951
1993
2035
2077
2119
2161
2203
2245
2287
2329
2370
2412
2455
2496
2538
2583
2624
2667
2709
2751
2793
2835
2877
2919
2961
3003
3045
3086
3128
3170
3213
3255
3298
3340
3382
3425
3468
3510
3552
3595
3637
3680
3722
3764
3807
3849
3892
3937
3979
4021
4064
4106
4148
4191
4233
4274
4316
4358
4401
4443
4486
4528
4571
4613
4655
4698
4740
4783
4825
4868
4910
4953
4995
5037
5080
5122
5165
5207
5250
5292
5335
5377
5419
5462
5504
5547
5589
5631
5674
5716
5758
5801
5844
5886
5928
5971
6013
6056
6098
6140
6183
6226
6268
6310
6353
6395
6438
6480
6522
6565
6607
6650
6692
6735
6777
6820
6862
6904
6947
6989
7031
7074
7117
7159
7201
7244
7286
7329
7371
7413
7456
7498
7541
7583
7626
7668
7711
7753
7795
7838
7880
7923
7965
8008
8050
8093
8135
8177
8220
8262
8304
8347
8390
8432
As you can see readings jumps a lot.I am not getting continuous readings. I can't figure out the problem here. I know I am going somewhere wrong in coding (I just started to learn ).
Any suggestions?