Wireless Data Transmission – Code

Transmitter Program

#define LASER_PIN           7
#define LASER_BAUDRATE      5  // bits per second
#define SIGNAL_HOLD_PERIOD  (1.0 / LASER_BAUDRATE) * 1000.0
#define SIGNAL_START_HOLD_PERIOD  SIGNAL_HOLD_PERIOD / 2.0

unsigned long transmit_begin;
byte c;
int message[10] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0};


int isNthBitSet (byte c, int n) {
  static unsigned char mask[] = {128, 64, 32, 16, 8, 4, 2, 1};
  return ((c & mask[n]) != 0);
}

void transmitHoldStart() {
  while (millis() - transmit_begin < SIGNAL_START_HOLD_PERIOD) {}
}

void transmitHold() {
  //transmit_begin = millis();
  while (millis() - transmit_begin < SIGNAL_HOLD_PERIOD) {}
}

void transmitByte(byte c) {

  for (int idx=0; idx<8; idx++) {
    message[idx+1] = isNthBitSet(c, idx);
  }

  transmit_begin = millis();
  digitalWrite(LASER_PIN, message[0]);
  transmitHoldStart();

  for (int idx=1; idx<10; idx++) {
    transmit_begin = millis();
    digitalWrite(LASER_PIN, message[idx]);
    transmitHold();
  }
}

void serialEvent() {
  c = Serial.read();
  //if (!isPrintable(c)) { return; }
  //Serial.write("Transmitting '");
  transmitByte( c );
  //if (isPrintable(c)) {
    Serial.write(c);
  //}
  //Serial.write("&gt;&gt;&gt; ");
}


void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(LASER_PIN, OUTPUT);
  transmit_begin = 0;
  c = '\0';
  Serial.begin(9600);
  Serial.write("\r\nReady to transmit!\r\n");
  Serial.write("\n&gt;&gt;&gt; ");

}

void loop() {
  
}

Receiver program

#define REF_PIN       24
#define SIG_PIN          23
#define LOGIC_1_CUTOFF      0
#define LASER_BAUDRATE      5
#define SIGNAL_HOLD_PERIOD  (1.0 / LASER_BAUDRATE) * 1000.0
#define START_BIT           1
#define STOP_BIT            0

int bit_val;
static unsigned char mask[] = {128, 64, 32, 16, 8, 4, 2, 1};
int buff[8] = {};
byte data = 0;
unsigned long receive_begin;
int bias = 0;
int bias_samp_size = 20;
int bias_samp_delay = 100;

void adjustSensorBias() {
  for (int i=0; i<bias_samp_size; i++) {
    bias += analogRead(SIG_PIN) - analogRead(REF_PIN);
    delay(bias_samp_delay);
  }
  bias /= bias_samp_size;
}

int sensorRead() {
  int val = analogRead(SIG_PIN) - analogRead(REF_PIN) - bias;
  Serial.write("sensorRead(): val = ");
  Serial.print(val);
  Serial.println();
  if (val < LOGIC_1_CUTOFF) { return 1; }
  return 0;
}

void poolBuffer() {
  int tmp = 0;
  for (int idx=0; idx<8; idx++) {
    tmp += buff[idx] * mask[idx];
  }
  data = (byte)tmp;
}

void receiveWait() {
  receive_begin = millis();
  while (millis() - receive_begin < SIGNAL_HOLD_PERIOD) {}
}

void laserRead() {
  receiveWait();
  for (int idx=0; idx<8; idx++) {
    buff[idx] = sensorRead();
    receiveWait();
  }
  if (sensorRead() != STOP_BIT) {
    receiveWait();
    receiveWait();
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(REF_PIN, INPUT);
  pinMode(SIG_PIN, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  adjustSensorBias();
}

void loop() {
  bit_val = sensorRead();

  //Serial.write("\r\nbit_val = ");
  //Serial.print(bit_val);
  //Serial.println();

  if (bit_val == START_BIT) {
    laserRead();
    poolBuffer();
    Serial.write("\r\ndata = ");
    Serial.write(data);
    Serial.println();
  }
}