Running 2 funtions at the same time

I have been working on something that you need to input the password using buttons to stop an alarm system. Each button has its string to be added to the total password and will be checked after 4 inputs. I wanted to run 2 functions (alarm and button input) so that whenever it would alarm, you can still input the password while the alarm is on.

Sample code:

int buzzer = 1;
int buttonpins = A0; //receives different values
String inputPass = “”;
String Password = “3214”; //Can be any combination
int input = 0;

void setup(){
pinMode();
}

void loop(){
checkButton();

if(input == 4){
if(inputPass == Password){
//okay
}
else{
alarm();
}
}
}

void alarm(){
digitalWrite(buzzer, HIGH);
//Insert how to checkButton while alarming…
delay(2000);
}

void checkButton(){
//Each analogRead reads different values since each button has a resistor with different values
if(analogRead(buttonPin) >= 100 && analogRead <= 200){
intputPass += “1”;
input++;
}
else if(analogRead(buttonPin) >= 300 && analogRead <= 400){
inputPass += “2”;
input++;
}
else if(analogRead(buttonPin) >= 500 && analogRead <= 600){
inputPass += “3”;
input++;
}
else if(analogRead(buttonPin) >= 700 && analogRead <= 800){
inputPass += “4”;
input++;
}
}

Each button has its string to be added

No, it does not. A string is a NULL terminated array of chars. A String is an instance of a class that has no place on the Arduino. You really are not expecting to allow the user to have any length password, are you? Who's going to routinely enter a 12843 character password?

Your code, as is, makes no sense. There is nothing that defines whether to turn the alarm on or not. Function names should have both a noun and a verb, describing what to do with what. alarm() does not satisfy that criteria.

With a 4 character password, it will take at least 8 seconds with the alarm screaming to enter the password. Not a system that I would ever use.

Your system does not allow for making a mistake. Not a system that I would ever use.

//Insert how to checkButton while alarming..

You don't need any code. After 2 seconds, alarm() ends, and loop() gets called again.

lxGooDByExl:
Sample code:

Your sample code has about a hundred mistakes in it.

For example:
"Entering a char" is not the same as "reading a value from the analog pin".

First you must know, which is the analog reading for "button not pressed".

Then "entering a char" is a "state change detection" and you will have to detect the state changing from "not pressed reading" to "button pressed reading".

If you do no "state change detection", the user simply presses one button and within one single millisecond he has entered code "1111" or "2222" or "3333" or "4444", i think.

If you do no "state change detection", the user simply presses one button and within one single millisecond he has entered code "1111" or "2222" or "3333" or "4444", i think.

Except that until 4 calls to the checkButtons() function have been made, there is a 2 second pause between calls.

Edit: I was hooking the else to the wrong if. You are correct. The program will call checkButtons() over and over until input is 4, which will happen quickly.

@ lxGooDByExl , the demo Several Things at a Time sounds like it might be relevant to your problem

...R