(SOLVED) Serial Monitor error with reading PWM

I'm having a problem where I'm trying to get an Arduino Mega to read PWM signals from an RC receiver. Here is my code:

byte xAxisPin = 2;
byte yAxisPin = 3;
byte zAxisPin = 4;
byte elevationPin = 5;
byte speedPin = 6;
byte modePin = 7;

int xAxisPwmValue;
int yAxisPwmValue;
int zAxisPwmValue;
int elevationPwmValue;
int speedPwmValue;
int modePwmValue;

int mode = 1;

void setup() {
Serial.begin(9600);
pinMode(xAxisPin, INPUT);
pinMode(yAxisPin, INPUT);
pinMode(zAxisPin, INPUT);
pinMode(elevationPin, INPUT);
pinMode(speedPin, INPUT);
pinMode(modePin, INPUT);
while (! Serial);
Serial.println("Connected!");
}

void loop() {
xAxisPwmValue = pulseIn(xAxisPin, HIGH);
yAxisPwmValue = pulseIn(yAxisPin, HIGH);
zAxisPwmValue = pulseIn(zAxisPin, HIGH);
elevationPwmValue = pulseIn(elevationPin, HIGH);
speedPwmValue = pulseIn(speedPin, HIGH);
modePwmValue = pulseIn(modePin, HIGH);
Serial.println("X axis: "+xAxisPwmValue);
Serial.println("Y axis: "+yAxisPwmValue);
Serial.println("Z axis: "+zAxisPwmValue);
Serial.println("Elevation: "+elevationPwmValue);
Serial.println("Raw speed: "+speedPwmValue);
Serial.println("Raw mode: "+modePwmValue);
delay(100);
}

The issue is with the Serial monitor. I open it with the transmitter turned off, and set it to 9600 baud, and right away there's something wrong, because after printing "Connected!", nothing at all happens for about 3 seconds. After that, it prints this:
"
X axis:
Y axis:
Z axis:
Elevation:
Speed:
Mode:
"
With no values, which is strange because with the transmitter turned off, the receiver should be sending a value of 0.

The biggest issue arises when I turn on the transmitter, as the serial monitor begins printing strange strings such as "}⸮⸮⸮2⸮K⸮⸮⸮⸮o⸮⸮q⸮⸮⸮⸮⸮[j⸮⸮9⸮z⸮,Sw⸮⸮⸮~⸮⸮7#⸮⸮⸮g⸮⸮⸮;;⸮⸮⸮⸮k⸮⸮⸮$⸮⸮o~⸮y⸮⸮?⸮⸮⸮⸮[?⸮⸮;⸮⸮O⸮⸮⸮n⸮5⸮'⸮{~⸮n⸮⸮⸮⸮⸮;⸮⸮⸮⸮⸮⸮i⸮⸮o!⸮҈⸮⸮]⸮⸮⸮⸮⸮}⸮1⸮?׻⸮". This is repeated over and over, until I shut down the Arduino. At this point, the random strings continue even after turning the transmitter off.

If the use of Byte seems strange at the top, this is because I modified an existing PWM-reading prefab file.

Any help would be great. Is there a problem with my computer, my code, or is my Arduino Mega broken?

And before you ask, YES, the baudrate in the setup and serial monitor matches.

And yes, there is nothing connected to pins 0 or 1

Edit: I still can't figure out the problem. Here are some of the things I tried and their outcomes:

Changing "Byte" to "int" - Nothing changed
Changing "Byte" to "byte" - Error compiling
Switching to the other serial port available - Upload failure
Reversing polarity to the receiver - The symbols disappeared, but I didn't get any data.
Changing "pulseIn" to "analogRead" - Error compiling
Changing "pulseIn" to "digitalRead" - Error compiling
Changing "int #####" to "int ##### = 0" - Same exact thing as before, except for zeroes instead of blank spaces after the data.

Instead of -
Serial.println("X axis: "+xAxisPwmValue); , maybe try -
Serial.println("X axis: " + String(xAxisPwmValue));
Thanks!

1 Like

Thanks, that fixed it.
I guess the Serial monitor was getting confused as to whether it should print a string or an integer.

You're welcome

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.