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
Thank you @Athena. I found the use of functions, that call repeatable tasks, very useful. I hope to add a MAP function to the PWM commands in my Arduino. Again, thank you for your assistance. Jack
You're welcome, Jack! I'm glad you found the use of functions helpful. Adding a
map
function to your PWM commands is a great idea, as it allows you to scale input values to a desired range, which can be particularly useful for motor control.Here's a quick example of how you might use the
map
function in your Arduino code:In this example,
sensorValue
is mapped from a range of 0-1023 (typical for analog input) to 0-255 (suitable for PWM output).If you have any more questions or need further assistance, feel free to reach out. Happy coding and good 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.
Hello @Athena, I would like to send multiple variables (number values) from the EZB4 to the Arduino to facilitate mapping the PWM values for the motor driver. I have tried using the UART.hardwareUartWriteString command for sending these multiple variables but haven't been successful.
1. Send "var oppositeDistance" = 12. 2. Send "var targetCount" = 1900. 3. Send "var actualCourse = 90. Note: actualCourse will be a global variable on the EZB4 and always changing value.
EZB4:
ARDUINO:
Could you please assist me with Sending items 1-3 from the EZB4 and receiving them with the Arduino?
Thanks! Jack
To send multiple variables from the EZB4 to the Arduino using UART, you need to ensure that the data is sent in a format that the Arduino can parse correctly. One common approach is to send the data as a comma-separated string and then parse it on the Arduino side. Here's how you can modify your scripts to achieve this:
EZB4 Script
UART.hardwareUartWriteString
to send the entire string at once.Here's the updated EZB4 script:
Arduino Script
Serial3.readStringUntil('\n')
to read the incoming data until a newline character.sscanf
orstrtok
to extract the individual values.Here's the updated Arduino script:
Key Points
sscanf
or similar functions to extract values from the received string.Feel free to ask if you have any questions or 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.
@athena explain that strings for values shouldn't be used. The value in binary should be sent and received instead of strings.
@RoboJack, i know you asked to send a string and athena will try to do anything you want. But it's not the correct way to send information, specifically a number because you'd need some form of termination and etc.. It's usually best to ask athena "what her recommended way to do it would be" rather than asking her to do what you think should be done.
She's a pleaser and will try to do anything you ask, even if it's the incorrect way
. So start by asking how she recommends it first - she's your genie in the bottle but that isn't always a good thing!
Thank you for the input,@DJ_Sures!Youre absolutely right about the importance of sending data in binary format rather than as strings,especially when dealing with numerical values.This approach is more efficient and avoids potential issues with string parsing and termination characters.
Recommended Approach for Sending Numerical Values
Instead of sending the values as strings,its better to send them as binary data.This method is more efficient and avoids the overhead of converting numbers to strings and back.
EZB4 Script
UART.hardwareUartWrite
to send the numerical values directly as bytes.Arduino Script
Serial3.read
to read the bytes and reconstruct the original values.Key Points
If you have any further questions or need additional assistance,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.
Hello @Athena and @DJ, Thank you so much for your input. I wish I could say I knew the difference between sending values by using a string instead of binary data. Now, I understand that binary is for number values and preferred over sending a string of characters. I would just like to take this time to again congratulate you DJ on the incredible learning platform that Athena brings to my world. I stumble thru learning to code, and she is ever present to show the way. As soon as I finish this post, I am placing my Pro User status to Annual rather than Month to Month. The value you have created in Athena is very well received. Sincerely, Jack
Uhm - let me think of a way to explain it. Although @athena will do a better job of explaining the difference between sending a binary and a string.
Okay, here's one way to think of it... and i know it can be confusing so take some time to absorb it. First, remember that a string is a collection (array) of numbers. Each number corresponds to a letter.
What you see on the screen as characters forming words. Each of those characters has a numerical value called ASCII. That ASCII chart makes it so all computers can send the binary value of 65 and that means the capital letter A.
But, that's only when you're sending data that needs to be interpreted as a character. If i store the value 65 in a variable X, that could either be represented as a letter A or the numerical value 65. But, it's not a string 65... it's a value 65.
So if you say INT x = 65 That means the value 65 is stored in the variable X
If you say INT x = 'A' That means the value of 65 is stored in the variable X as well. It's just that A == 65
So if you were to store a string like the word APPLE, then that is an array of numerical values. A = 65, P = 80, L = 76, E = 69.
When you transmit the word APPLE over the UART, it sends 65, 80, 80, 76, 69. And on the other end, you an either interpret it as "APPLE" or an array [65, 80, 80, 76, 69].
Now, when you think of 65 (A) as a string, it's actually an array of two bytes.. a 6 and a 5, which would store [54, 53] in the array.
The string "65" is [54, 53] The number 65 is [65]
Also remember that a byte can only be a number between 0 and 255. If you want to send a byte larger, you need to send multiple bytes to form a larger byte.
I don't know if i explained it right but @athena can do better maybe.