converting text to binairy with light

hello ,
Thank you to everyone who helped me I have solved the previous problem of decoding the data received by the light, but I got another problem Come on that I can only send one or two characters only and I want to send text where is the problem Thank you

#include <SoftwareSerial.h>

const char LED = 13; // the transmitter
const char LDR = 3; //the ldr sensor
String msg_codee = ""; //a binary string 010101 ... corresponding to the encoded message from the pc and sent to the optical channel


void setup() {
  Serial.begin(9600);//opens serial port, sets data rate to 9600 bps

  pinMode(LDR, INPUT_PULLUP);
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);
  Serial.write(" ready for a message ");
  Serial.println("  ");
}
void loop() {     //reply only when you receive data:


  coding();  //transform the message received from pc into a binary sequence
  emission();  //use this binary suite to control the optical channel
  //decoding(); for ir half or full deplex
 }

//********************************** coding *********************************

void coding() {
  while (Serial.available()) // read the incoming byte:
  {
    String character = String(Serial.read(), BIN); // transforms each character of the message by its ASCII code in binary
    while (character.length() < 8)                 // as long as the character encoded in binary is not 8 bits,
    {
      character = String(0) + character;  //then complete with '0' at the beginning to make 8 bits = one byte
    }
    Serial.print(" msg on BIN[");
    Serial.print(character);
    Serial.println("]");
    msg_codee += character;
    //   while (true) {      continue;    } msg_codee++;    // go on to the next character
 
  }
}// end of the coding

/*############################################ EMISSION ###########################################*/
void emission() { // read the incoming byte:

 if (msg_codee.length() > 0)  {
    msg_codee = String("11111111") + msg_codee + String("00000000"); // for start and stop bit like                       the attach pic below show

   
    
    Serial.print("the message coded is [");    Serial.print(msg_codee);    Serial.println("]");
    /************************************************************************/
    int msglength = msg_codee.length(); //to go through the binary chain
    int i = 0;
    while (i < msglength) { //

      
     if (msg_codee[i] == '1') // Whenever I hit a '1' bit,

      {
        digitalWrite(LED, HIGH);
        delay(200);         //then I turn on the led light for 200 microseconds
      }
      if (msg_codee[i] == '0')      { // Whenever I hit a '0' bit,
        digitalWrite(LED, LOW); delay(200); //then I turn off the led light for 200 microseconds
      }
      digitalWrite(LED,0);
      i++ ;
    }
    msg_codee = "";
 }  }
 // at the end of the emission

study this, you might get some ideas:

void setup() {
  Serial.begin(115200);
}

void loop() {
  int c = Serial.read();
  if (c != -1) { // if we have received a byte
    Serial.print((char) c);
    Serial.print(F("\t0x"));
    Serial.print((byte) c, HEX);
    Serial.print(F("\t0b"));
    for (char aBit = 7; aBit >= 0; aBit--) {
      Serial.print(bitRead(c, aBit));
    }
    Serial.print(F("\t"));
    for (char aBit = 7; aBit >= 0; aBit--) {
      if (bitRead(c, aBit)) Serial.write('*'); else Serial.write('_');
    }
    Serial.println();
  }
}

--> Open the Serial console at 115200 bauds, no end line, and type something like "HELLO WORLD" and enter, you should see this:

[color=purple]H 0x48    0b01001000  _*__*___
E   0x45    0b01000101  _*___*_*
L   0x4C    0b01001100  _*__**__
L   0x4C    0b01001100  _*__**__
O   0x4F    0b01001111  _*__****
    0x20    0b00100000  __*_____
W   0x57    0b01010111  _*_*_***
O   0x4F    0b01001111  _*__****
R   0x52    0b01010010  _*_*__*_
L   0x4C    0b01001100  _*__**__
D   0x44    0b01000100  _*___*__
[/color]

@J-M-L Thanks, I want to continue working on my program and debug it. I don't want to try other programs and get distracted

OK. studying code usually opens the mind to new ideas. your code is really over-engineered you don't need alternative String representation with 0 and 1 when you have them already within the message characters and can read them directly (but at least it's your code and that's fine)

I got another problem Come on that I can only send one or two characters only and I want to send text where is the problem

what do you mean by this? what do you see that you don't want?

One problem you have is in the way you handle the incoming Serial buffer. Your baud rate is 9600. A character is represented by 10 bits (give or take) so you get roughly 960 characters per second.

But your microprocessor is super fast, and while the data is still coming in, you are speeding through the while (Serial.available()) {...}loop in your coding() function and you empty the buffer before everything has been transmitted from the host computer, and you'll likely only get a few chars

if you move to 115200 bauds, you'll see less of the problem - but it's fundamentally still there..as food for thoughts, I would also suggest to study Serial Input Basics to handle this

also abusing the String class is not a good idea.

J-M-L: OK. studying code usually opens the mind to new ideas. your code is really over-engineered you don't need alternative String representation with 0 and 1 when you have them already within the message characters and can read them directly (but at least it's your code and that's fine) what do you mean by this? what do you see that you don't want?

Our experiment aims to illustrate the coding and the transmission of data between devices via light. During this experiment, we can type a message (text) on the keyboard of a computer, this The message will then be encoded and transmitted to another computer using a laser. To code messages in term of light pulses, we have confined ourselves to the simplest example: the laser can take two states to transmit the message, the state turned on and the state turned off. To encode the text typed in one succession of flashing laser, we used the ASCII table. Computers can only handle numbers (no letters or characters), when they display a character, they consider this character as a number of its own. The ASCII table contains all the characters as well as their "number equivalent", this number that can be expressed in the decimal, binary, ... It's the equivalent in binary base that we will use to code our characters: indeed, a number in binary consists only of 0 and 1, we can therefore translate the 1 of the number into binary by the lit state of the laser and the 0s by the off state.

any help :confused:

code is still in post #1

when I print a [color=purple]1[/color] or a [color=purple]*[/color], turn your laser on, when I print a [color=purple]0[/color] or a [color=purple]-[/color], have the laser off…