Trying to make a digital lock, but button seems bugged

Hello! Currently, i'm making a digital lock for a school project, but i can't seem to be able to get the only button on the project working.

// total_hours_wasted = 92

#include "Adafruit_LEDBackpack.h"

int input;

int codigo = 1111;

int tentativas = 3;

bool BA;

int B2 = 0;

int B3 = 0;

int B4 = 0;
unsigned long lastpress = 0;
unsigned long stateonetime = 100;
unsigned long statetwotime = 2000;
int BAn = 0;

int B2n = 0;

int B3n = 0;

int B4n = 0;
int win = false;
int lose = false;
int reset = false;
bool switching;
bool send;



Adafruit_7segment led_display1 = Adafruit_7segment();

void setup()
{
     Serial.begin(9600);
  led_display1.begin(112);
   pinMode(5, INPUT);
    pinMode(2, INPUT);
  pinMode(3, INPUT);
    pinMode(4, INPUT);
  pinMode(8, INPUT);
    pinMode(9, INPUT);
      pinMode(10, OUTPUT);
    pinMode(11, OUTPUT);
   pinMode(12, OUTPUT);
pinMode(13, INPUT);
}
void checkBA()
{
  BA = digitalRead(5);   
  if (BA == LOW) 
  {BAn = 0000;}

  else {BAn = 1000;}


}
void checkB2()
{
   B2 = digitalRead(2);
     if (B2 == HIGH) 
  {B2n = 100;}


  else 
  {B2n = 000;}


}
void checkB3()
{
   B3 = digitalRead(3);
     if (B3  ==  HIGH ) 
  {B3n = 10;}

   else 
  {B3n = 00;}



}
void checkB4()
{
  B4 = digitalRead(4);
     if (B4  ==  HIGH) 
  {B4n = 1;}


   else 
  {B4n = 0;}


}
void loop() {
input = (BAn + (B2n + (B3n + B4n)));
  send = digitalRead(8);
  switching = digitalRead(13);
checkBA();
  checkB2();
  checkB3();
  checkB4();
  led_display1.println(((BAn + (B2n + (B3n + B4n)))));
  led_display1.writeDisplay();
  if (win == true) {digitalWrite(10, HIGH);}
  if (lose == true) {digitalWrite(11, HIGH); tone(12, 523, 1000);}
  if (switching == HIGH) {reset = true; }
  else if (switching == LOW) {reset = false;}
  if (send == LOW){
    if (reset == true){
      tentativas = 3; win = false; lose = false; digitalWrite(10, LOW); digitalWrite(11, LOW);}
if(reset == false)
  {if (tentativas <= 3) 
        {if (input == codigo) 
             {win = true; Serial.println("Ganhou");} 
           else 
             {tentativas = (tentativas -1); Serial.println("Falhou uma tentativa");}
         if (tentativas == 0) {Serial.println("Perdeu"); lose = true;}
   }
  }
 }
  delay(10);
} 


I believe the problem lies in the send == LOW part. But i couldn't get it to work.

Please use Tools -> Auto Format before posting your code. See below for what that might look like.

Shouldn't you calculate 'input' AFTER you call checkBA(), checkB2(), checkB3(), and checkB4()?

You're using 'INPUT' mode for your buttons/switches so you MUST have external pull-up or pull-down resistors on each pin. Do you have them?

// total_hours_wasted = 92

#include "Adafruit_LEDBackpack.h"

int input;

int codigo = 1111;

int tentativas = 3;

bool BA;
int B2 = 0;
int B3 = 0;
int B4 = 0;

unsigned long lastpress = 0;
unsigned long stateonetime = 100;
unsigned long statetwotime = 2000;
int BAn = 0;
int B2n = 0;
int B3n = 0;
int B4n = 0;
int win = false;
int lose = false;
int reset = false;
bool switching;
bool send;

Adafruit_7segment led_display1 = Adafruit_7segment();

void setup()
{
  Serial.begin(9600);
  led_display1.begin(112);
  pinMode(5, INPUT);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(8, INPUT);
  pinMode(9, INPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, INPUT);
}

void checkBA()
{
  BA = digitalRead(5);
  if (BA == LOW)
  {
    BAn = 0000;
  }
  else
  {
    BAn = 1000;
  }
}

void checkB2()
{
  B2 = digitalRead(2);
  if (B2 == HIGH)
  {
    B2n = 100;
  }
  else
  {
    B2n = 000;
  }
}

void checkB3()
{
  B3 = digitalRead(3);
  if (B3  ==  HIGH )
  {
    B3n = 10;
  }

  else
  {
    B3n = 00;
  }
}

void checkB4()
{
  B4 = digitalRead(4);
  if (B4  ==  HIGH)
  {
    B4n = 1;
  }
  else
  {
    B4n = 0;
  }
}

void loop()
{
  input = (BAn + (B2n + (B3n + B4n)));
  send = digitalRead(8);
  switching = digitalRead(13);
  checkBA();
  checkB2();
  checkB3();
  checkB4();
  led_display1.println(((BAn + (B2n + (B3n + B4n)))));
  led_display1.writeDisplay();
  
  if (win == true)
  {
    digitalWrite(10, HIGH);
  }
  
  if (lose == true)
  {
    digitalWrite(11, HIGH);
    tone(12, 523, 1000);
  }
  
  if (switching == HIGH)
  {
    reset = true;
  }
  else if (switching == LOW)
  {
    reset = false;
  }
  
  if (send == LOW)
  {
    if (reset == true)
    {
      tentativas = 3; win = false; lose = false; digitalWrite(10, LOW); digitalWrite(11, LOW);
    }
    
    if (reset == false)
    {
      if (tentativas <= 3)
      {
        if (input == codigo)
        {
          win = true;
          Serial.println("Ganhou");
        }
        else
        {
          tentativas = (tentativas - 1);
          Serial.println("Falhou uma tentativa");
        }
        
        if (tentativas == 0)
        {
          Serial.println("Perdeu");
          lose = true;
        }
      }
    }
  }
  delay(10);
}

Post a schematic, not a frizzy picture of how you have wired it. Hand drawn is ok.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.