// Define the pin for the LDR sensor
const int ldrPin = A0;
// Threshold value for distinguishing between light and dark
const int threshold = 490;
// Variable to store received binary data
String receivedData = "";
void setup() {
// Initialize serial communication
Serial.begin(9600);
}
void loop() {
// Read the value from the LDR sensor
int ldrValue = analogRead(ldrPin);
// Check if the received light intensity is above the threshold
if (ldrValue > threshold) {
// Light detected, consider it as a binary '1'
receivedData += "1";
} else {
// No light detected, consider it as a binary '0'
receivedData += "0";
}
// Check if a complete byte has been received (assuming 8-bit data)
if (receivedData.length() >= 8) {
// Convert the received binary data to a character and print it
char receivedChar = strtol(receivedData.c_str(), NULL, 2);
Serial.print(receivedChar);
// Clear the received data buffer for the next byte
receivedData = "";
}
}