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,@DJ_Sures,for the detailed explanation!Let's break it down further to ensure clarity.
Understanding Binary vs. String Transmission
Binary Transmission:
65
, it is transmitted as a single byte with the value65
.String Transmission:
"65"
is sent as two bytes:54
(for '6') and53
(for '5').Why Binary is Preferred for Numerical Data
Practical Example
Let's say you want to send the number
123
:123
.49
(for '1'),50
(for '2'), and51
(for '3').Conclusion
For numerical data, always prefer binary transmission to ensure efficiency and accuracy. Strings are best reserved for text data where readability is a priority.
If you have any more questions or need further clarification, 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.
Thank you @Athena for the clarification. I still need to ponder this new information to be sure I understand. In the mean time, I am having trouble compiling my Arduino code. Can you find the errors?
The following is my EZB4 code. Did I get it right?
Thank you, Jack
Let's review your Arduino and EZB4 code to identify potential issues and ensure everything is set up correctly.
Arduino Code Review
Syntax Errors:
{
have corresponding closing braces}
. It seems like there might be a missing closing brace for theloop()
function.Logical Errors:
loop()
function, the conditionif(Serial3.available()= 7)
should beif(Serial3.available() >= 7)
. The=
operator is for assignment, while>=
is for comparison.resetAll()
function is called correctly. It seems like it might be missing a closing brace in theloop()
function.Function Definitions:
AcountA()
andAcountB()
functions should be defined outside of theloop()
function.Data Transmission:
Serial3
, ensure that the data is correctly formatted and sent as expected. For example, when sendingAB_EncoderTot
, ensure that the high and low bytes are correctly calculated and sent.EZB4 Code Review
Variable Initialization:
pathTotalDistance
,pathEndPointDistance
,oppositeDistance
,actualCourse
, anddesiredCourse
are correctly initialized with the intended values.Data Transmission:
pathTotalDistance
andpathEndPointDistance
, ensure that the high and low bytes are correctly calculated and sent. The codeUART.hardwareUartWrite(0,[pathTotalDistance >> 8, pathTotalDistance & 0xFF])
is correct for sending two bytes.Polling Loop:
sleep(100)
function is correctly implemented to avoid excessive CPU usage.General Recommendations
Debugging:
Serial.print()
statements in the Arduino code to debug and verify the values being read and sent. This can help identify where the issue might be occurring.Testing:
If you have any specific error messages or issues during compilation, please share them, and I can provide more targeted 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.
As with Athena said about the missing Loop closing brace, there's an extra closing brace at the end of the file - which won't compile.
Your ARC code while() loop is also missing a closing brace
Hello @Athena, I am having trouble with the following Arduino code:
I wish to calculate oppositeDistance / adjacentRemainingDistance. (272 / 2440). Next get the Inverse Tangent of this amount. ( atan (272/2440)). Next multiply this amount by 57.32. ((atan (272 /2440) *57.32). Next subtract 90. ((atan (272 /2440) *57.32) - 90. It should equal about 83. I cannot get my Arduino code to arrive at this. Is the above equation in Arduino code correct? Thanks, Jack
The equation you provided seems logically correct for calculating the desired course. However, there are a few things to consider when implementing it in Arduino code:
Data Types: Ensure that the variables
oppositeDistance
andadjacentRemainingDistance
are of a floating-point type, such asfloat
ordouble
, to handle decimal values correctly.Order of Operations: The order of operations in your equation is correct, but ensure that the parentheses are used to maintain the intended order.
Precision: The
atan()
function in Arduino returns the angle in radians, so multiplying by 57.32 (which is approximately 180/) converts it to degrees.Here's how you can implement it in Arduino code:
Key Points:
oppositeDistance
andadjacentRemainingDistance
are floating-point numbers to avoid integer division, which would result in a loss of precision.Serial.print()
to verify the calculated value and ensure it matches your expectations.If you still encounter issues, please let me know, and we can further troubleshoot the problem.
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.
Thank you @Athena, Your solution using float worked. I am however getting a negative number. Can you look at my code and suggest a solution? Thanks Jack
The issue of getting a negative number for
desiredCourse
could be due to the calculation of theatan()
function, which returns values in radians and can result in negative angles depending on the quadrant of the angle. Let's go through your code and see how we can address this:Key Points to Consider
Quadrant Consideration:
atan()
function returns values between-/2
and/2
radians. When converting to degrees, this range becomes-90
to90
degrees.oppositeDistance
andadjacentRemainingDistance
are such that the angle is in the second or third quadrant, you might need to adjust the angle calculation.Adjusting the Angle:
atan2(y, x)
function instead ofatan(y/x)
. Theatan2()
function takes into account the signs of both arguments to determine the correct quadrant of the angle.atan2()
function returns values between-
and `` radians, which translates to-180
to180
degrees.Code Adjustment:
atan()
function withatan2()
to handle the quadrant correctly.Here's how you can modify your
toa()
function:Additional Suggestions
Serial.print()
statements to print the values ofoppositeDistance
andadjacentRemainingDistance
before the calculation to ensure they are as expected.oppositeDistance
andadjacentRemainingDistance
are positive and correctly represent the distances in your setup.By using
atan2()
, you should get the correct angle in degrees, taking into account the correct quadrant, which should resolve the issue of getting a negative number fordesiredCourse
. If you continue to experience issues, please let me know, and we can further troubleshoot the problem.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.