Arduino ide atmega16A serial string issue

Hi.
I have written quite a few working codes, but never one with serial.
I am trying to write serial for the very first time using arduino ide and atmega16A.
but getting some glitches
First I started with an uno at one end (board2) to see what was reading/writing.
but the uno died, so switches to another 16A.
right now I am using two breadboards before I attempt to fill in the blanks and a larger more complete board and code.
for some reason, I can't remove the Serial.println lines without all the code stopping to work ??? any ideas as to why ?
it works with those there even though I won't have a use for it.

on the next thing.
I can't seem to write higher end 3 digit numbers (above 255) so figured I would try string, but my attempts are completely shutting down the rest of the functions.
Scenario 1 works, but If I try 2 or 3 in place of 1, NOTHING works.

PS: The atmegas are running on internal 8mhz.
and, the reason I need the upper numbers is that I have to add 2 potentiometers map values into the serial and I assume they will take up the whole lower band of numbers,
Any help would be appreciated
thanks

//16A BOARD 1
const int SW1 = 3;
const int SW2 = 4;
const int SW5 = 2;
//const int SW7 = 7;
const int SW8 = 8;
const int SW20 = 21; 
const int LED1 =  19;  
const int LED2 =  17;
const int LED3 =  18;
const int LED4 =  5;
char LED2on = 123;
char LED2off = 113;
char SW20off = 20;
char SW20on = 21;
void setup() {
  pinMode(LED1, OUTPUT);
  digitalWrite(LED1, LOW);
  pinMode(LED2, OUTPUT);
  digitalWrite(LED2, LOW);
  pinMode(LED3, OUTPUT);
  digitalWrite(LED3, LOW);
  pinMode(LED4, OUTPUT);
  digitalWrite(LED4, LOW);
  pinMode(SW1, INPUT_PULLUP);
  pinMode(SW2, INPUT_PULLUP);
  pinMode(SW5, INPUT_PULLUP);
 // pinMode(SW7, INPUT_PULLUP);
  pinMode(SW8, INPUT_PULLUP);
  pinMode(SW20, INPUT_PULLUP);

  
  Serial.begin(115200);
  //Serial.write("ready");

  while (!Serial);
  delay(2000);
}

void loop() {
if (digitalRead (SW1) == LOW)
    {digitalWrite (LED2 ,HIGH);}
    
if (digitalRead (SW2) == LOW)    
    {digitalWrite (LED2 ,LOW); }
    

   if (Serial.available()) 
  {
    { digitalWrite(LED4,   HIGH);
    }
 /*
  * //#############
  // SCENARIO 3
    String L1 =  Serial.readString();
 
if (L1 == "666" )
   {digitalWrite (LED3 ,HIGH); 
   } 
if (L1 == "777" )
   {digitalWrite (LED3 ,LOW);
   }
//#############   
   
 
//#############
// SCENARIO 2 
 
if (Serial.readString()== "666" )
   {digitalWrite (LED3 ,HIGH); 
   } 
if (Serial.readString() == "777" )
   {digitalWrite (LED3 ,LOW);
   }
//#############   
*/
//#############
// SCENARIO 1 

char L1 =  Serial.read();
// ON MOM SW  
if (L1 == 14 )
   {digitalWrite (LED3 ,HIGH);delay(5); 
   } 
if (L1 == 19 )
   {digitalWrite (LED3 ,LOW);delay(10);
   }

//#############
   
// ON 2SWITCHES  
char L2 =  Serial.read();    
if (L2 == 15 )
   {digitalWrite (LED1 ,HIGH);delay(5); 
   }    
if (L2 == 20 )
   {digitalWrite (LED1 ,LOW);delay(10);
   } 
             
if (digitalRead (LED2) == LOW)
    { Serial.write(LED2off);
    delay(2);} 
if (digitalRead (LED2) == HIGH)
    {Serial.write(LED2on);
     delay(2);}
if (digitalRead (SW20) == LOW)
    { Serial.write(SW20on);
    delay(2);}
if (digitalRead (SW5) == LOW)
{ Serial.write(SW20off);
    delay(2);}  
    
  }

}
// 16A BOARD 2

const int SW1 = 31;
const int SW2 = 29; 
const int SW3 = 27;
const int SW4 = 25;
const int SW5 =  11;    
const int LED1 =  16;
const int LED2 =  17;
const int LED3 =  18;
const int LED4 =  19;
const int LED5 =  20;
const int LED6 =  21;
const int LED7 =  14;
char SW20off = 20;
char SW20on = 21;

void setup() {
  pinMode(LED1, OUTPUT);
  //digitalWrite(LED1, LOW);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(SW1, INPUT_PULLUP);
  pinMode(SW2, INPUT_PULLUP);
  pinMode(SW3, INPUT_PULLUP);
  pinMode(SW4, INPUT_PULLUP);
  pinMode(SW5, INPUT_PULLUP);
  Serial.begin(115200);
  
 Serial.println("PIA");//REMOVING THIS STOPS EVERYTHING ??
  while (!Serial);
  delay(2000);
}

void loop() {
  
  
        
 if (Serial.available()) 
  {
/*     
//#############
// SCENARIO 3 OR 2
if (digitalRead (SW1) == LOW)
     {Serial.write("666");delay(2);
     digitalWrite(LED1,   HIGH);
     }
                                    
   if (digitalRead (SW2) == LOW)    
     {Serial.write("777");delay(2);     
      digitalWrite(LED1,   LOW);     
     }
//#############     
  */    
   
//#############
// SCENARIO 1

 if (digitalRead (SW1) == LOW)
     {Serial.write(14);delay(2);
     digitalWrite(LED1,   HIGH);
     }
                                    
   if (digitalRead (SW1) == HIGH)    
     {Serial.write(19);delay(2);     
      digitalWrite(LED1,   LOW);     
     }
                            
//#############

   if (digitalRead (SW3) == LOW)
     {Serial.write(15);delay(2);
     digitalWrite(LED3,   HIGH);
     }
   if (digitalRead (SW4) == LOW)
     { Serial.write(20);delay(2);
     digitalWrite(LED3,   LOW);
     } 
     
 char LED2on = Serial.read () ; 
   if (LED2on == 123 )
   {digitalWrite (LED2 ,HIGH);delay(5);
  Serial.println("LED2on");//REMOVING THIS STOPS THE LED2 OFF ??
   } 
 char LED2off = Serial.read () ; 
   if (LED2off == 113 )
   {
   Serial.println("LED2off");//REMOVING THIS STOPS EVERYTHING ??
   digitalWrite (LED2 ,LOW);
   delay(10);
   }

   if (Serial.read() == 21 )
   {digitalWrite (LED6 ,HIGH);
   delay(5);}

   if (Serial.read() == 20 )
   {digitalWrite (LED6 ,LOW);
   delay(10);}
   
 { digitalWrite(LED7,   HIGH);
    //delay(200);
    //digitalWrite(LED5,   LOW);
   }  
         
  delay(10);

  }

}

Serial.write(...) writes byte sized values 0..255

If you have higher numbers you need to split them in 2 bytes.

int large = 31415;   //  assume the int is 2 bytes, on some platforms it is 4

Serial.write(large & 0xFF);  //  low byte of large
Serial.write(large >> 8);  //  high byte of large

At the receiving end you need to do two reads and combine the bytes into an int again.

ok, thanks,
like this to send ?

int L1 = 777;
 if (digitalRead (SW1) == LOW)
     {Serial.write(L1 & 0xFF);
      Serial.write(L1 >> 8);delay(2);
     digitalWrite(LED1,   HIGH);
     }

yes,

Yes. But as soon as you start sending multiple bytes, you need to have a way to synchronize the TX and RX ends. Otherwise the RX could mistake the first byte for the second one and vice versa.

OK, Good, But I am having issues with the receive end of this. Can't quite wrap my old brain around it since I am not sure what I am trying to look for to read ? and I wont have another uno for serial println till next week to see.

That line would ordinarily have needed to go after the while loop:

while (!Serial);        // Wait until the serial device is ready
Serial.write("ready");  // Now we are ready

However, the ATMega16, like the 328p, 328pb or 2560, does not have a virtual serial port for the program to wait for like on the Leonardo or Micro. The port is implemented in hardware which is always ready so the program will not wait because the loop will end on the first iteration. The loop is therefore meaningless on this MCU.

In this case a delay (which you do have later in the code) would have to be applied. You don't actually need both:

Serial.begin(115200);
delay(500);
Serial.write("ready");  // Now we are ready
Serial.flush();

The flush is not strictly necessary if there is a sufficient delay, but I sometimes like to add it for test purposes to make sure that the prompt is printed before the program moves on. You can tweak the delay as required. Not sure it needs 2 seconds. It partly depends on whether you are using a boot-loader, but even then it shouldn't need more than 1000ms.

Serial.readString() does not stop on a terminator. It just keeps on going until there is no more data and it times out and then returns whatever has been read into the target String variable. If you are sending data continuously, then it will just keep reading until there is a sufficient pause in the transmission which may not capture what you want. Since you are using String objects, you might as well use:

Serial.readStringUntil('\n');

and make sure that the string is terminated with a newline:

Serial.write("666\n");

or;

Serial.println("666");

That way you can be sure to read only the string up to the terminator which you can then do something with. Otherwise you have to check what has been read for terminator character and then split the current string and splice anything left over to the next one accordingly.

ArduinoDocs notes on the functions under discussion:

https://docs.arduino.cc/language-reference/en/functions/communication/serial/readString/

https://docs.arduino.cc/language-reference/en/functions/communication/serial/readStringUntil/

Have a look also at this thread for inspiration:

https://forum.arduino.cc/t/serial-input-basics-updated/382007

Those examples actually avoid the use of the String object and use a character array instead.

Just one more tip: before posting code use the Tools -> Auto Format function in the IDE. It just tidies it up a bit and make it a little easier for volunteers to read.

Thanks ALL for your replies.
I have not had time to try strings yet.
I did manage to program 2 boards. One with 2 Pots and two 7 segment displays to work two motors run by the other board., and did it all using numbers under 255.

Now I am not having fun trying to adjust the PWM pin speeds on this Atmega16A to help get rid of motors humming at all speeds except full on.
Any leads as to a good post to read.
I tried two that I found on line. but only one males the one motor a little quieter.