Newbie in need of help!

Hey guys, I'm trying to build an escape rooms for my friends. I have a box with 5 potentiometers. I'm looking to have an orange light blink constantly. When the knobs are in the wrong order i want a red light on, when the knobs are correct I want the red to shut off and a green to go on. I've watched tutorials and used AI to help but I can't get the thing to work.

// --- Pin Assignments ---
const int potPins[5] = {A0, A1, A2, A3, A4}; // Potentiometers
const int orangeLED = 2; // Always blinks
const int redLED = 3; // ON when any knob is wrong
const int greenLED = 4; // ON only when all knobs are correct

// --- Target values for each knob ---
const int targets[5] = {300, 512, 700, 400, 600}; // Adjust per puzzle
const int tolerance = 40;

/*
Knob Positions:
- Knob 1 (A0): Target = 300 β†’ Acceptable: 260–340 (~1/3 turn)
- Knob 2 (A1): Target = 512 β†’ Acceptable: 472–552 (centered)
- Knob 3 (A2): Target = 700 β†’ Acceptable: 660–740 (~2/3 turn)
- Knob 4 (A3): Target = 400 β†’ Acceptable: 360–440 (~40% turn)
- Knob 5 (A4): Target = 600 β†’ Acceptable: 560–640 (~just past center)
*/

void setup() {
pinMode(orangeLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
Serial.begin(9600);
}

void loop() {
digitalWrite(orangeLED, HIGH);
delay(200);
digitalWrite(orangeLED, LOW);
delay(200);

bool allCorrect = true;

for (int i = 0; i < 5; i++) {
int val = analogRead(potPins[i]);
Serial.print("Knob ");
Serial.print(i);
Serial.print(": ");
Serial.println(val);

if (abs(val - targets[i]) > tolerance) {
allCorrect = false;
}
}

if (allCorrect) {
digitalWrite(greenLED, HIGH);
digitalWrite(redLED, LOW);
} else {
digitalWrite(greenLED, LOW);
digitalWrite(redLED, HIGH);
}
}

And this is how I wired it:

POTENTIOMETER WIRING (REPEAT FOR ALL 5)


Pot Pin β†’ Connect To

Left leg β†’ GND rail (blue)

Middle leg β†’ A0–A4 (respectively)

Right leg β†’ 5V rail (red)

Assignments:

  • Pot 1 β†’ A0

  • Pot 2 β†’ A1

  • Pot 3 β†’ A2

  • Pot 4 β†’ A3

  • Pot 5 β†’ A4

LED WIRING


Orange LED (Blinking):

  • Long leg (anode) β†’ D2

  • Short leg β†’ Resistor β†’ GND rail

Red LED (Wrong):

  • Long leg β†’ D3

  • Short leg β†’ Resistor β†’ GND rail

Green LED (Correct):

  • Long leg β†’ D4

  • Short leg β†’ Resistor β†’ GND rail

Anyone know what I'm doing wrong?

In the future use the IDE Tools / Auto Format before posting code.
The easiest way to do your project is to use a state machine. Look under Projects/Tutorials then read a few of the threads about State Machines.

What is it not doing? Instead of being greater than the tolerance, it should be between +- the tolerance, this way you have some wiggle room.

Can you be more specific.
What does you code not do that you want it to do?
What does your code do that you do not want it to do?

Does the Orange led blink?
Are the analog read values reasonable?

No where in the long list did you tell us the resistance value of your potentiometers. Are they all the same value?

How have you defined the "Right Order" and "Wrong Order" of the knobs?

"Right Order" means: All the knobs have the foloowing positions in terms of ADC counts?

Knob Positions:
- Knob 1 (A0): Target = 300 β†’ Acceptable: 260–340 (~1/3 turn)
- Knob 2 (A1): Target = 512 β†’ Acceptable: 472–552 (centered)
- Knob 3 (A2): Target = 700 β†’ Acceptable: 660–740 (~2/3 turn)
- Knob 4 (A3): Target = 400 β†’ Acceptable: 360–440 (~40% turn)
- Knob 5 (A4): Target = 600 β†’ Acceptable: 560–640 (~just past center)

Wrong Order: Means any of the knob's ADC count is out of range?

What will the Logic be for the Oranje Led?
Green is correct , Red is wrong so where does Orange fit in?
Orange is just Blinking?

Probably, it is a status that system is active.

@vinnylich

First try with two pots to establish the logic.

Keep blinking Ornage Led at 1-sec interval in the background using TC1 on interrupt

That makes it far too complicated for a beginner. A better advice would be to study the blink-without-delay example.

I had a mind to keep those codes totally separated from the main logic. If OP desires help, codes for TC1 could be provided.

As I see it, the below will keep it separated.

...
...

void setup()
{
  ...
  ...
}

void loop()
{
  bwod();
  logic();
}

void bwod()
{
  // blink without delay implementatiomn
}

void logic()
{
  // logic implementatiomn
}

I don't think this single line will detect if all the Pots is correct within the range for each Pot.

It should. It's inside a loop. If any pot is too far from target, the Boolean variable gets set to false.

@vinnylich try this to see what serial monitor shows:

// --- Pin Assignments ---
const int potPins[5] = {A0, A1, A2, A3, A4}; // Potentiometers
const int orangeLED = 2; // Always blinks
const int redLED = 3; // ON when any knob is wrong
const int greenLED = 4; // ON only when all knobs are correct

// --- Target values for each knob ---
const int targets[5] = {300, 512, 700, 400, 600}; // Adjust per puzzle
const int tolerance = 40;

/*
Knob Positions:
- Knob 1 (A0): Target = 300 β†’ Acceptable: 260–340 (~1/3 turn)
- Knob 2 (A1): Target = 512 β†’ Acceptable: 472–552 (centered)
- Knob 3 (A2): Target = 700 β†’ Acceptable: 660–740 (~2/3 turn)
- Knob 4 (A3): Target = 400 β†’ Acceptable: 360–440 (~40% turn)
- Knob 5 (A4): Target = 600 β†’ Acceptable: 560–640 (~just past center)
*/

void setup() {
pinMode(orangeLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
Serial.begin(9600);
}

void loop() {
digitalWrite(orangeLED, HIGH);
delay(200);
digitalWrite(orangeLED, LOW);
delay(200);

bool allCorrect = true;

for (int i = 0; i < 5; i++) {
int val = analogRead(potPins[i]);
Serial.print("Knob ");
Serial.print(i);
Serial.print(": ");
Serial.print(val);
Serial.print(" vs target: ");
Serial.println(val - targets[i]);

if (abs(val - targets[i]) > tolerance) {
allCorrect = false;
}
}

if (allCorrect) {
digitalWrite(greenLED, HIGH);
digitalWrite(redLED, LOW);
} else {
digitalWrite(greenLED, LOW);
digitalWrite(redLED, HIGH);
}
}

PS. To share what serial monitor shows, do not use screen image. Uncheck "Auto scroll", copy the text from serial monitor and paste into your reply between code tags.

Even if it worked it would be too hard. Five knobs that have to be positioned just so, with no feedback whatsoever until they all are.

Unless you are planning to provide clues in the environment that players can discover and figure out.

So tell us more.

a7

The above template is populated with codes for multi-tasking. Two tasks are being executed concurrently without blocking the MCU. This sketch may be helpful for OP.

const int ledPin =  LED_BUILTIN;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 1000;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  bwod();
  logic();
}

void bwod()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    if (ledState == LOW)
    {
      ledState = HIGH;
    }
    else
    {
      ledState = LOW;
    }
    digitalWrite(ledPin, ledState);
  }
}

void logic()
{
  if ((analogRead(A0) >= 260 && analogRead(A0) <=  340)
      && (analogRead(A1) >= 472 && analogRead(A1) <=  552))
  {
    Serial.println("Green LED is On.");
    Serial.println("Red LED is Off.");
  }
  else
  {
    Serial.println("Green LED is Off.");
    Serial.println("Red LED is On.");
  }
}
  • As a point of interest only, this line of code takes > 400us :roll_eyes:
    Might be best to use BWOD to read the analogs less often.

It does. The sketch needed a "millis()blink" and some formatting.

diagram.json for wokwi
{
  "version": 1,
  "author": "foreignpigdog x",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": -33.6, "left": -67.7, "attrs": {} },
    { "type": "wokwi-potentiometer", "id": "pot1", "top": -116.5, "left": 105.4, "attrs": {} },
    { "type": "wokwi-potentiometer", "id": "pot2", "top": -116.5, "left": 191.8, "attrs": {} },
    { "type": "wokwi-potentiometer", "id": "pot3", "top": -116.5, "left": 278.2, "attrs": {} },
    { "type": "wokwi-potentiometer", "id": "pot4", "top": -116.5, "left": 364.6, "attrs": {} },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -87.6,
      "left": 64.2,
      "rotate": 90,
      "attrs": { "color": "orange" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": -116.4,
      "left": 64.2,
      "rotate": 90,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-led",
      "id": "led3",
      "top": -145.2,
      "left": 64.2,
      "rotate": 90,
      "attrs": { "color": "green" }
    },
    { "type": "wokwi-potentiometer", "id": "pot5", "top": -116.5, "left": 451, "attrs": {} }
  ],
  "connections": [
    [ "nano:GND.3", "pot1:GND", "black", [ "v0", "h28.8" ] ],
    [ "nano:5V.2", "pot1:VCC", "red", [ "v0", "h67.2" ] ],
    [ "nano:GND.3", "pot2:GND", "black", [ "v0", "h115.2" ] ],
    [ "nano:GND.3", "pot3:GND", "black", [ "v0", "h220.8" ] ],
    [ "nano:GND.3", "pot4:GND", "black", [ "v0", "h345.6" ] ],
    [ "nano:5V.2", "pot2:VCC", "red", [ "v0", "h153.6" ] ],
    [ "nano:5V.2", "pot3:VCC", "red", [ "v0", "h259.2" ] ],
    [ "nano:5V.2", "pot4:VCC", "red", [ "v0", "h307.2" ] ],
    [ "nano:GND.2", "led1:C", "black", [ "v0" ] ],
    [ "nano:GND.2", "led2:C", "black", [ "v0" ] ],
    [ "nano:GND.2", "led3:C", "black", [ "v0" ] ],
    [ "nano:2", "led1:A", "green", [ "v0" ] ],
    [ "nano:3", "led2:A", "green", [ "v0" ] ],
    [ "nano:4", "led3:A", "green", [ "v0" ] ],
    [ "nano:GND.3", "pot5:GND", "black", [ "v0", "h364.8" ] ],
    [ "nano:5V.2", "pot5:VCC", "red", [ "v0", "h432" ] ],
    [ "nano:A0", "pot1:SIG", "green", [ "v9.6", "h134.4" ] ],
    [ "nano:A1", "pot2:SIG", "green", [ "v19.2", "h230.4" ] ],
    [ "nano:A2", "pot3:SIG", "green", [ "v28.8", "h268.8" ] ],
    [ "nano:A3", "pot4:SIG", "green", [ "v38.4", "h288" ] ],
    [ "nano:A4", "pot5:SIG", "green", [ "v48", "h441.6" ] ]
  ],
  "dependencies": {}
}
corrected and commented code
// --- Pin Assignments ---
const int potPins[5] = {A0, A1, A2, A3, A4}; // Potentiometers
const int orangeLED = 2; // Always blinks
const int redLED = 3; // ON when any knob is wrong
const int greenLED = 4; // ON only when all knobs are correct

// --- Target values for each knob ---
const int targets[5] = {300, 512, 700, 400, 600}; // Adjust per puzzle
const int tolerance = 40;

unsigned long timer, timeout = 200; // ***  ADD THIS LINE

/*
  Knob Positions:
  - Knob 1 (A0): Target = 300 β†’ Acceptable: 260–340 (~1/3 turn)
  - Knob 2 (A1): Target = 512 β†’ Acceptable: 472–552 (centered)
  - Knob 3 (A2): Target = 700 β†’ Acceptable: 660–740 (~2/3 turn)
  - Knob 4 (A3): Target = 400 β†’ Acceptable: 360–440 (~40% turn)
  - Knob 5 (A4): Target = 600 β†’ Acceptable: 560–640 (~just past center)
*/

void setup() {
  pinMode(orangeLED, OUTPUT);
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // digitalWrite(orangeLED, HIGH); // *** REMOVE THIS LINE
  // delay(200);                    // *** REMOVE THIS LINE
  // digitalWrite(orangeLED, LOW);  // *** REMOVE THIS LINE
  // delay(200);                    // *** REMOVE THIS LINE

  if (millis() - timer > timeout) {                     // *** ADD THIS LINE
    timer = millis();                                   // *** ADD THIS LINE
    digitalWrite(orangeLED, !digitalRead(orangeLED));   // *** ADD THIS LINE
  }

  bool allCorrect = true;

  for (int i = 0; i < 5; i++) {
    int val = analogRead(potPins[i]);
    Serial.print("(");// *** ADD THIS LINE
    Serial.print(i);
    Serial.print(":");
    if (val < 100) Serial.print(" "); // *** ADD THIS LINE
    if (val < 10) Serial.print(" ");// *** ADD THIS LINE
    Serial.print(val);
    Serial.print(") ");// *** ADD THIS LINE

    if (abs(val - targets[i]) > tolerance) {
      allCorrect = false;
    }
  }
  Serial.println();

  if (allCorrect) {
    digitalWrite(greenLED, HIGH);
    digitalWrite(redLED, LOW);
  } else {
    digitalWrite(greenLED, LOW);
    digitalWrite(redLED, HIGH);
  }
}

You can cut the analogRead() time by 2 if you simply read into two variables and use those variables in the if statement.

Further see @LarryD's remark.

You mean:

unsigned int y0 = analogRead(A0);
unsigned int y1 = analogRead(A1);
if(( 260 <= y0 <= 340) && (472 <= y1 <= 552))
{
   //...............
}
else
{
   //.................
}