Unexpected serial output break

I faced strange issue with Serial.print. My code has the following cycle:

for (byte j = 0; j <= 36; j++ )
    {
       if(j%4==0){Serial.print( "-" );}
       Serial.print( message[j]);
    }

But i see no output on console.

However there is output for variants below:

for (byte j = 0; j <= 36; j++ )
    {
       Serial.print( message[j]);
    }
for (byte j = 0; j <= 36; j++ )
    {
       Serial.print(j);
    }
for (byte j = 0; j <= 36; j++ )
    {
       Serial.print(j%4);
    }

Don't understand what causes serial communication break :frowning:

Post your code.

Here is it:

#define MAX_LONG_DUR 920  
#define MIN_LONG_DUR 720 
#define MAX_SHORT_DUR 520 
#define MIN_SHORT_DUR 320 
#define MAX_DUR 340  
#define MAX_BITS 36

int rxPin = 2; 
volatile unsigned long impLow=0; 
volatile unsigned long impHigh=0;
volatile static unsigned long timer=0;
byte preambule=0;
byte message_len=0;
unsigned long start_timestamp=0;
char message[36];
  
void setup() {
  attachInterrupt(0, parse_wave, CHANGE);
  Serial.begin(115200);
  Serial.println("Serial initialized");
}

void loop(){
//  Serial.println(preambule);
  if(preambule==1 && message_len<=MAX_BITS)
  {
      process_bits();


  if (message_len==MAX_BITS && preambule==1){
     noInterrupts();
     preambule=message_len=0;
     for (byte j = 0; j <= MAX_BITS; j++ )
    {
      if(j%4==0){Serial.print( "-" );}
      Serial.print( message[j]);

    } 
     Serial.println(" [ message complete ]  "); 
     interrupts();
     return;
  }

  if (micros()-start_timestamp > 50000 && preambule==1){
    noInterrupts();
    preambule=message_len=0;
    Serial.println(" [ message fail ]  "); 
    interrupts();
    return;
  }
}  
}

void find_preambula(){
 if (impLow>=4000 && impLow<=4200 && impHigh>=MIN_SHORT_DUR && impHigh<=MAX_SHORT_DUR && preambule==0){
    Serial.print("ok: ");
    impLow=impHigh=0;
    preambule=1;
    start_timestamp=micros();
    return;
 } 
}

void process_bits(){
   
  if (preambule==1 && impLow>=MIN_SHORT_DUR  && impLow<=MAX_SHORT_DUR  && impHigh>=MIN_LONG_DUR && impHigh<=MAX_LONG_DUR){
 // Serial.print("0");
    message[message_len] = '0';
    impLow=impHigh=0;
    message_len++;
    return;
  }  
  
  if (preambule==1 && impLow>=MIN_LONG_DUR && impLow<=MAX_LONG_DUR && impHigh>=MIN_SHORT_DUR && impHigh<=MAX_SHORT_DUR){
  //Serial.print("1"); 
    message[message_len] = '1';
    impLow=impHigh=0;
    message_len++;
    return;
  }

}

void parse_wave(){
  if(digitalRead(rxPin)==HIGH){
    impLow=micros()-timer;
  } else  {
    impHigh=micros()-timer;
  }
  timer=micros();
  find_preambula();
}
message_len<=MAX_BITS)

Nope.
Your array is 36 elements long,
Element 36 does not exist.

AWOL:

message_len<=MAX_BITS)

Nope.
Your array is 36 elements long,
Element 36 does not exist.

Anyhow this code should work.

If i remove this line

if(j%4==0){Serial.print( "-" );}

Serial output appears as expected.

Serial initialized
ok: 0001010111100111101011100100110 [ message complete ]
ok: 0001010111100111101011100100110 [ message complete ]
ok: 0001010111100111101011100100110 [ message complete ]
ok: 0001010111100111101011100100110 [ message complete ]
ok: 0001010111100111101011100100110 [ message complete ]
ok: 0001010111100111101011100100110 [ message complete ]

Solution found: never use Serial.print inside interrupt (topic for the reference)