1

In Arduino Mega 2560 R3, properly selecting the Serial Monitor bauds, this code works with #define R 38400, but for some reason, not with #define R 19200 (?),

#define R 38400
int rate=R;
HardwareSerial* hwSerial[]={&Serial,&Serial1,&Serial2,&Serial3};
void setup() {
    for (int i=0;i<=3;i++){
        hwSerial[i]->begin(rate);
        while (!hwSerial[i]);
    } 
    hwSerial[0]->print("Hello World"); 
}

void loop() {
}

Both speeds should be not problematic for Arduino, what is happening?

It seems the int rate R; is problematic when changing speeds (??).

4
  • 1
    19200 is OK and 38400 fails? int can't hold 38400 – Juraj Jul 30 '19 at 17:57
  • Yes. And int RATE=38400; fails but #define RATE 38400 works (??) – Brethlosze Jul 30 '19 at 17:59
  • @Juraj That is the solution... Thanks... – Brethlosze Jul 30 '19 at 17:59
  • 1
    I made the same mistake few days ago. Took mi 10 minutes to realize – Juraj Jul 30 '19 at 18:07
2

Answer is simple. You assign 38400 to a variable of type int. The signed range of integer values that can be stored in 16 bits is −32,768 through 32,767;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.