Serial port 红外Arduino定制系列

Serial port 红外Arduino定制系列,serial-port,arduino,infrared,Serial Port,Arduino,Infrared,基本上,我想做一个串行系统,运行arduino上红外LED之间的通信。下面的代码是一个数组,其中包含1和0的集合。我需要将这个8位数组转换成单个字符并输出它。但我不知道怎么做。我们将不胜感激 int IR_serial_read(){ int output_val; int current_byte[7]; int counter = 0; IR_serial_port = digitalRead(4); i

基本上,我想做一个串行系统,运行arduino上红外LED之间的通信。下面的代码是一个数组,其中包含1和0的集合。我需要将这个8位数组转换成单个字符并输出它。但我不知道怎么做。我们将不胜感激

    int IR_serial_read(){
        int output_val;
        int current_byte[7];
        int counter = 0;
        IR_serial_port = digitalRead(4);
        if (IR_serial_port == HIGH){
            output_val =1;
        }
        if (IR_serial_port == LOW){
            output_val =0;
        }
        current_byte[counter] = output_val;
        counter +=1
}

最好使用逐位运算符,我认为or函数在这里最有用,因为如果输入为1,它会设置一个位,如果输入为0,它不会更改,可以使用循环来循环数组并设置位。 看看你的代码,你确定你收到了全部8位吗?你似乎节省了7比特。 由于创建字节数组的唯一目的是仅使用1和0,因此建议立即在同一循环中设置位。 以下是我建议的代码:

byte inputByte = 0;                     // Result from IR transfer. Bits are set progressively.
for (byte bit = 0;  bit < 8; bit++) {   // Read the IR receiver once for each bit in the byte
    byte mask = digitalRead(4);         // digitalRead returns 1 or 0 for HIGH and LOW
    mask <<= bit;                       // Shift that 1 or 0 into the bit of the byte we are on
    inputByte |= mask;                  // Set the bit of the byte depending on receive
}
byte inputByte=0;//红外传输的结果。位是逐步设置的。
对于(字节位=0;位<8;位++){//为字节中的每一位读取一次IR接收器
字节掩码=digitalRead(4);//digitalRead为高电平和低电平返回1或0
面具