How to read binary correctly?

I have a project that consists of 2 Arduinos as a transmitter and a receiver. I have no problem at the transmitter Arduino, but my problem is at the receiver, where I get input as a binary sequence of 4 bits and translate them using an if-else statement. The problem is that when reading the binary at the if-else statement, I have interference between the second and first statements; when Orange LED is supposed to be HIGH, I have Orange and Red alternating between HIGH and LOW; this also happens when Yellow is supposed to be HIGH; I have Yellow and Green alternating between HIGH and LOW. I figured that this happens because the first bit at the orange and red are both 0, and at the yellow and green are both 1. But I don't know how to solve this problem, and read the whole 4 bits before making a decision? Help please.

const int RED_LED_PIN = 2;
const int ORANGE_LED_PIN = 3;
const int YELLOW_LED_PIN = 4;
const int GREEN_LED_PIN = 5;
const int INPUT_PIN = 8;

void setup() {
  pinMode(RED_LED_PIN, OUTPUT);
  pinMode(ORANGE_LED_PIN, OUTPUT);
  pinMode(YELLOW_LED_PIN, OUTPUT);
  pinMode(GREEN_LED_PIN, OUTPUT);
  pinMode(INPUT_PIN, INPUT);
}

void loop() {
  byte receivedData = 0 ;

  for (int i = 0; i < 4; i++) {
    receivedData |= digitalRead(INPUT_PIN) << i;
    delay(10);
  }

  digitalWrite(RED_LED_PIN, LOW);
  digitalWrite(ORANGE_LED_PIN, LOW);
  digitalWrite(YELLOW_LED_PIN, LOW);
  digitalWrite(GREEN_LED_PIN, LOW);

if (receivedData == 0b0000) {
  digitalWrite(RED_LED_PIN, HIGH);
  digitalWrite(ORANGE_LED_PIN, LOW);
  digitalWrite(YELLOW_LED_PIN, LOW);
  digitalWrite(GREEN_LED_PIN, LOW);
} 
else if (receivedData == 0b0110) {
  digitalWrite(RED_LED_PIN, LOW);
  digitalWrite(ORANGE_LED_PIN, HIGH);
  digitalWrite(YELLOW_LED_PIN, LOW);
  digitalWrite(GREEN_LED_PIN, LOW);
} 
else if (receivedData == 0b1001) {
  digitalWrite(RED_LED_PIN, LOW);
  digitalWrite(ORANGE_LED_PIN, LOW);
  digitalWrite(YELLOW_LED_PIN, HIGH);
  digitalWrite(GREEN_LED_PIN, LOW);
} 
else if (receivedData == 0b1111) {
  digitalWrite(RED_LED_PIN, LOW);
  digitalWrite(ORANGE_LED_PIN, LOW);
  digitalWrite(YELLOW_LED_PIN, LOW);
  digitalWrite(GREEN_LED_PIN, HIGH);
}

  delay(100);
}

Do you have a 10k pulldown resistor connected from pin 8 to GND to keep the pin from floating HIGH, LOW, HIGH, LOW when the button is not pressed?

No, i didn't use any resistor between pin 8 to GND before, but after your comment i just did and nothing happens. FYI I'm using Tinkercad.

Why did you create the problem when standard serial communications is available for both Arduinos.

I don't know how to measure the pulses length actually.

Here is the transmitter code:

const int TRIGGER_PIN = 12;
const int ECHO_PIN = 11;
const int OUTPUT_PIN = 10;

void setup() {
  pinMode(TRIGGER_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(OUTPUT_PIN, OUTPUT);
}

void loop() {
  digitalWrite(TRIGGER_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER_PIN, LOW);

  long duration = pulseIn(ECHO_PIN, HIGH);
  int distance = duration * 0.034 / 2;
  int percentage = map(distance, 30, 200, 100, 0);
  percentage = constrain(percentage, 0, 100);

  byte binaryOutput;

  if (percentage >= 0 && percentage <= 9) {
    binaryOutput = 0b0000;
  } else if (percentage >= 10 && percentage <= 24) {
    binaryOutput = 0b0110;
  } else if (percentage >= 25 && percentage <= 49) {
    binaryOutput = 0b1001;
  } else if (percentage >= 50 && percentage <= 100) {
    binaryOutput = 0b1111;
  }

  for (int i = 0; i < 4; i++) {
    digitalWrite(OUTPUT_PIN, bitRead(binaryOutput, i));
    delay(10);
  }

  delay(100);
}

Because I will modulate and transmit the output through a channel from the first Arduino until it reaches the second, that's why I can't use conventional methods.

looks like you want to read a 4 bit sequence.
i'll assume you expect bits to be 10 msec apart

how do you know when the sequence starts? is there a start bit what is always the opposite state of the input than the state of the start bit?

how do you synchronize receiving data to the transmission data? do you repidly (more frequently than every 10 msec) to recognize the start bit and then delay for 5 msec to read the first bit and then delay by 10 msec for every following bit?

isn't there a device on the chip that alread does this?

I didn't write the code myself; I'm so bad at writing codes, I have copied it from ChatGPT.

I'm trying to build two separate devices connected by a powerline cable, the output from the transmitter Arduino will be received at the receiver Arduino.

In the Tinkercad simulation, I have simply connected the output from the transmitter to the input at the receiver without going into the details of the modulation, channel, and de-modulation process.

You're absolutely right! But unfortunately I can't drop it since it's a part of my senior design project and I didn't realize this problem until now.

Yes

i'm curious, what is the modulated signal injected onto the power line?

Actually, it's not really injected; it'll be ASK modulated and added using an op-amp with the power signal to the channel as a start. Injection into the powerline is the next step for the project.

Thank you for your advice, I'll stop and cover the basics if it will give me better results than this method. Really appreciate your help.

I have tested the modulation and de-modulation of a random binary signals using ASK modulation through a powerline with 60Hz power running through it and I have succeeded. This is not a problem at all now. It's true that I have faced some problems injecting the message into the powerline but I have changed my plan to add it with the power line for a start, and then I will look more into the actual injection of the message.
Also, thank you for your advice about X-10.

I understand you, thank you so much for your help.

why can't you use a tone detector on the receive side and feed it's output into the RX of a UART and similarly use a tone generator on the transmit side driven by the TX side of a UART on the chip?

If @dhary-1 can reliably see bits on both sides, why bother using a tone generator and detector?

Or is this already part of what is going on:

@dhary-1 please elaborate on your power line modulator/demodulator scheme. As suggested, if you have a power line modem, in effect, the easy thing to do is use serial comms between the Arduinos.

a7

I don't think I need a tone detector and generator, but UART might work since my problem is receiving the binary sequence and getting back each single bit alone to determine the state of each LED.

you said amplitude shift keying (ASK) modulation

ask_modulated_waveform