#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int sendingPins[5] = {2, 3, 4, 5, 6}; // Pins for sending signals
const int receivingPins[5] = {8, 9, 10, 11, 12}; // Pins for receiving signals
const int buttonPin = A0; // Button pin
const int redLedPin = 7; // Red LED pin
const int greenLedPin = 13; // Green LED pin
bool buttonPressed = false; // Flag to track if the button is pressed
bool pinsReceived[5] = {false}; // Array to mark received pins
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
Serial.begin(115200);
// Initialize the LCD
lcd.init();
// Turn on the backlight
lcd.backlight();
// Set sending pins as OUTPUT
for (int i = 0; i < 5; i++) {
pinMode(sendingPins[i], OUTPUT);
}
// Set receiving pins as INPUT_PULLUP
for (int i = 0; i < 5; i++) {
pinMode(receivingPins[i], INPUT_PULLUP);
}
// Set button pin as INPUT_PULLUP
pinMode(buttonPin, INPUT_PULLUP);
// Set LED pins as OUTPUT
pinMode(redLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
// Display the initial message on the LCD
lcd.setCursor(0, 0);
lcd.print("Press the button");
}
void loop() {
// Check if button A0 is pressed
if (digitalRead(buttonPin) == LOW && !buttonPressed) {
// Set the flag to indicate that the button has been pressed
buttonPressed = true;
int numReceived = 0; // Counter for received pins
// Loop through each sending pin
for (int i = 0; i < 5; i++) {
// Send signal from the current sending pin
digitalWrite(sendingPins[i], LOW);
// Loop through each receiving pin and check its value
for (int j = 0; j < 5; j++) {
// Read the value of the receiving pin
int receivedValue = digitalRead(receivingPins[j]);
// If the pin receives the signal (LOW), mark it as received
if (receivedValue == LOW) {
pinsReceived[j] = true;
numReceived++; // Increment the counter for received pins
}
}
// Turn off signal on the current sending pin
digitalWrite(sendingPins[i], HIGH);
// Delay for a short duration before the next iteration
delay(100); // Adjust delay duration as needed
}
// Check if all pins are received
bool allReceived = true;
for (int i = 0; i < 5; i++) {
if (!pinsReceived[i]) {
allReceived = false;
break;
}
}
// Display the result on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Received: ");
lcd.print(numReceived);
lcd.setCursor(0, 1);
// Control LEDs based on the connection status
if (allReceived) {
digitalWrite(greenLedPin, HIGH); // Turn on green LED
digitalWrite(redLedPin, LOW); // Turn off red LED
lcd.print("Connection OK");
} else {
digitalWrite(greenLedPin, LOW); // Turn off green LED
digitalWrite(redLedPin, HIGH); // Turn on red LED
lcd.print("M: ");
for (int i = 0; i < 5; i++) {
if (!pinsReceived[i]) {
// Display pin number as 1 to 5 instead of 8 to 12
lcd.print(5 - i);
lcd.print(" ");
}
}
}
// Reset pinsReceived array and button press flag for next button press
for (int i = 0; i < 5; i++) {
pinsReceived[i] = false;
}
// Reset the button press flag when the button is released
buttonPressed = false;
}
}