I have changed to using standard LEDs, one green and one red for each sensor but they're lighting up on their own.
Here is the code:
#include <LiquidCrystal.h>
// Define LCD pins
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
// Define pins for light sensors
const int sensor1Pin = A0;
const int sensor2Pin = A1;
const int sensor3Pin = A2;
// Define pins for RGB LEDs (only red and green)
const int led1RedPin = 8;
const int led1GreenPin = 9;
const int led2RedPin = 10;
const int led2GreenPin = 11;
const int led3RedPin = 12;
const int led3GreenPin = 13;
void setup() {
Serial.begin(9600);
// Initialize LCD
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("802");
// Set LED pins as outputs
pinMode(led1RedPin, OUTPUT);
pinMode(led1GreenPin, OUTPUT);
pinMode(led2RedPin, OUTPUT);
pinMode(led2GreenPin, OUTPUT);
pinMode(led3RedPin, OUTPUT);
pinMode(led3GreenPin, OUTPUT);
}
void loop() {
// Read values from light sensors
int sensor1Value = analogRead(sensor1Pin);
int sensor2Value = analogRead(sensor2Pin);
int sensor3Value = analogRead(sensor3Pin);
// Check if all light sensor readings are above a certain threshold
if (sensor1Value > 50 && sensor2Value > 50 && sensor3Value > 50) {
// Check the sequence of sensor readings
if (sensor1Value < sensor2Value && sensor2Value < sensor3Value) {
// Correct sequence detected, turn LEDs green
setRGB(led1RedPin, led1GreenPin, 0, 255); // RGB values for green
setRGB(led2RedPin, led2GreenPin, 0, 255);
setRGB(led3RedPin, led3GreenPin, 0, 255);
// Display "802" on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("802");
} else {
// Incorrect sequence detected, turn respective LED red
if (sensor1Value >= sensor2Value) {
setRGB(led1RedPin, led1GreenPin, 255, 0); // RGB values for red
} else {
// Turn off LED 1 if the condition is not met
setRGB(led1RedPin, led1GreenPin, 0, 0);
}
if (sensor2Value >= sensor3Value) {
setRGB(led2RedPin, led2GreenPin, 255, 0);
} else {
// Turn off LED 2 if the condition is not met
setRGB(led2RedPin, led2GreenPin, 0, 0);
}
if (sensor3Value <= sensor1Value) {
setRGB(led3RedPin, led3GreenPin, 255, 0);
} else {
// Turn off LED 3 if the condition is not met
setRGB(led3RedPin, led3GreenPin, 0, 0);
}
// Clear LCD
lcd.clear();
}
} else {
// Turn off all LEDs if light levels are too low
setRGB(led1RedPin, led1GreenPin, 0, 0); // Turn off LED 1
setRGB(led2RedPin, led2GreenPin, 0, 0); // Turn off LED 2
setRGB(led3RedPin, led3GreenPin, 0, 0); // Turn off LED 3
// Clear LCD
lcd.clear();
}
delay(1000);
}
// Function to set RGB values for an LED
void setRGB(int redPin, int greenPin, int redValue, int greenValue) {
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
}