COM Problem with standalone arduino board with atmel328P chip

hiiiiii all
thanks for support from all members, :slight_smile: :slight_smile:
simply, my problem is that i make simple board based on Atmel328P with COM Port and MAX232 chip and 8 opto coupler input, my code is to send one character to PC if you close any point of 8 inputs
i try to make the code with arduino and try with atmel studio and i have the same problem.
my problem is when i powered the board with any power adapter like 9V with voltage regulator or use 5V adapter, the code didn’t send any char to PC, when i connect from TTL if i use arduino or USBASP if i use atmel studio code, the code is work ok without any problem, and i use USB Serial converter and select from PUTTY the com of my USB Serial converter.

// arduino code
// digital pin 2 has a pushbutton attached to it. Give it a name:
const int sw8 = 2;
const int sw7 = 3;
const int sw6 = 4;
const int sw5 = 5;
const int sw4 = 9;
const int sw3 = 10;
const int sw2 = 11;
const int sw1 = 12;

int buttonState_1 = 0;
int buttonState_2 = 0;
int buttonState_3 = 0;
int buttonState_4 = 0;
int buttonState_5 = 0;
int buttonState_6 = 0;
int buttonState_7 = 0;
int buttonState_8 = 0;

void setup() {
  Serial.begin(9600);
  pinMode(sw1, INPUT);
  pinMode(sw2, INPUT);
  pinMode(sw3, INPUT);
  pinMode(sw4, INPUT);
  pinMode(sw5, INPUT);
  pinMode(sw6, INPUT);
  pinMode(sw7, INPUT);
  pinMode(sw8, INPUT);
}
void loop() 
{
  buttonState_1 = digitalRead(sw1);
  buttonState_2 = digitalRead(sw2);
  buttonState_3 = digitalRead(sw3);
  buttonState_4 = digitalRead(sw4);
  buttonState_5 = digitalRead(sw5);
  buttonState_6 = digitalRead(sw6);
  buttonState_7 = digitalRead(sw7);
  buttonState_8 = digitalRead(sw8);

  if (buttonState_1 == LOW)
  {Serial.println("A");
    delay(500);}
  if (buttonState_2 == LOW)
  {Serial.println("B");
    delay(500);}
  if (buttonState_3 == LOW)
  {Serial.println("C");
    delay(500);}
  if (buttonState_4 == LOW)
  {Serial.println("D");
    delay(500);}
  if (buttonState_5 == LOW)
  {Serial.println("E");
    delay(500);}
  if (buttonState_6 == LOW)
  {Serial.println("F");
    delay(500);}
  if (buttonState_7 == LOW)
  {Serial.println("G");
    delay(500);}
  if (buttonState_8 == LOW)
  {Serial.println("H");
    delay(500);}

}

////////

//Atmel Studio Code
/*
 * 8_input_opto_program.c
 *
 * Created: 10/19/2018 5:57:23 PM
 * Author : Mahmoud
 */ 

#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>
#include <math.h>

uint8_t printed1 = 0;
uint8_t printed2 = 0;
uint8_t printed3 = 0;
uint8_t printed4 = 0;
uint8_t printed5 = 0;
uint8_t printed6 = 0;
uint8_t printed7 = 0;
uint8_t printed8 = 0;

int main(void)
{
 uint16_t UBRR_Value=103;
 UBRR0L = (uint8_t) UBRR_Value;
 UBRR0H = (uint8_t) (UBRR_Value>>8);
 UCSR0B = (1<<RXEN0) | (1<<TXEN0);
 UCSR0C = (3<<UCSZ00);

    while (1) 
    {
 
 while( ! (UCSR0A & (1<<UDRE0) ) );
 
 ///////////////////////////////////////////////////// input 1
 if (! (PINB & (1<<PB4)))
 {
 if (printed1 == 0)
 {
 UDR0 = '1';
 _delay_ms(50);
 printed1 = 1;
 _delay_ms(50);
 }
 }
 if (PINB & (1<<PB4))
 {
 if (printed1 == 1)
 {
 printed1 = 0;
 _delay_ms(50);
 }
 }
 ///////////////////////////////////////////////////// input 2
 if (! (PINB & (1<<PB3)))
 {
 if (printed2 == 0)
 {
 UDR0 = '2';
 _delay_ms(50);
 printed2 = 1;
 _delay_ms(50);
 }
 
 }
 if (PINB & (1<<PB3))
 {
 if (printed2 == 1)
 {
 printed2 = 0;
 _delay_ms(50);
 }
 }
 ///////////////////////////////////////////////////// input 3
 if (! (PINB & (1<<PB2)))
 {
 if (printed3 == 0)
 {
 UDR0 = '3';
 _delay_ms(50);
 printed3 = 1;
 _delay_ms(50);
 }
 }
 if (PINB & (1<<PB2))
 {
 if (printed3 == 1)
 {
 printed3 = 0;
 _delay_ms(50);
 
 }
 }
 ///////////////////////////////////////////////////// input 4
 if (! (PINB & (1<<PB1)))
 {
 if (printed4 == 0)
 {
 UDR0 = '4';
 _delay_ms(50);
 printed4 = 1;
 _delay_ms(50);
 }
 }
 if (PINB & (1<<PB1))
 {
 if (printed4 == 1)
 {
 printed4 = 0;
 _delay_ms(50);
 
 }
 }
 ///////////////////////////////////////////////////// input 5
 if (! (PIND & (1<<PD5)))
 {
 if (printed5 == 0)
 {
 UDR0 = '5';
 _delay_ms(50);
 printed5 = 1;
 _delay_ms(50);
 }
 }
 if (PIND & (1<<PD5))
 {
 if (printed5 == 1)
 {
 printed5 = 0;
 _delay_ms(50);
 }
 }
 ///////////////////////////////////////////////////// input 6
 if (! (PIND & (1<<PD4)))
 {
 if (printed6 == 0)
 {
 UDR0 = '6';
 _delay_ms(50);
 printed6 = 1;
 _delay_ms(50);
 }

 }
 if (PIND & (1<<PD4))
 {
 if (printed6 == 1)
 {
 printed6 = 0;
 _delay_ms(50);
 }
 }
 ///////////////////////////////////////////////////// input 7
 if (! (PIND & (1<<PD3)))
 {
 if (printed6 == 0)
 {
 UDR0 = '7';
 _delay_ms(50);
 printed7 = 1;
 _delay_ms(50);
 }
 }
 if (PIND & (1<<PD3))
 {
 if (printed7 == 1)
 {
 printed7 = 0;
 _delay_ms(50);
 }
 }
 ///////////////////////////////////////////////////// input 8
 if (! (PIND & (1<<PD2)))
 {
 if (printed6 == 0)
 {
 UDR0 = '8';
 _delay_ms(50);
 printed8 = 1;
 _delay_ms(50);
 }

 }
 if (PIND & (1<<PD2))
 {
 if (printed8 == 1)
 {
 printed8 = 0;
 _delay_ms(50);
 }
 }

 
    }
 return 0;
}

this is the output from putty in the 2 cases when connected the charters is ok, and when conect only adapter the PUTTY read something i don’t Know