Asked
— Edited

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
Hello @Athena, You wrote, "Use a Sufficient Buffer Size: If possible, configure the buffer size to accommodate the expected data rate. This can help reduce the risk of overflow."
How do I increase the Buffer Size? Should I monitor is level? How is that done? Thanks. Jack
Hello Jack,
Increasing the buffer size and monitoring its level can help manage data flow more effectively in your serial communication setup. Here's how you can approach these tasks:
Increasing Buffer Size
Arduino Serial Buffer:
HardwareSerial.h
file in the Arduino core library.HardwareSerial.h
file in your Arduino installation directory. The path might look something like this:Arduino/hardware/arduino/avr/cores/arduino/HardwareSerial.h
.#define SERIAL_BUFFER_SIZE 64
.#define SERIAL_BUFFER_SIZE 128
.Note: Modifying core library files can affect all Arduino projects, so proceed with caution. Also, increasing the buffer size will consume more RAM, which is limited on microcontrollers.
EZB Buffer:
Monitoring Buffer Level
Arduino Buffer Monitoring:
Serial.available()
. This function returns the number of bytes available for reading from the serial buffer.EZB Buffer Monitoring:
UART.hardwareUartAvailable()
function to check the number of bytes available in the UART receive buffer.Best Practices
By increasing the buffer size and monitoring its level, you can better manage data flow and reduce the risk of overflow. If you have any more questions or need further assistance, feel free to ask!
Best regards, Athena
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.
There might be a misunderstanding or a focus on details that aren't critical to the problem you're addressing. Athena, as a tool, is designed to provide feedback and guidance based on the context of your input so that her responses reflect your direction.
The "buffer" Athena mentioned refers to the Arduino's receive buffer. The suggestion arose because your original code transmits the X value in a loop at a very high frequency, even when the value hasn’t changed. This can overwhelm the Arduino's receive buffer, potentially causing performance issues or unexpected behavior.
Her recommendation to transmit the value only when it changes is rooted in good coding practices. This approach minimizes unnecessary data transmission and prevents the Arduino's receive buffer from being flooded with redundant information. While the term "buffer" might seem significant, it's not central to your implementation goals; instead, it’s part of her explanation for why optimizing your code in this way is beneficial.
Rather than focusing on the term "buffer," the key takeaway is her advice to optimize communication by sending updates only when necessary. This ensures efficient resource usage and avoids potential issues with the Arduino's ability to process incoming data.
Thanks @Athena and @DJ for your assistance. Jack
You're welcome, Jack! If you have any more questions or need further assistance in the future, feel free to reach out. Happy coding and best of luck with 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.
Nice. Keep rocking on your project
Hello @Athena,
1. Can you please review how I have used the MAP function for motor control and provide recommendations? 2. I would like to ramp up the motor speed on the initial pass thru the MAP function, and after that the mapped PWM should be applied directly. Can you please provide recommendations for ramping the speed up in the initial run? 3. Can you please review the overall sketch and provide recommendations?
Thanks so much. Jack
ARDUINO CODE:
Hello Jack! Let's go through your code and address your questions:
1. Review of the MAP Function Usage
The
map()
function in Arduino is used to re-map a number from one range to another. In your code, you are using it to adjust motor speeds based on the course difference. Here's a quick review:diffCourse
from-90
to90
to a PWM range of0
to255
. This is a common approach to adjust motor speeds based on directional error.diffCourse
is always within the-90
to90
range to avoid unexpected behavior. You might want to add aconstrain()
function to keepdiffCourse
within this range.2. Ramping Up Motor Speed
To ramp up the motor speed on the initial pass, you can gradually increase the PWM value over a set period. Here's a simple way to implement this:
rampUpMotors(leftSpeed, rightSpeed);
instead ofstartMotors(leftSpeed, rightSpeed);
during the initial run.3. Overall Sketch Review
Serial3
for communication with the EZ-B. Make sure the baud rate and data format match on both ends.loop()
function into smaller functions for better readability and maintainability.If you have any specific areas you'd like more detailed feedback on, feel free to ask!
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.