Guys, I need help from you. I use JGA25-370 DC12V RPM250 motor which has built-in encoder. It has two channel pins, GND, 3.3V supply pins. I connect two-channels pins to digital pin 2 & 3 (I use UNO). I downloaded the code and modified it and got some data ( they are encoder0Pos value ). I know it by the name of variable in the program. However I don't know surely what are those. May be they are position or speed of the motor. In which way can I control my motor with L298 driver.
My code is as below.
int encoder0PinA=2;
int encoder0PinB=3;
int mA=9;int mB=10;
volatile int encoder0Pos=0;
volatile int encoder0PinALast=LOW;
volatile int n=LOW;
int valNew=0;
int valOld=0;
volatile int m=LOW;
void setup() {
pinMode(encoder0PinA, INPUT);
pinMode(mA, OUTPUT);
pinMode(mB, OUTPUT);
pinMode(encoder0PinB, INPUT);
Serial.begin(9600);
attachInterrupt(1, CountA, CHANGE);
attachInterrupt(0, StateB, FALLING);
}
void loop() {
digitalWrite(mA, HIGH);
digitalWrite(mB, LOW);
encoder0PinALast=n;
valNew=encoder0Pos;
if(valNew !=valOld){
Serial.print(encoder0Pos, DEC);
Serial.print(m);
Serial.print("/");
valOld=valNew;
}
}
void CountA(){
n=digitalRead(encoder0PinA);
if((encoder0PinALast == LOW) && (n==HIGH)){
if(m==LOW){
encoder0Pos--;
}else{
encoder0Pos++;
}
}
}
void StateB(){
m=digitalRead(encoder0PinB);
}
motor_encode_Read.ino (892 Bytes)