Infra Red Arduino Custom Serial -
basically looking make serial-like system runs communication between ir leds on arduino. below code gets point having array collection of 1s , 0s in it. need convert 8 bit array single character , output it. don't know how this. appreciated.
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 }
this best done bitwise operators, think or function of best use here set bit if input 1 , not change if 0, use loop loop through array , set bits. looking @ code, sure receiving 8 bits? seem saving 7 bits. creating byte array solely purpose of using 1s , 0s, suggest setting bits in same loop. here code suggest:
byte inputbyte = 0; // result ir transfer. bits set progressively. (byte bit = 0; bit < 8; bit++) { // read ir receiver once each bit in byte byte mask = digitalread(4); // digitalread returns 1 or 0 high , low mask <<= bit; // shift 1 or 0 bit of byte on inputbyte |= mask; // set bit of byte depending on receive }
this put inside loop read bytes in data stream.
designed readability , can optimised further. reads least significant bit first.
can apply same technique array if wish keep using array of bytes 1s , 0s, replace digitalread
array location (current_byte[bit]
).
Comments
Post a Comment