[Serial issue] Exact same code wont work on Mega, but works with Uno

Hi, some code I was using on my Uno that was working perfectly (outputting on serial monitor correctly) does not seem to work on my Mega. It is receiving serial input (the Rx light flashes as it should), but I simply can't serial.read (I think) correctly.

To be clear, the problem is no serial monitor output, beyond the Serialprintln("This works") in setup.

I would just keep using the Uno, but I need the Mega's multi serial ports for this project. Any ideas on what's happening? Only thing I've seen mentioned was to put pinMode( 15, INPUT_PULLUP) in setup - I tried it and this did nothing. Thanks for the help!

const unsigned int MAX_MESSAGE_LENGTH = 8;
char message[MAX_MESSAGE_LENGTH];
unsigned int message_pos =0;

void setup() {

 Serial.begin(115200);
 Serial.println("This works");
 
}

void loop() {

 //Check to see if anything is available in the serial receive buffer
 
 
 while(Serial.available() > 0)
 {
   //Read the next available byte in the serial receive buffer
   char inByte = Serial.read();

   //Message coming in (check not terminating character) and guard for over message size
   if ( inByte != 'n' && (message_pos < MAX_MESSAGE_LENGTH -1) )
   {
     //Add the incoming byte 
    message[message_pos] = inByte;
     message_pos++;
   }
   //Full message received...
   else
   {
     //Add null character to string
     message[message_pos] = '\0';
     long number = atol(message);
     Serial.println(number);

     //Reset for the next message
     message_pos = 0;
   }
 }
} 

If you are connecting to pins 14 and 15, that is Serial3.

Post a schematic showing how the wiring should be.

Post photos showing how the wiring is.

Thanks for the response! I'm not connecting to any pins actually, this is purely just USB in - which is why I'm so confused as to why it's struggling..

I suggest you start by getting the code to compile.

Code compiles, I copy pasted with an extra bracket in my original post but its been edited (I deleted something incompletely). Again, like I said originally, this already works with my Uno.

So you are typing into the send box in the serial monitor?

The code that you posted will not compile.

error: 'message_pos' was not declared in this scope
error: 'MAX_MESSAGE_LENGTH' was not declared in this scope
error: 'message' was not declared in this scope

1 Like

Apologies, the edit to the post is showing up for me now, the declarations in the beginning were missing. Just tried it now and it works, thanks again.

No, this is receiving serial data from a raspberry pi python script, the port, baud, etc.. I've double checked and are all correct (nothing's changed since the Uno).

I believe it works now, let me know if there's still an issue.

That kind of information should have been provided in the first post. Please read the forum guidelines to see how to make a good post.

Terminating on a lower case N?

Will do next time, however for the sake of this problem, the only changed variable is the arduino itself, I figured the issue is Arduino specific -not input related. Thanks

Yes, the term character shouldn't matter, correct?

Just an odd choice.

Fair, any suggestions for the core issue at hand would be appreciated though

Robin2's serial input basics tutorial may be of interest.

That tutorial was great and was how I got to this point successfully on the Uno, it's really just getting the Mega to cooperate that doesn't seem to work. Unfortunately Robins tutorial doesn't address this odd case.

Update: turns out the Mega does actually properly work with my code, it simply can only handle inputs extremely slowly. Whereas the Uno received inputs with 20ms in between them no problem, I can only seem to get the Mega to output anything if I feed it serial inputs at an interval of 900ms. Any ideas?

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