how to seperate the binary code

I am doing a light communication project. The transmitter side translate the ASCII code to binary code and send 1010101 to the LED. If the LED turn on it means 1, turn off it means 0. The receiver side I use a photoresister to receive the LED light's number. If it bigger than one number( the LED turn on) it means 1, else it means 0. I need to write a program to capture these 1 and 0, and translate it to ASCII. The transmitter side I followed the morse code example. But the receiver side I totally don't know how to do. How to separate the 101010? I need a pattern insert into the signal to let the receiver side easy to separate, does anyone have a sample code or example ?
Thank you.

You could do like the UART does. Send the data at a predetermined frequency. Let's say each bit is shown for 2 ms.

When no data is transmitted, the LED is on.
When you want to send a byte, you send a start signal, two low bits, so the LED is off for 4 ms.
Then you show each bit in your byte for 2 ms each.
You finish with a stop bit, a high, so the LED is on for 2 ms.
In total you "sent" 2+8+1 bits to transmit your byte. Now you can send the next byte, or go back to idle.

On the receiving end:
You detect the start signal (two low bits) - 4 ms
Then you center your sample by waiting 1/2 bit length - 1 ms - before sampling the first data bit
You sample a bit each 2 ms until you have the whole byte.
Detect the stop bit, and save the byte.

arnakke:

I really appreciate your idea, it just what I think. But I don't understand what UART is. Could you show me some sample code?
Thanks.

http://www.terryjfield.com/?p=22

Note the author used a photo transistor because photo resistors can be slow. A LED should substitute for the IR LED without any issues but remember silicon photo transistors are more sensitive to some colors than others ... You can Google this fact or better, refer to the data sheet for the pt you purchased.

Ray

The receiver have received a bunch of 101010. I just separate every code to 6 bits of binary, such as 'a' is 101000, 's' is 101010. Every code's first bit is 1. I wrote the code as follow, if the photocell receive the light number bigger than 800 then it is 1, else is 0.
Who can help me to change it better. The follow code only can translate 40 that I set. I do want to translate 101000 to 'a', but I don't know how to do.

int photocellPin = 2;  //  (photocell) anallog pin 2
int photocellVal = 0;
int liuming = 800; // photocell variable
int words[40];
int i = 0;

void setup() {
  Serial.begin(9600);
}
 
void loop() {
  
int k;
for (k=0; k<40;k++)
{  photocellVal = analogRead(photocellPin);
   if ( photocellVal > liuming )
   { words[k]=1;
     delay (250);
   }
  else
  { words[k]=0;
   delay(250);
  } 
 
 // delay(250);      
}

for (k = 0; k < 40; k++)

{ 
  if (words[k] == 1 )
   {  
     for ( i = 0; i< 6; i++)
     { Serial.print(words[k+i]);}
     k = k+i-1 ; }
  else 
   { Serial.print('\n');}  
   }     

   }

It's not an easy problem to decode Morse code. First off, you can't just separate your string of 1's and 0's into pieces 6 bits long. Some characters are long ('h' is 10101010) and others are short ('e' is 10). By the way, the example you give for 's' (3 dots) looks correct (101010), but 'a' (dot dash) should be 101110, not 101000. Most importantly, you need to have a way to know when a character ends. In Morse code the space between characters is the same length as a dash, which would be three zeros in your system. First find a way to determine when a character has ended.

Once you have accomplished that, each character can be isolated. Then simply multiply each bit of the character by increasing powers of a number, say 2. For example, multiply the first bit by 2, the second bit by 4, the third bit by 8 and so on, adding up the numbers as you go. With this method each character will have a unique value that can be identified in a look-up table or a switch() statement.

Good luck!

Hi again!
The UART is Universal Asynchronous Receiver/Transmitter, it's the hardware that takes care of sending/receiving serial data on eg the arduino. Maybe it wasn't entirely correct to say "do as the uart", because you can program it to act in different ways, but what I suggested is the most common use. Except I just looked it up on wikipedia, and I can see that I mixed up the start/stop bits: it's usually 1 start bit and 2 stop bits.

To get an idea of a software implementation you could take a look at the softwareserial source files. But it might be a bit too complicated for now. I can make a suggestion for some simple code you can test:

Transmitter:

// bit width in milliseconds
// should be even for easy centering
#define BITW 60

int ledpin = 3;


void setup() {
  // Put our light bus in idle state
  digitalWrite(ledpin, 1);
}

void loop() {
  // Send message every 2 seconds
  ledWrite("Hello world!\n");
  delay(2000);
}


// Send one character byte on the LED
void ledWrite(char c) {
  
  // Send the start bit
  digitalWrite(ledpin, 0);
  delay(BITW);
  
  // Transmit the byte, one bit at a time
  // least significant bit first
  for (byte i = 0; i<8; i++) {
    bool bit = (c >> i) & 0x01; // Shift right and extract rightmost bit
    digitalWrite(ledpin, bit);
    delay(BITW);
  }
  
  // Send the stop bits
  digitalWrite(ledpin, 1);
  delay(BITW*2);
  
  // We leave the led on, meaning our light-bus is idle
}

// Send a cstring on the led
void ledWrite(char s[]) {
  int i = 0;
  // Send each char until end of string
  while (s[i] != '\0') {
    ledWrite(s[i]);
    i++;
  }
}

Receiver:

// bit width in milliseconds
// should be even for easy centering
#define BITW 60

int photopin = A0;

int lim = 800; // above or equal is 1, below is 0

bool ledRead() {
  return (analogRead(photopin) >= lim);
}


void setup() {
  Serial.begin(115200);
  
  // Wait for transmitter to come alive
  while (!ledRead());
}

void loop() {
  // Wait for start bit
  while (ledRead());
  
  // Center our sampling
  delay(BITW/2);
  
  // This will hold the character we receive
  char c = 0;
  
  // If start bit is still there, start sampling
  if (!ledRead) {
    for (byte i = 0; i<8; i++) {
      delay(BITW);
      c |= ledRead() << i;      
    }
    // Check that we get a stop bit and then pass on data
    delay(BITW);
    if (ledRead())  {
      Serial.write(c);
    }
    
    // The last stop bit is ignored so we can catch up with any
    // misalligment in the sampling
  }
}

Code compiles but isn't tested...

It's not an easy problem to decode Morse code.

It is quite easy but not trivial.
I did it in hardware ( no processor involved ) in the late 60's.

If you assign a blank byte, that is a byte with no Morse code in it as 0000 0001 then shift in from the right a 0 for dot and a 1 for dash then you will always have a unique number for each character that you can apply to your translation look up table.

Just looked in the data sheet of a photo resistor, and the 10 ms bit width might be too fast. Something in the range of 50 - 100 ms will probably be more reliable. I changed the previous post.

Just an FYI:

It's not an easy problem to decode Morse code.

It's actually pretty easy.

My Arduino code for both Nokia and 2x16 LCD:
http://www.hackster.io/rayburne/magic-morse-on-arduino

Ray

Added:
Numeric digits are very easy with a lightweight algorithm:
http://www.instructables.com/id/Orb-Flashes-Temperature-in-Morse-Code/