Converting analog data from a photo-detector to its ASCII code

Greetings,

I am working on a project where I have an LED as a transmitter and Photodetector as a receiver. I am sending a text that is encoded using serial.println() function and enables LED flicking according to the transmitted text ASCII code. At the receiver side, I am connecting a photodetector to an analog pin A0, and I want to receive the text that was transmitted

Using this code, we sent a text from PC (A) to PC(B) without any components to test it, and it worked well, but I don't know how to adjust it to be able to receive using a photodetector

TX: (Arduino MEGA)

void setup() {
  // put your setup code here, to run once:
Serial.begin (115200);
Serial1.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
    Serial.println("Hello");
    
    Serial1.println("Hello world");
    delay(1000);
}

Rx: (Arduino UNO)

#include <SoftwareSerial.h>
SoftwareSerial mySerial(a0, 11); // RX, TX

void setup() {
  // put your setup code here, to run once:
Serial.begin (115200);
mySerial.begin(9600);
}

void loop() { // run over and over
  if (mySerial.available()) {
    Serial.write(mySerial.read());
  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }
}

I used this code to read from the receiving pin, and it will take the ascii code and convert it back to the original text. I, however, noticed that when I run this code I am not receiving anything.

In my attempt to address the issues of the above code, I tried the below code of analog read example form Arduino references:

int analogValue = 0;    // variable to hold the analog value

void setup() {
  // open the serial port at 9600 bps:
  Serial.begin(115200);
}

void loop() {
  // read the analog input on pin 0:
  analogValue = analogRead(A0);
  // print it out in many formats:
  Serial.println(analogValue);       // print as an ASCII-encoded decimal
  Serial.println(analogValue, BIN);  // print as an ASCII-encoded binary
  //delay(10);
}

Using this code I was able to read something in my serial monitor (the sensed value and its binary representation). However, I noticed that only transmitted (bit 1) is received.

Thanks for your help in advance

If you're transmitting at 9600 bits per second, the sampling using analogRead isn't going to work, because you're effectively sampling at the same rate.

Try slowing down the transmit to maybe 300 bits per second, and cut out the printing on every cycle.

I changed the baud rate to 300 at the transmitter, I kept it the same (as it was) at the receiver, but I still can't receive anything other than high

void setup() {
  // put your setup code here, to run once:
Serial.begin (115200);
Serial1.begin(300);
}

void loop() {
  // put your main code here, to run repeatedly:
    //Serial.println("Hello");
    Serial1.println("hi");
    delay(1000);
}

However, I noticed that only transmitted (bit 1) is received.

How did you figure that?
How do you know it isn't bit 0?

I am always receiving the sensed value as 7, which means high. When I turn off the LED, the sensed value is 0.

Try to think like a UART - detect when the start bit begins (continuous sampling), wait half a bit-period and sample again; if it is still a start bit condition, sample the data bits by waiting whole bit periods, otherwise go back to the idle state.

This Thread about Serial Infra Red may be of interest.

...R