Running into problems

noob alert

Can someone point me in the right direction? This is the demo video of the code. https://youtu.be/JGQNfLieVJM

I set it up without resistors (didn’t have any at the time). LED 1 was dim and button 1 didn’t seem to work.

#define BTN1 2
#define BTN2 3
#define BTN3 4
#define BTN4 5

#define LED1 6
#define LED2 7
#define LED3 8
#define LED4 9

#define RELAY 10

byte btn[4] = {BTN1, BTN2, BTN3, BTN4};
byte led[4] = {LED1, LED2, LED3, LED4};

byte pressed[4] = {0};
byte pass[4] = {2, 3, 1, 4};
byte md = 0;

unsigned long tim = 0;
void setup() 
{
  Serial.begin(9600);
  for (byte i = 0; i < 4 ; i++)
  {
    pinMode(btn[i], INPUT_PULLUP);
    pinMode(led[i], OUTPUT);
    digitalWrite(led[i], HIGH);
  }
  pinMode(RELAY, OUTPUT);
  digitalWrite(RELAY, HIGH);
  tim = millis();
}

void loop() 
{
  switch(md)
  {
    case 0:
       if(!digitalRead(BTN1) && millis() - tim > 100)
       {
          pressed[0] = 1;
          md = 1;
          digitalWrite(LED1, LOW);
          while(!digitalRead(BTN1));
       }
       else if(!digitalRead(BTN2) && millis() - tim > 100)
       {
          pressed[0] = 2;
          md = 1;
          digitalWrite(LED2, LOW);
          while(!digitalRead(BTN2));
       } 
       else if(!digitalRead(BTN3) && millis() - tim > 100)
       {
          pressed[0] = 3;
          md = 1;
          digitalWrite(LED3, LOW);
          while(!digitalRead(BTN3));
          Serial.println("BTN3");
       }
       else if(!digitalRead(BTN4) && millis() - tim > 100)
       {
          pressed[0] = 4;
          md = 1;
          digitalWrite(LED4, LOW);
          while(!digitalRead(BTN4));
       }
       else if (digitalRead(BTN1) && digitalRead(BTN2) && digitalRead(BTN3) && digitalRead(BTN4))
       {
          tim = millis();
       }
       
       break;

    case 1:
       if(!digitalRead(BTN1) && millis() - tim > 100)
       {
          pressed[1] = 1;
          md = 2;
          digitalWrite(LED1, LOW);
          while(!digitalRead(BTN1));
       }
       else if(!digitalRead(BTN2) && millis() - tim > 100)
       {
          pressed[1] = 2;
          md = 2;
          digitalWrite(LED2, LOW);
          while(!digitalRead(BTN2));
       } 
       else if(!digitalRead(BTN3) && millis() - tim > 100)
       {
          pressed[1] = 3;
          md = 2;
          digitalWrite(LED3, LOW);
          while(!digitalRead(BTN3));
          Serial.println("BTN33");
       }
       else if(!digitalRead(BTN4) && millis() - tim > 100)
       {
          pressed[1] = 4;
          md = 2;
          digitalWrite(LED4, LOW);
          while(!digitalRead(BTN4));
       }
       else if (digitalRead(BTN1) && digitalRead(BTN2) && digitalRead(BTN3) && digitalRead(BTN4))
       {
          tim = millis();
       }
       break;

    case 2:
       if(!digitalRead(BTN1) && millis() - tim > 100)
       {
          pressed[2] = 1;
          md = 3;
          digitalWrite(LED1, LOW);
          while(!digitalRead(BTN1));
       }
       else if(!digitalRead(BTN2) && millis() - tim > 100)
       {
          pressed[2] = 2;
          md = 3;
          digitalWrite(LED2, LOW);
          while(!digitalRead(BTN2));
       } 
       else if(!digitalRead(BTN3) && millis() - tim > 100)
       {
          pressed[2] = 3;
          md = 3;
          digitalWrite(LED3, LOW);
          while(!digitalRead(BTN3));
          Serial.println("BTN333");
       }
       else if(!digitalRead(BTN4) && millis() - tim > 100)
       {
          pressed[2] = 4;
          md = 3;
          digitalWrite(LED4, LOW);
          while(!digitalRead(BTN4));
       }
       else if (digitalRead(BTN1) && digitalRead(BTN2) && digitalRead(BTN3) && digitalRead(BTN4))
       {
          tim = millis();
       }
       break;

    case 3:
       if(!digitalRead(BTN1) && millis() - tim > 100)
       {
          pressed[3] = 1;
          md = 4;
          digitalWrite(LED1, LOW);
          while(!digitalRead(BTN1));
       }
       else if(!digitalRead(BTN2) && millis() - tim > 100)
       {
          pressed[3] = 2;
          md = 4;
          digitalWrite(LED2, LOW);
          while(!digitalRead(BTN2));
       } 
       else if(!digitalRead(BTN3) && millis() - tim > 100)
       {
          pressed[3] = 3;
          md = 4;
          digitalWrite(LED3, LOW);
          while(!digitalRead(BTN3));
          Serial.println("BTN3333");
       }
       else if(!digitalRead(BTN4) && millis() - tim > 100)
       {
          pressed[3] = 4;
          md = 4;
          digitalWrite(LED4, LOW);
          while(!digitalRead(BTN4));
       }
       else if (digitalRead(BTN1) && digitalRead(BTN2) && digitalRead(BTN3) && digitalRead(BTN4))
       {
          tim = millis();
       }
       break;

    case 4:
       bool check = true;
       for (int i = 0; i<4; i++)
       {
        if (pressed[i] != pass[i])
          check = false;
       }

       if (check)
       {
        digitalWrite(RELAY, LOW);       
       }
       else 
       {
          all_on();
          delay(100);
          all_off();
          delay(100);
          all_on();
          delay(100);
          all_off();
          delay(100);
          all_on();
          delay(100);
          all_off();
          delay(100);
          md = 0;
          all_on();
          digitalWrite(RELAY, HIGH);  
          for (int i = 0; i < 4; i++)
          {
            pressed[i] = 0;
          }
       }
       break;
  }
}

void all_off()
{
  for (int i = 0; i < 4; i++)
  {
    digitalWrite(led[i], LOW);
  }
}

void all_on()
{
  for (int i = 0; i < 4; i++)
  {
    digitalWrite(led[i], HIGH);
  }
}

No idea about the code , but you can’t just bodge about with this stuff or you’ll destroy parts .
Leds need resistors .
I can see “relay “ in your code - how is that powered ? How are your buttons connected ?

Could you provide a circuit diagram ?

hammy:
No idea about the code , but you can’t just bodge about with this stuff or you’ll destroy parts .
Leds need resistors .
I can see “relay “ in your code - how is that powered ? How are your buttons connected ?

Could you provide a circuit diagram ?

Thanks for the reply, I was just about to the delete the post because I am an idiot. I was using pins 1-9 instead of 2-10; hence the problem. I have added 10k resistors to the buttons as suggested and 330 resistors to the LEDs.

Right now its MOSTLY working correctly. However, it is supposed to allow all 4 digits before resetting if incorrect and instead sometimes it resets on 1, 2, or 3 digits (when incorrect).

Could this be low quality buttons or interference with overlapping wires (using jumpers right now)? Currently the relay is NOT installed, I am testing with another LED. I would draw the circuit but I have no idea the correct way to do it. Essentially it goes

Pin--resistor (330)--LED--Ground
Pin--Button--resistor (10k)--Ground

Someone mentioned the below but I have not been able to Google a fix for that issue (if that's what it is).

"the main issue that you are receiving is that some time due to noise system register multiple key presses when you press single time. Arduino has weak internal pullups that's the reason"

look up debounce

Thanks, that will probably work; I'll have it added.