Read bit and convert to char for IR

Hello, i'm working on an IR Project using keypad as an input and serial monitor as an output. I also using Pulse distance Coding and NEC remote protocol.

This is my tx code:

#include <Keypad.h>
String kodebithuruf[]=
  {"01100001","01100010","01100011","01100100","01100101",
  "01100110","01100111","01101000","01101001","01101010",
  "01101011","01101100","01101101","01101110","01101111",
  "01110000","01110001","01110010","01110011","01110100",
  "01110101","01110110","01110111","01111000","01111001",
  "01111010","00100000",
  "00110000","00110001","00110010","00110011","00110100",
  "00110101","00110110","00110111","00111000","00111001",
  "00101110","00101100","00111010"};

char data[50];
int k=0;
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3','^'},
  {'4','5','6','/'},
  {'7','8','9','

And this is my Rx code:

volatile int durasi;
volatile int prev_time;
volatile int data_simpan[255];
int x=0;
byte tambah;

void setup() {
Serial.begin(250000);
Serial.println(F("Ready to receive IR signals"));
attachInterrupt(1, jatuh, RISING);
}

void loop() {
}

void jatuh() {
detachInterrupt(1);
prev_time=micros();
attachInterrupt(1,naik,FALLING);
}

void naik(){
detachInterrupt(1);
durasi=micros()-prev_time;
data_simpan[x]=durasi;
baca(data_simpan[x]);
x++;
attachInterrupt(1, jatuh, RISING);
}

byte lock=0;
int ya=0;
void baca(int sementara){
if (ya!=9){
if ((sementara < 5000)&&(sementara > 4000)){
 ya=1;
 tambah==1;
 lock==0;
 //"header"
} else if ((sementara < 2000)&&(sementara > 1500)){
 lock==tambah+lock;
 tambah=tambah*2;
 ya++;
 //"1";
  } else if ((sementara < 800)&&(sementara > 300)){
    tambah=tambah*2;
    ya++;
    //"0";
    }
	
}else if (ya==9){
 Serial.write(lock);
 tambah=1;
 lock==0;
 ya=1;
 }else{
  Serial.println("Error bit");
  }
}

I succeed when i read binary data and if i only send 1 character and reset it before send any other data, but if i send more than 1, strange character always showed up.

This is my first post, so sorry if i mistaken anything. I really want to graduate this year, so i really appreciate any kind of help, thanks!},
  {'<','0','>','@'}
};

byte rowPins[ROWS] = {7, 6, 5, 4};
byte colPins[COLS] = {11, 10, 9, 8};

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
  Serial.begin(1200);
}

void loop(){
  char key = keypad.getKey(); 
  if (key){
    Serial.println(key);
    if (key!='


And this is my Rx code:

§DISCOURSE_HOISTED_CODE_1§


I succeed when i read binary data and if i only send 1 character and reset it before send any other data, but if i send more than 1, strange character always showed up. 

This is my first post, so sorry if i mistaken anything. I really want to graduate this year, so i really appreciate any kind of help, thanks!){
    data[k]=key;
    k++;
    }else if(key='

And this is my Rx code:

§_DISCOURSE_HOISTED_CODE_1_§

I succeed when i read binary data and if i only send 1 character and reset it before send any other data, but if i send more than 1, strange character always showed up.

This is my first post, so sorry if i mistaken anything. I really want to graduate this year, so i really appreciate any kind of help, thanks!){
    k++;
    data[k]='!';
    header();
    hasil();
    }
  }
}

void header() {
  tone(3,38000);
  delayMicroseconds(4250);
  noTone(3);
  header_mark();
}

void header_mark() {
  noTone(3);
  digitalWrite(3,HIGH);
  delayMicroseconds(4500);
  Serial.println("header");
}

void logika0(){
  tone(3,38000);
  delayMicroseconds(260);
  noTone(3);
  digitalWrite(3,HIGH);
  delayMicroseconds(520);
  Serial.print('0');
  return;
}

void logika1(){
  tone(3,38000);
  delayMicroseconds(260);
  noTone(3);
  digitalWrite(3,HIGH);
  delayMicroseconds(1670);
  Serial.print('1');
  return;   
}

void kirimhuruf(int posisi){
  String simbol = kodebithuruf[posisi];

for (int i=simbol.length();i>-1;i--;){

if (simbol[i]=='1'){
      logika1();
    }else{
      logika0();
    }
    }
}

void hasil() { 
int karakter;
int p=0,s=0;
while (data[p]!='!'){
  p++;
}
for (int k=0;k<p;k++){
karakter = data[k];
  Serial.print(data[k]);
  if ((karakter>='a')&&(karakter<='z'))
    kirimhuruf(karakter-'a');
    else
    if ((karakter>='A')&&(karakter<='Z'))
    kirimhuruf(karakter-'A');
      else
      if (karakter==' ')
      kirimhuruf(26);
        else
        if ((karakter>='0')&&(karakter<='9'))
        kirimhuruf(karakter-'0'+27);
          else
          if (karakter=='.')
          kirimhuruf(37);
            else
            if (karakter==',')
            kirimhuruf(38);
              else
              if (karakter==':')
              kirimhuruf(39);
  }
}


And this is my Rx code:

§DISCOURSE_HOISTED_CODE_1§


I succeed when i read binary data and if i only send 1 character and reset it before send any other data, but if i send more than 1, strange character always showed up. 

This is my first post, so sorry if i mistaken anything. I really want to graduate this year, so i really appreciate any kind of help, thanks!
  for (int i = simbol.length(); i > -1;)
  {
    i--;

Why the unconventional use of a for loop ?

More generally, why are you using Strings, especially when you could use bytes, or even better the bits within bytes to represent your data ?

"why the unconventional data for a loop?"
Because i want to send data from the LSB to MSB so i can read the data as byte in the receiver.

And i use string instead of byte in tx because i haven't succeed in using byte for rx

Why not put the variable decrement in the for loop initialisation ?

Serial sends bytes whatever data type you use

Yeah, i edited it, thanks.
Actually i've checked my coding using serial monitor and oscilloscope in the tx and rx are fine. But when i change the byte into char in the rx it error. Do you know why?

Do you know why?

Not without seeing your code and error messages.

The code is as looked above. But i add serial print to show the bit.

byte lock=0;
int ya=0;
void baca(int sementara){
if (ya!=9){
if ((sementara < 5000)&&(sementara > 4000)){
 ya=1;
 tambah==1;
 lock==0;
 Serial.println("header");
} else if ((sementara < 2000)&&(sementara > 1500)){
 lock==tambah+lock;
 tambah=tambah*2;
 ya++;
 Serial.print("1");
  } else if ((sementara < 800)&&(sementara > 300)){
    tambah=tambah*2;
    ya++;
    Serial.print("0");
    }else{
      Serial.print("x");
      }

}else if (ya==9){
 Serial.write(lock);
 tambah=1;
 lock==0;
 ya=1;
 }else{
  Serial.println("Error bit");
  }
}

But, the serial write didn't write the character as showed in the picture below.

decode.JPG

I've changed my code when using byte, i use = instead of ==. If i send more than 1 character, the last character can't be converted from byte to char. This is what i change:

byte lock=0;
int ya=0;
void baca(int sementara){
if (ya!=9){
if ((sementara < 5000)&&(sementara > 4000)){
 ya=1;
 tambah=1;
 lock=0;
 Serial.println("header");
} else if ((sementara < 2000)&&(sementara > 1500)){
 lock=tambah+lock;
 tambah=tambah*2;
 ya++;
 Serial.print("1");
  } else if ((sementara < 800)&&(sementara > 300)){
    tambah=tambah*2;
    ya++;
    Serial.print("0");
    }else{
      //Serial.print("x");
      }

}else if (ya==9){
 Serial.write(lock);
 tambah=1;
 lock=0;
 ya=1;
 }else{
  Serial.println("Error bit");
  }
}

This is when i send 7:

Ready to receive IR signals
header
11101100

It didn't show the character, but when i send 2 character, the first character is showed.

This is when i reset the tx and send 75:

Ready to receive IR signals
header
1110110071101100

And this is when i reset tx and send 759:

Ready to receive IR signals
header
111011007101011005011100

The last char only showed if i resend the data or add another data:

Ready to receive IR signals
header
11101100 7 10101100 5 00011100 //this is when i press 758
11101100 7 10101100 5 00011100 8 header //this is when i resend it                                 
11101100 7 10101100 5 00011100 8 10011100 //this is when i press 9 then send without reset
11101100 7 10101100 5 00011100 8 10011100 9 header // this is when i press resend

And so on..
Also, i want if i already send the data, i don't need to reset the arduino before send the new data.