Serial.print problem

I'm was wonder if serial print has print limit of a string?

switch (number) {
    case 1:
      checklight();
      Serial.println("Case 1 - Check Light");
      Serial.print("Light output is: ");
      Serial.println(light);
      number = light;      
      Wire.write(number);
      break;
    case 2:
      check_watertemp_in();
      Serial.println("Case 2 - Water Temp IN" );
      Serial.print("Water in is: ");
      Serial.println(watertempin);
      number = watertempin;
      Wire.write(number);      
      break;

As you can see in the code, the serial print in the case 1 is smaller in length the one in case 2. But if I add a character, ie: Serial.println("Case 1 - Check Light A"); it kill it freeze it. The only way to get is to start to send data again is to re upload the data. Why? I can't figure it out.

There’s more to this story. You should be able to print just about any length string you want if you’re doing it right. Post the whole code. This code isn’t happening in some interrupt handler is it?

If you’re running out of SRAM then adding one more character to a string will cause the thing to go completely haywire - usually continually resetting.

If you don’t want to post the whole sketch then read https://www.arduino.cc/en/tutorial/memory, learn to use the F() macro and let us know if that fixed it.

Here all the code, I tried the F() function and it didn’t help. If I add a space or one character in case 1 it dies when it’s running. But it complies ok… Case 2 - 4 are suppose to be plug to sensor which I don’t have at the moment, for now I’m faking them.

#include <Wire.h>
#define SLAVE_ADDRESS 0x04
 int state = 0;
 int number = 0;
 int photocellPin = 3;
 int photocellReading = 0;
 int LEDbrightness = 0;
 int light = 0;
 int outside_airtemp = 0;
 int watertempin = 0;
 int watertempout = 0;

void setup() {
 pinMode(13, OUTPUT);
 Serial.begin(9600); 
 Wire.begin(SLAVE_ADDRESS);
 Wire.onReceive(receiveData);
 Wire.onRequest(sendData);
 }

void loop(){
 delay(100);
 }

void receiveData(int byteCount){
  while(Wire.available()) {
  number = Wire.read();
  Serial.print(F("data received: "));
  Serial.println(number);
  switch (number) {
    case 1:
      checklight();
      Serial.println(F("Case 1 - Check Light"));
      Serial.print(F("Light out put is: "));
      Serial.println(light);
      number = light;      
      Wire.write(number);
      break;
    case 2:
      check_watertemp_in();
      Serial.println(F("Case 2 - Water Temp IN "));
      Serial.print(F("Water in is: "));
      Serial.println(watertempin);
      number = watertempin;
      Wire.write(number);      
      break;
    case 3:
      check_watertemp_out();
      Serial.println(F("Case 3 - Water Temp OUT "));      
      Serial.print(F("Water out is: "));
      Serial.println(watertempout);
      number = watertempout;
      Wire.write(number);
      break;
    case 4:
      check_outside_airtemp();
      Serial.println(F("Case 4 - Air TEMP "));
      Serial.print(F("Air temp outside:"));
      Serial.println(outside_airtemp);      
      number = outside_airtemp;
      Wire.write(number);
      break;
  }
 }
}

void sendData(){
 Wire.write(number);
 }
 
void checklight(){
  photocellReading = analogRead(photocellPin);
  LEDbrightness = map(photocellReading, 0, 1023, 0, 255);
  if (LEDbrightness>=29 && LEDbrightness<=56){
    light=128;
    goto end; 
    }
  if (LEDbrightness>=57 && LEDbrightness<=84){
    light=192;
    goto end;
    }
  if (LEDbrightness>=85 && LEDbrightness<=112){
    light=224;
    goto end;
    }
  if (LEDbrightness>=113 && LEDbrightness<=140){
    light=240;
    goto end;
    }
  if (LEDbrightness>=141 && LEDbrightness<=168){
    light=248;
    goto end;
    }
  if (LEDbrightness>=167 && LEDbrightness<=196){
    light=252;
    goto end;
    }
  if (LEDbrightness>=197 && LEDbrightness<=224){
    light=254;
    goto end;
    }
  if (LEDbrightness>=225){
    light=255;
    goto end;
    }
   end:{
   }
}

void check_watertemp_in(){
  watertempin = 22;
  }

void check_watertemp_out(){
  watertempout=25;
  }

void check_outside_airtemp(){
  outside_airtemp=26;
  }

Yes, Delta was right. You are trying to print inside an interrupt.

The interrupt should be very short. Just set a few variables which the main loop can pick up and process. Make sure to declare those variables volatile.

Anytime you use a switch/case construct and if you have some memory left, add a default like:

      default:
         Serial.print("I shouldbn't be here. number = ");
         Serial.println(number);
         break;

Quite often the value of number isn't what you think it should be. For example, since Wire.read() returns a byte, is what you get a '1' (as a char) or 1 (as an int)? If it's a char, the case isn't going to be in range for number.