Combination lock (I need help)

I've been working on a combination lock with 4 push buttons and the way I was gonna make it work was just have it where if button1 was pressed a 1 would be displayed on the serial monitor and the same for the other 3 buttons and if the Correct code was entered (ex. 1324) an led would light up. Is there a way I can read the information from the serial monitor? I know I can read stuff that I type into it but I want it to read stuff that's put into it from a button.

You cannot read back data sent via Serial. Instead use 4 variables (or an array) for the buttons pressed, which you can inspect at any time.

If nothing helps, post your code using code tags </>.

#include <Servo.h>

Servo lock;
const int button1 = 4;
const int button2 = 5;
const int button3 = 6;
const int button4 = 7;
const int Submit = 3;
const int ledpin = 9;

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int SubmitState = 0;


void setup() {
  
pinMode (button1, INPUT);
pinMode (button2, INPUT);
pinMode (button3, INPUT);
pinMode (button4, INPUT);
pinMode (Submit, INPUT);
pinMode (ledpin, OUTPUT);
lock.attach(8);

Serial.begin(9600);
}

void loop() 
{
  Restart:
  
  buttonState1 = digitalRead(button1);
  buttonState2 = digitalRead(button2);
  buttonState3 = digitalRead(button3);
  buttonState4 = digitalRead(button4);
  SubmitState = digitalRead(Submit);
  
  lock.write(0);
  digitalWrite (ledpin, LOW);
  
  if (buttonState1 == HIGH) 
  {
    Serial.print("1");
    delay(250);
  }
  
  if (buttonState2 == HIGH) 
  {
    Serial.print("2");
    delay(250);
  }
  
  if (buttonState3 == HIGH) 
  {
    Serial.print("3");
    delay(250);
  }
  
  if (buttonState4 == HIGH) 
  {
    Serial.print("4");
    delay(250);
  }
}

Think about this case:

A user holds down all four buttons at once.

What will happen?

But yeah, so where are you planning to program the actual combination checking?

In the device you're serially connected to?

(Why?)

lemme get this straight.

you press the button on pin4 labeled button 1

you want the serial monitor to read

1

you come back and hour later and press button2
what you want the serial monitor to read

12

then if you press button4 you want it to read

124

if that is what you want, the proper term is concatenation.

then, once you press button1 again, you want it to read

1241
and then a red LED comes on ?

if you press any button,
recentButtonPress = millis()
}
if (millis()-recenButtonPress>10 seconds)
reset
ButtonHAsBeenPressed=0
string1= //make blank
string2=
string3=

string4=

}

=== not code, just the idea ===

if you pressed any button.
ButtonHAsBeenPressed = ButtonHAsBeenPressed+1

if(ButtonHAsBeenPressed==1)
String1=( put number you associate with this button)
if(ButtonHAsBeenPressed==2)
String2=( put number you associate with this button)
if(ButtonHAsBeenPressed==3)
String3=( put number you associate with this button)
if(ButtonHAsBeenPressed==4)
String1=( put number you associate with this button)

stringToBeDisplayed = String1+String2+String3+String4

serialprint stringToBeDisplayed

I figured it out.
Here's the code.

#include <Servo.h>

Servo lock;
const int button = 10;
const int button1 = 4;
const int button2 = 5;
const int button3 = 6;
const int button4 = 7;
const int ledpin = 9;

boolean lastButton1 = LOW;
boolean lastButton2 = LOW;
boolean lastButton3 = LOW;
boolean lastButton4 = LOW;

void setup() {
  
pinMode (button1, INPUT);
pinMode (button2, INPUT);
pinMode (button3, INPUT);
pinMode (button4, INPUT);
pinMode (Submit, INPUT);
pinMode (ledpin, OUTPUT);
lock.attach(8);

Serial.begin(9600);
}

void loop() 
{
  Restart:
  
  lock.write(20);
  digitalWrite (ledpin, LOW);
  
  if (digitalRead(button1) == HIGH) {
    lastButton1 = HIGH;
    delay(100);
  }

  if (digitalRead(button3) == HIGH && lastButton1 == HIGH) {
    lastButton1 = LOW;
    lastButton3 = HIGH;
    delay(100);
  }
  
  if (digitalRead(button2) == HIGH && lastButton3 == HIGH && lastButton1 == LOW) {
    lastButton2 = HIGH;
    lastButton3 = LOW;
    delay(100);
  }
  
  if (digitalRead(button4) == HIGH && lastButton2 == HIGH && lastButton1 == LOW && lastButton3 == LOW) {
    digitalWrite (ledpin, HIGH);
    lock.write(125);
    delay(100);
  }
  
  if (digitalRead(button) == HIGH) {
    lock.write(125);
    delay(100);
  }
}

You are going to run into a few problems with that bro,

  • you're only going to have a tenth of a second to open whatever is locked before it resets
  • There is no wrong answer, nothing resets it, meaning it would take pushing 16 buttons, and it would unlock hands down everytime
  • Run through the code once, unlock it, then identify your flaw during the next go around if you don't restart the arduino.

You are getting the idea though, If you tell me what the line restart: is supposed to do in theory, i'll see if I can fix this for you. I like math, this should be fun.

Thomas