All Tutorials / DJ Sures / Use Arduino for Wheel Encoder Counter

Use Arduino for Wheel Encoder Counter

Program Arduino

Here is the program which you will upload to your Arduino. The program has 2 interrupt counters for the pins connected to the encoders. The main program will loop until the letter 'a' i received from the EZ-B over the UART. Once the 'a' is received, the Arduino will transmit 2 bytes, which is are the two encoder values, respectively. Once the data is transmitted, the count of the 2 bytes is reset.

Resetting the values on each query will make it much easier in EZ-Builder to identify what wheel is spinning quicker/slower.

Arduino Code:

Code:


// just reading the encoder, nothing else.

int encoderValue_A = 0;
int encoderValue_B = 0;

void setup() {

Serial.begin(9600);

pinMode(2, INPUT);
pinMode(3, INPUT);

attachInterrupt(digitalPinToInterrupt(2), countA, FALLING);
attachInterrupt(digitalPinToInterrupt(3), countB, FALLING);
}

void loop() {

// if there is data to read, read it
if (Serial.available() > 0) {

// read the incoming data from the ezb
int incomingByte = Serial.read();

// command 'a' means transmit the current values and reset the counters
if (incomingByte == 'a') {

// send the values
Serial.write(encoderValue_A);
Serial.write(encoderValue_B);

// reset the values
encoderValue_A = 0;
encoderValue_B = 0;
}
}

void countA() {

encoderValue_A++;
}

void countB() {

encoderValue_B++;
}