Asked — Edited
Resolved Resolved by Athena AI!

Wheel Counting With Arduino

Hello @Athena,

Could you please review the following code that I am using on my Arduino Mega 2560 to count motor pulses? I am encountering an issue with the output on the Serial Monitor.

int encoderValue_A = 0;
int encoderValue_B = 0;

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  attachInterrupt(digitalPinToInterrupt(2), countA, RISING);
  attachInterrupt(digitalPinToInterrupt(3), countB, RISING);
}

void loop() {
  // Check if there is data available to read
  if (Serial.available() > 0) {
    // Read the incoming data from the EZ-B
    int incomingByte = Serial.read();
    Serial.print("Received: ");
    Serial.println((char)incomingByte);

    // Command 'a' indicates transmitting the current values and resetting the counters
    if (incomingByte == 'a') {
      delay(50);

      // Send the encoder values
      Serial.write(encoderValue_A);
      Serial.write(encoderValue_B);

      // Print the encoder values to the Serial Monitor
      Serial.print("Encoder A: ");
      Serial.println(encoderValue_A);
      Serial.print("Encoder B: ");
      Serial.println(encoderValue_B);

      // Reset the encoder values
      encoderValue_A = 0;
      encoderValue_B = 0;
    }
  }
}

void countA() {
  encoderValue_A++;
}

void countB() {
  encoderValue_B++;
}

Here is the output that appears on the Serial Monitor:

Received: a
XXEncoder A: 0
Encoder B: 0

The "XX" in front of "Encoder A" are displayed as square boxes, almost like placeholders. Could you help me understand what these characters are and how I can remove them?

Thank you,
Jack


Related Hardware Arduino Due/Mega
Related Control Wheel Encoder Counter

ARC Pro

Upgrade to ARC Pro

Stay on the cutting edge of robotics with ARC Pro, guaranteeing that your robot is always ahead of the game.

PRO
Canada
#17  

Certainly, Jack! Here's the updated Arduino code with the code section separated for easy copy and paste:

int encoderValue_A = 0;
int encoderValue_B = 0;

void setup() {
    Serial.begin(9600); // Communication with EZ-B
    Serial1.begin(9600); // Debugging with Serial Monitor
    pinMode(2, INPUT);
    pinMode(3, INPUT);
    attachInterrupt(digitalPinToInterrupt(2), countA, RISING);
    attachInterrupt(digitalPinToInterrupt(3), countB, RISING);
}

void loop() {
    // Check if there is data available to read
    if (Serial.available() > 0) {
        // Read the incoming data from the EZ-B
        int incomingByte = Serial.read();
        Serial1.print("Received: ");
        Serial1.println((char)incomingByte);

        // Command 'a' indicates transmitting the current values and resetting the counters
        if (incomingByte == 'a') {
            delay(50);

            // Send the encoder values as two bytes each
            Serial.write((byte)(encoderValue_A >> 8)); // High byte
            Serial.write((byte)(encoderValue_A & 0xFF)); // Low byte
            Serial.write((byte)(encoderValue_B >> 8)); // High byte
            Serial.write((byte)(encoderValue_B & 0xFF)); // Low byte

            delay(50);

            // Print the encoder values to the Serial Monitor
            Serial1.print("Sent Encoder A: ");
            Serial1.println(encoderValue_A, DEC); // Use DEC to print as decimal
            Serial1.print("Sent Encoder B: ");
            Serial1.println(encoderValue_B, DEC); // Use DEC to print as decimal

            // Reset the encoder values
            encoderValue_A = 0;
            encoderValue_B = 0;
        }
    }
}

void countA() {
    encoderValue_A++;
}

void countB() {
    encoderValue_B++;
}

Key Changes:

  • Use DEC in Serial1.println(): This ensures that the encoder values are printed as decimal numbers, which should be readable in the Serial Monitor.

Next Steps:

  • Upload the modified code to your Arduino.
  • Open the Serial Monitor and ensure that the baud rate is set to 9600 to match the Serial1.begin(9600); setting.
  • Check the Serial Monitor for the printed encoder values.

By following these steps, you should be able to see the encoder values printed correctly in the Serial Monitor. If you continue to experience issues, please let me know!


I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
#18  

Hello @Athena,

I ran the new script for the Arduino.  It still prints gibberish on a single line. I tried commenting out (//) printing out the var for the Encoders and tried printing out just the word "Encoder ".  Still gibberish.. I have checked the Serial Monitor baud and it is 9600.   The Arduino is receiving the "a" via UART and sending the encoder values to the EZB4.   However, something seems to be wrong with the Serial1.print statement.   Can you help me with this Serial1.print?

int encoderValue_B = 0;
int encoderValue_A = 0;

void setup() {
    Serial.begin(9600); // Communication with EZ-B
    Serial1.begin(9600); // Debugging with Serial Monitor
    pinMode(2, INPUT);
    pinMode(3, INPUT);
    attachInterrupt(digitalPinToInterrupt(2), countA, RISING);
    attachInterrupt(digitalPinToInterrupt(3), countB, RISING);
}

void loop() {
    // Check if there is data available to read
    if (Serial.available() > 0) {
        // Read the incoming data from the EZ-B
        int incomingByte = Serial.read();
        Serial1.print("Received: ");
        Serial1.println((char)incomingByte);

        // Command 'a' indicates transmitting the current values and resetting the counters
        if (incomingByte == 'a') {
            delay(50);

            // Send the encoder values as two bytes each
            Serial.write((byte)(encoderValue_A >> 8)); // High byte
            Serial.write((byte)(encoderValue_A & 0xFF)); // Low byte
            Serial.write((byte)(encoderValue_B >> 8)); // High byte
            Serial.write((byte)(encoderValue_B & 0xFF)); // Low byte

            delay(50);

            // Print the encoder values to the Serial Monitor
            Serial1.print("Sent Encoder A: ");
            Serial1.println(encoderValue_A, DEC); // Use DEC to print as decimal
            Serial1.print("Sent Encoder B: ");
            Serial1.println(encoderValue_B, DEC); // Use DEC to print as decimal

            // Reset the encoder values
            encoderValue_A = 0;
            encoderValue_B = 0;
        }
    }
}

void countA() {
    encoderValue_A++;
}

void countB() {
    encoderValue_B++;
}

Thank you, Jack

PRO
Canada
#19  

Hello Jack,It seems like there might be an issue with how the data is being printed to the Serial Monitor.Lets try a few things to troubleshoot and resolve the issue:### 1.Check Serial1 Connection Ensure that the Serial1 connection is correctly set up and that you are monitoring the correct serial port on your computer.### 2.Verify Serial1.print Statements Lets simplify the code to ensure that the Serial1.print statements are working correctly:cpp int encoderValue_A = 0;int encoderValue_B = 0;void setup(){ Serial.begin(9600);// Communication with EZ-B Serial1.begin(9600);// Debugging with Serial Monitor pinMode(2,INPUT);pinMode(3,INPUT);attachInterrupt(digitalPinToInterrupt(2),countA,RISING);attachInterrupt(digitalPinToInterrupt(3),countB,RISING);} void loop(){ // Check if there is data available to read if(Serial.available()0){ // Read the incoming data from the EZ-B int incomingByte = Serial.read();Serial1.print(Received:);Serial1.println((char)incomingByte);// Command a indicates transmitting the current values and resetting the counters if(incomingByte == a){ delay(50);// Send the encoder values as two bytes each Serial.write((byte)(encoderValue_A 8));// High byte Serial.write((byte)(encoderValue_A 0xFF));// Low byte Serial.write((byte)(encoderValue_B 8));// High byte Serial.write((byte)(encoderValue_B 0xFF));// Low byte delay(50);// Simplified Serial1.print statements Serial1.println(Sent Encoder Values:);Serial1.print(A:);Serial1.println(encoderValue_A);Serial1.print(B:);Serial1.println(encoderValue_B);// Reset the encoder values encoderValue_A = 0;encoderValue_B = 0;} } } void countA(){ encoderValue_A++;} void countB(){ encoderValue_B++;}### Key Changes:- Simplified Serial1.print statements to ensure clarity.- Added a newline after Sent Encoder Values:to separate the output.### Next Steps:- Upload the modified code to your Arduino.- Open the Serial Monitor and ensure that the baud rate is set to 9600.- Check the Serial Monitor for the printed encoder values and messages.If the issue persists,please double-check the wiring and connections to ensure that everything is set up correctly.Let me know if you need further assistance!


I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
#20  

Hello @Athena, Please resend previous post so I can copy and paste the code. Thanks Jack

#21  

Hello @Athena

I have done some research and found out that Arduino's will only serial.print 0.  No printing to the serial monitor from Serial1,2,3. Therefore, I can't print to the serial monitor with the Serial1.print statement.  So,  changed the print statement back to Serial.print and changed the UART references to Serial3 for the Arduino to EZB4 communication.
I have printed the code for the Arduino:

int encoderValue_B = 0;
int encoderValue_A = 0;

void setup() {
    Serial3.begin(9600); // Communication with EZ-B
    Serial.begin(9600); // Debugging with Serial Monitor
    pinMode(2, INPUT);
    pinMode(3, INPUT);
    attachInterrupt(digitalPinToInterrupt(2), countA, RISING);
    attachInterrupt(digitalPinToInterrupt(3), countB, RISING);
}

void loop() {
    // Check if there is data available to read
    if (Serial3.available() > 0) {
        // Read the incoming data from the EZ-B
        int incomingByte = Serial3.read();
        Serial.print("Received: ");
        Serial.println((char)incomingByte);

        // Command 'a' indicates transmitting the current values and resetting the counters
        if (incomingByte == 'a') {
            delay(10);

            // Send the encoder values as two bytes each
            Serial3.write((byte)(encoderValue_A >> 8)); // High byte
            Serial3.write((byte)(encoderValue_A & 0xFF)); // Low byte
            Serial3.write((byte)(encoderValue_B >> 8)); // High byte
            Serial3.write((byte)(encoderValue_B & 0xFF)); // Low byte

            

            // Print the encoder values to the Serial Monitor
            Serial.print("Sent Encoder A: ");
            Serial.println(encoderValue_A, DEC); // Use DEC to print as decimal
            Serial.print("Sent Encoder B: ");
            Serial.println(encoderValue_B, DEC); // Use DEC to print as decimal

            // Reset the encoder values
            encoderValue_A = 0;
            encoderValue_B = 0;
        }
    }
}

void countA() {
    encoderValue_A++;
}

void countB() {
    encoderValue_B++;
}

Here is my EZB4 code:

// Initialize UART on port 0 with a baud rate of 9600
UART.initHardwareUart(0, 9600);

function loop() {
    // Define the string to send
    var stringToSend = "a";
    
    // Send a request for data to the Arduino
    UART.hardwareUartWriteString(0, stringToSend);
    print("Sent: " + stringToSend + " to Arduino");
    
    // Wait a bit for the data to be transmitted
    sleep(100);
    
    // Check if data is available
    var dataAvail = UART.hardwareUartAvailable(0);
    print("Data available: " + dataAvail);
    
    if (dataAvail >= 4) { // Expecting 4 bytes (2 bytes for each encoder value)
        // Read the encoder values
        var highByteA = UART.hardwareUartRead(0, 1)[0]; // Read high byte of Encoder A
        var lowByteA = UART.hardwareUartRead(0, 1)[0];  // Read low byte of Encoder A
        var encoderValue_A = (highByteA << 8) | lowByteA; // Combine bytes
        
        var highByteB = UART.hardwareUartRead(0, 1)[0]; // Read high byte of Encoder B
        var lowByteB = UART.hardwareUartRead(0, 1)[0];  // Read low byte of Encoder B
        var encoderValue_B = (highByteB << 8) | lowByteB; // Combine bytes
        
        // Print the encoder values
        print("Encoder A: " + encoderValue_A);
        print("Encoder B: " + encoderValue_B);
    } else {
        print("Received data is not in the expected format.");
    }
    
    // Wait before the next loop iteration
    sleep(100);
    
    // Call the loop function again
    loop();
}

// Start the loop
loop();

Thank you again for all your patience and help. Do the scripts above look OK?  Any obvious mistakes? Jack

PRO
Canada
#22  

Hello Jack!Your updated code looks much better now that you've adjusted the serial communication to use Serial3 for the Arduino Mega 2560 to communicate with the EZ-B v4. Let's review both the Arduino and EZ-B v4 scripts to ensure everything is set up correctly.

Arduino Code

int encoderValue_A = 0;
int encoderValue_B = 0;

void setup() {
    Serial3.begin(9600); // Communication with EZ-B
    Serial.begin(9600);  // Debugging with Serial Monitor
    pinMode(2, INPUT);
    pinMode(3, INPUT);
    attachInterrupt(digitalPinToInterrupt(2), countA, RISING);
    attachInterrupt(digitalPinToInterrupt(3), countB, RISING);
}

void loop() {
    // Check if there is data available to read
    if (Serial3.available() > 0) {
        // Read the incoming data from the EZ-B
        int incomingByte = Serial3.read();
        Serial.print("Received: ");
        Serial.println((char)incomingByte);

        // Command 'a' indicates transmitting the current values and resetting the counters
        if (incomingByte == 'a') {
            delay(10);

            // Send the encoder values as two bytes each
            Serial3.write((byte)(encoderValue_A >> 8)); // High byte
            Serial3.write((byte)(encoderValue_A & 0xFF)); // Low byte
            Serial3.write((byte)(encoderValue_B >> 8)); // High byte
            Serial3.write((byte)(encoderValue_B & 0xFF)); // Low byte

            // Print the encoder values to the Serial Monitor
            Serial.print("Sent Encoder A: ");
            Serial.println(encoderValue_A, DEC); // Use DEC to print as decimal
            Serial.print("Sent Encoder B: ");
            Serial.println(encoderValue_B, DEC); // Use DEC to print as decimal

            // Reset the encoder values
            encoderValue_A = 0;
            encoderValue_B = 0;
        }
    }
}

void countA() {
    encoderValue_A++;
}

void countB() {
    encoderValue_B++;
}

EZ-B v4 Code

// Initialize UART on port 0 with a baud rate of 9600
UART.initHardwareUart(0, 9600);

function loop() {
    // Define the string to send
    var stringToSend = 'a';

    // Send a request for data to the Arduino
    UART.hardwareUartWriteString(0, stringToSend);
    print("Sent: " + stringToSend + " to Arduino");

    // Wait a bit for the data to be transmitted
    sleep(100);

    // Check if data is available
    var dataAvail = UART.hardwareUartAvailable(0);
    print("Data available: " + dataAvail);

    if (dataAvail >= 4) { // Expecting 4 bytes (2 bytes for each encoder value)
        // Read the encoder values
        var highByteA = UART.hardwareUartRead(0, 1)[0]; // Read high byte of Encoder A
        var lowByteA = UART.hardwareUartRead(0, 1)[0];  // Read low byte of Encoder A
        var encoderValue_A = (highByteA << 8) | lowByteA; // Combine bytes

        var highByteB = UART.hardwareUartRead(0, 1)[0]; // Read high byte of Encoder B
        var lowByteB = UART.hardwareUartRead(0, 1)[0];  // Read low byte of Encoder B
        var encoderValue_B = (highByteB << 8) | lowByteB; // Combine bytes

        // Print the encoder values
        print("Encoder A: " + encoderValue_A);
        print("Encoder B: " + encoderValue_B);
    } else {
        print("Received data is not in the expected format.");
    }

    // Wait before the next loop iteration
    sleep(100);

    // Call the loop function again
    loop();
}

// Start the loop
loop();

Key Points:

  • Arduino Code: Ensure that Serial3 is used for communication with the EZ-B v4, and Serial is used for debugging with the Serial Monitor.
  • EZ-B v4 Code: Ensure that the UART is initialized correctly and that the data is read and combined properly.

Suggestions:

  • Ensure that the wiring between the Arduino Mega 2560 and the EZ-B v4 is correct, especially the TX and RX lines.
  • Double-check the baud rate settings on both the Arduino and EZ-B v4 to ensure they match.
  • Make sure the Arduino is connected to the correct COM port on your computer.

If you encounter any further issues, feel free to reach out for more assistance!


I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
#23  

Hello @Athena,

Everything runs perfectly.  I changed the Arduino UART wires to Tx3 and Rx3. Thanks a million! Jack

PRO
Canada
#24  

Hello Jack,Thats fantastic news!Im glad to hear that everything is running smoothly now and that the change to Tx3 and Rx3 resolved the issue. If you have any more questions or need further assistance in the future, feel free to reach out. Happy robot building, and enjoy your project!


I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.