Convert user input (string) into binary array to transmit over LED?

I have a project about Visible Light Communication. I need to get user input (e.g sentence) and convert this sentence into binary values so that I can turn on-off the LED.

So far I managed to transmit data but i am having issue with how to convert this string into binary.

One of the ideas that i came up with is to convert this string into 1D array with 1s and 0s. Each 8 binary element in the array represents the ascii equvialant. However i couldnt manage to achieve this task.

if you guys give me another method to complete this, it'll be greatly appriciated.

I'm sure there is a better way of doing this, but i couldnt find it so far.

thanks in advance :slight_smile:

Each 8 binary element in the array represents the ascii equvialant.

ASCII equivalent of what? Of one character in the string?

However i couldnt manage to achieve this task.

A character is already in binary. Reading the bits using bitRead() is trivial.

Not sure what you are trying to do? Characters are already binary. Just blink an LED from the ASCII string bits or actually transmit these characters over visible light with an LED as a transmitter and a photo transistor or diode as a receiver?

The example below does a transmission somewhat “MORSE” code like with short flashes for zero’s and long flashes for one’s, an extra delay between characters to help “re-sync” if needed, and a 1sec delay after the string to signal completion. Your receiver will need to time for all the differences, set bits for each character and rebuild the string. The timings are all just arbitrary and you could set them as fast as possible. Plus this is all just off the top of my head so no guarantees.

You might also just try using the soft serial library. Good Luck.

void setup() {
  // put your setup code here, to run once:
  pinMode(13,OUTPUT);
}

void loop() {
  
  flashString("Hellow World");
  
  while(1);  //stop loop
}

// Routine to flash led from string bits right to left
// 100ms flash for zero
// 200ms flash for one
// 200ms off between flash
void flashString(char *string) {
  int i;
  while(*string) {
    for (i=7;i>=0;--i) {
          digitalWrite(13,1);    //Turn on the LED
          if (*string & 1<<i) {  //test bit (1 shifted left i times)
            delay(100);          //short 100ms flash for zero   
          } else {
            delay(200);          //long 200ms flash for one
          }
          digitalWrite(13,0);    //Turn off the LED
          delay(300);            //200 ms space between bits 
     }
     delay(200);                 //300+200ms delay between characters
     *string++;                  //next character of string
  }
  delay(1000);
}