Need help coding a puzzle

Hi,

For one of my projects, I have to design a puzzle that leads into another within my group but I'm having a hard time with it. My puzzle is three light sensors are arranged so that they have to sense light in a certain order (231) and when the correct one is revealed, it lights an LED up with green (I'm using RGB LEDs) and if they reveal the wrong one, it lights up with red and until the sensor registers that the light level is low enough. Once the correct sequence is made, a display reveals the password for the next puzzle. I've managed to get the display to work but I'm having a really hard time with the breadboard, can anyone help me? - I'm currently using the Mega 2560 The Most Complete Starter Kit if that helps.

Thanks to anyone who can help me.

  • Show us the sketch you have made thus far.

  • Show us the schematic for this project and good images of the actual wiring.

There you go, I haven't done any sketches though.

Are they common anode or common cathode?

If they are common cathode, you need to connect Arduino pins to the red and green anodes and connect the cathode to ground. When you make one of the Arduino pins HIGH, the led will light up.

But if they are common anode, you need to connect Arduino pins to the cathodes and connect the anode to 5V. When you make one of the Arduino pins LOW, the led will light up.

By the way, you only need 1 resistor, connected to the common cathode or anode pin, because you will only ever light the led either red or green, not both at the same time (which would be yellow, but that would not work without a second resistor).

I'm unsure if they're common anode or cathode. It doesn't say with the pack I bought.

Then that's the first thing to figure out. Do you need help doing that?

I would definitely appreciate the help. I can't tell with the RGB ones.

Ok, I have have access to a search engine called Google. Here you go:

How do RGB LEDs work? | Random Nerd Tutorials.

  • Wiring for CA and CC for an RGB LED.

image

Bed time here.

:yawning_face:

Do you have a multi meter?

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);
}

This will report how much light falls on the sensor. Not when the light falls on the sensor...

I was using it to see if the sensors were working, that's my bad

@hyperion112 ,

As you flagged this saying that you no longer need it I have closed it so people don't waste time providing answers you don't want. If this is incorrect please send me a PM to clarify.

Thank you