Data & asynchronous transmission to use in wireless communications

Hi! and thank you for support...

The receiving side has a photodetector that receives light: higher levels of light = '1' ...then there is some electronic stages(filtering and amplifier)...and on the last stage there is a decoder. This decoder is a Manchester to NRZ decoder. This means that at the receiving end, arduino will try to get a NRZ signal.

The current code receiver is based on the MANCHESTER library... The library was done thinking on RF systems but also works with IR and visible light as long there is hardware able to do it. In my case, i use a LED emmiter and not a RF emmiter/receiver.

Transmission:
Problems with this code is that HIGH and LOW have different times (sometimes), because the way i am writing to the ports

#include <VLC.h>

#define TxPin 8  //the digital pin to use to transmit data

unsigned int Tdata = 0xffff;  //the 16 bits to send

void setup()
{                
MANCHESTER.SetTxPin(TxPin);      // sets the digital pin as output default 4
MANCHESTER.SetPW(2);     // sets the pulse half width
}//end of setup

void loop()
{
  cli();
 
   MANCHESTER.Transmit(Tdata);
   sei();
 //delayMicroseconds(5);
}//end of loop

Receiving end:

#include <MANCHESTER.h>

#define RxPin 4

void setup()
{ 
 MANCHESTER.SetRxPin(RxPin); //user sets rx pin default 4
 MANCHESTER.SetTimeOut(1000); //user sets timeout default blocks
 Serial.begin(9600);   // Debugging only
}//end of setup

void loop()
{
 unsigned int data = MANCHESTER.Receive();
 Serial.println(data);
}//end of loop

Th above code was taken from examples but it works.
Basically waits for 3 '0's and 1 '1' and then starts receiving....

VLC.cpp
I made some changes from the real MANCHESTER library so i could have less response times...

#include "VLC.h"


//#define HALF_BIT_INTERVAL 1000 // microseconds

MANCHESTERClass::MANCHESTERClass() //constructor
{
  TxPin = TxDefault;
  pinMode(TxPin, OUTPUT); // sets the digital pin 4 default as output
}//end of constructor

void MANCHESTERClass::SetPW(unsigned int interval)
{
	HALF_BIT_INTERVAL = interval; // user sets the pulse width of half bit
}//end of set half pulse width

void MANCHESTERClass::SetTxPin(char pin)
{
	if (pin <=7)
		TxPin = pin; // user sets the digital pin as output
	else if ((pin >= 8) || (pin <= 13))
		TxPin = pin - 8; // user sets the digital pin as output
	DDRB = DDRB | B0000001 | (1 << TxPin);
  //pinMode(TxPin, OUTPUT); // sets the digital pin 4 default as output
}//end of set transmit pin

void MANCHESTERClass::Transmit(unsigned int data)
{
  unsigned char byteData[2] = { data  >> 8, data & 0xFF};
  TransmitBytes(2, byteData);
}

void MANCHESTERClass::TransmitBytes(unsigned char numBytes, unsigned char *data)
{
  // Setup last send time so we start transmitting in 10us
  //lastSend = micros() - HALF_BIT_INTERVAL + 10;
   
  // Send 14 0's
  for( int i = 0; i < 14; i++) //send capture pulses
     sendzero(); //end of capture pulses
    
  // Send a single 1
  sendone(); //start data pulse
 
  // Send the user data
  for (unsigned char i = 0; i < numBytes; i++)
  {
    unsigned int mask = 0x01; //mask to send bits
	
	for (char j = 0; j < 8; j++)
    {
	unsigned int c = data[i] & mask;
		switch( c ) 
		{
			case 0:
				sendzero();
				break;
			case 1 :
				sendone();
				break;
		}
      mask = mask << 1; //get next bit
    }//end of byte
  }//end of data
}//end of send the data

void MANCHESTERClass::sendzero(void)
{

 
  //digitalWrite(TxPin, HIGH);
  PORTB = B00000001;
 
  delayMicroseconds(HALF_BIT_INTERVAL);
  //digitalWrite(TxPin, LOW);
  PORTB = B00000000;

  //lastSend = micros();
}//end of send a zero

void MANCHESTERClass::sendone(void)
{

  //digitalWrite(TxPin, LOW);
  PORTB = B00000000;

delayMicroseconds(HALF_BIT_INTERVAL);
  //digitalWrite(TxPin, HIGH);
  PORTB = B00000001;
}//end of send one

MA

Thank you