#include <LiquidCrystal.
h>
// Pin definitions
const int greenLedPin = 2; // Green LED
const int redLedPin = 3; // Red LED
const int blueLedPin = 4; // Blue LED
const int buzzerPin = 5; // Buzzer
const int analogInput1 = A0; // For board insertion
const int analogInput2 = A1; // For SW1
const int analogInput3 = A2; // For SW2
// Duration constants
const int buzzerOkDuration = 100; // OK sound duration (in milliseconds)
const int buzzerErrorDuration = 100; // Error sound duration (in milliseconds)
const int ledDuration = 100; // LED duration (in milliseconds)
int cycleCount = 1; // Initialize cycle count
int totalBoardsMeasured = 0; // Count total boards measured
bool allChecksOk; // Track overall status for the cycle
String resultA0 = ""; // Store result for A0
String resultA1 = ""; // Store result for A1
String resultA2 = ""; // Store result for A2
// Count successful and error checks
int countA0Ok = 0, countA0Error = 0;
int countA1Ok = 0, countA1Error = 0;
int countA2Ok = 0, countA2Error = 0;
// Initialize the LCD for 20x4
LiquidCrystal lcd(12, 11, 10, 9, 8, 7); // Adjust pin numbers as necessary
void setup() {
// Set pin modes for LEDs and buzzer
pinMode(greenLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
pinMode(blueLedPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Initialize Serial communication and LCD
Serial.begin(9600);
lcd.begin(20, 4); // Set dimensions for 20x4 LCD
// Turn off LEDs and buzzer initially
resetIndicators();
void loop() {
allChecksOk = true; // Reset status for the new cycle
// Step 1: Insert the board
blueLedOn("Please insert the board");
waitForVoltage(analogInput1, checkVoltageA0);
// Step 2: Press SW1
blueLedOn("Press SW1");
waitForVoltage(analogInput2, checkVoltageA1);
// Step 3: Press SW2
blueLedOn("Press SW2");
waitForVoltage(analogInput3, checkVoltageA2);
// Indicate readiness for repeating the cycle
resetIndicators();
// Increment total boards measured
totalBoardsMeasured++;
// Display the current board number and total boards measured on the LCD
lcd.clear();
lcd.print("Board: ");
lcd.print(cycleCount); // Using cycleCount to represent the board number
lcd.setCursor(0, 1); // Move to the second line
lcd.print("Total Boards: ");
lcd.print(totalBoardsMeasured); // Display total boards measured
delay(2000); // Show the board number for 2 seconds
// Update the counts based on results
updateCounts();
// Report individual results
displayResults();
// Increment cycle count after each complete cycle
cycleCount++;
// Wait for detachment of A0, A1, A2 before starting the next cycle
waitForDetachment();
// Continuously update the LCD with board status
updateLCDStatus();
void resetIndicators() {
digitalWrite(greenLedPin, LOW);
digitalWrite(redLedPin, LOW);
digitalWrite(blueLedPin, LOW);
noTone(buzzerPin); // Turn off buzzer
void blueLedOn(String message) {
digitalWrite(blueLedPin, HIGH);
lcd.clear();
lcd.print(message);
Serial.println(message);
void waitForVoltage(int analogPin, void (*checkFunction)(float)) {
String checkingMessage;
// Set appropriate message based on the analog pin
if (analogPin == analogInput1) {
checkingMessage = "Please insert the board";
} else if (analogPin == analogInput2) {
checkingMessage = "Press SW1";
} else if (analogPin == analogInput3) {
checkingMessage = "Press SW2";
lcd.clear();
lcd.print(checkingMessage);
Serial.println(checkingMessage);
delay(10); // Short delay for stabilization
while (true) {
float voltage = readVoltage(analogPin);
// Wait for valid voltage detection based on the pin
if ((analogPin == analogInput1 && voltage >= 1.0 && voltage <= 3.0) ||
(analogPin == analogInput2 && voltage < 0.1) ||
(analogPin == analogInput3 && voltage < 0.1)) {
break; // Proceed when a valid voltage is detected
delay(300); // Short delay to reduce CPU usage
digitalWrite(blueLedPin, LOW); // Turn off blue LED
tone(buzzerPin, 1000, 200); // Beep buzzer for 200ms
delay(10); // Short pause after beep
lcd.clear();
lcd.print("Checking...");
float voltage = readVoltage(analogPin); // Get the final voltage reading
Serial.print("Measured Voltage: ");
Serial.println(voltage);
checkFunction(voltage); // Call the appropriate check function
float readVoltage(int analogPin) {
float totalVoltage = 0.0;
for (int i = 0; i < 5; i++) { // Take 5 readings for averaging
int analogValue = analogRead(analogPin);
totalVoltage += (analogValue * 5.0) / 1023.0; // Convert to voltage
delay(3); // Short delay between readings
return totalVoltage / 5; // Return average voltage
void checkVoltageA0(float voltage) {
// Check for initial valid voltage within the range
if (voltage >= 1.0 && voltage <= 3.0) {
// Delay before re-measurement
delay(300);
// Measure voltage again
float newVoltage = readVoltage(analogInput1);
Serial.print("Re-measured Voltage A0: ");
Serial.println(newVoltage);
if (newVoltage >= 1.0 && newVoltage <= 3.0) {
digitalWrite(greenLedPin, HIGH);
tone(buzzerPin, 2000, buzzerOkDuration); // Single OK sound
delay(buzzerOkDuration);
resultA0 = "OK after wait"; // Store new result
} else {
digitalWrite(redLedPin, HIGH);
tone(buzzerPin, 400, buzzerErrorDuration); // Error sound
delay(buzzerErrorDuration);
resultA0 = "Error after wait"; // Store new result
allChecksOk = false; // Mark error
} else {
digitalWrite(redLedPin, HIGH);
tone(buzzerPin, 400, buzzerErrorDuration); // Error sound
delay(buzzerErrorDuration);
resultA0 = "Error"; // Store result
allChecksOk = false; // Mark error
delay(ledDuration);
resetIndicators();
void checkVoltageA1(float voltage) {
// Check if voltage is less than 0.1 immediately mark as OK
if (voltage < 0.1) {
digitalWrite(greenLedPin, HIGH);
tone(buzzerPin, 2000, buzzerOkDuration); // Single OK sound
delay(buzzerOkDuration);
resultA1 = "OK"; // Store result
countA1Ok++; // Increment OK count
} else {
digitalWrite(redLedPin, HIGH);
tone(buzzerPin, 400, buzzerErrorDuration); // Error sound
delay(buzzerErrorDuration);
resultA1 = "Error"; // Store result
countA1Error++; // Increment Error count
allChecksOk = false; // Mark error
delay(ledDuration);
resetIndicators();
void checkVoltageA2(float voltage) {
// Check if voltage is less than 0.1 immediately mark as OK
if (voltage < 0.1) {
digitalWrite(greenLedPin, HIGH);
tone(buzzerPin, 2000, buzzerOkDuration); // Single OK sound
delay(buzzerOkDuration);
resultA2 = "OK"; // Store result
countA2Ok++; // Increment OK count
} else {
digitalWrite(redLedPin, HIGH);
tone(buzzerPin, 400, buzzerErrorDuration); // Error sound
delay(buzzerErrorDuration);
resultA2 = "Error"; // Store result
countA2Error++; // Increment Error count
allChecksOk = false; // Mark error
}
delay(ledDuration);
resetIndicators();
void updateCounts() {
if (allChecksOk) {
countA0Ok++;
} else {
countA0Error++;
void displayResults() {
lcd.clear(); // Clear the display
lcd.print("A0: "); lcd.print(resultA0);
lcd.setCursor(0, 1); // Move to the second line
lcd.print("A1: "); lcd.print(resultA1);
lcd.setCursor(0, 2); // Move to the third line
lcd.print("A2: "); lcd.print(resultA2);
void updateLCDStatus() {
lcd.setCursor(0, 3); // Move to the fourth line
lcd.print("A0: "); lcd.print(countA0Ok); lcd.print(" OK "); lcd.print(countA0Error); lcd.print(" Err");
lcd.setCursor(10, 3); // Move to the end of the fourth line
lcd.print("A1: "); lcd.print(countA1Ok); lcd.print(" OK "); lcd.print(countA1Error); lcd.print(" Err");
void waitForDetachment() {
while (true) {
// Continuously check the voltage on A0, A1, A2
float voltageA0 = readVoltage(analogInput1);
float voltageA1 = readVoltage(analogInput2);
float voltageA2 = readVoltage(analogInput3);
// Check if A0 has returned to pull-down value (close to 0V)
// and A1 and A2 have returned to pull-up value (close to 5V)
if (voltageA0 < 0.1 && voltageA1 > 4.0 && voltageA2 > 4.0) {
lcd.clear();
lcd.print("Ready for next");
delay(500); // Brief pause before starting the next cycle
break; // Exit the loop to start the next cycle
delay(100); // Short delay to reduce CPU usage