I am trying to program something where i need to put in a pincode for something to display on an lcd but i need to press the buttons all at the same time for the thing on the lcd to be displayed and was wondering if you could make it so once the arduino has received a signal from button 1 then it doesnt need to be held down anymore until it has run through the code and the other buttons have been pressed
What you need to do is implement a Finite State Machine (you can look this up).
At the moment the code needs all the buttons to be pressed for it to pass through all the if statements. What you need to have is a situation where when you have pressed button 1, the code is waiting for button 2, when button 2 is pressed the code is waiting for button 3, etc. If any are out of sequence the code will reset.
The structure of the code will look something like this:
static int state = 0;
switch (state)
{
case 0:
if button 1 is pressed
state = 1;
else
state = 0; // remains the same
break;
case 1:
if button 2 is pressed
state = 2;
else
state = 0; // reset
break;
etc for the other states/buttons
A switch is an input device. You can't make it be pressed.
What you do is write the code so that, once the state of the switch is determined to be pressed, you no longer care about what the state of that switch is/was/will be.
You'll be in a world of hurt if you ever decide that your pin code should be 3412, instead of 1234, with the code that you have now.
If I'm understanding you right, your main goal is to make your pushbutton responses behave like a toggle switch, where you press it once to turn on, then again to turn off. I'll neglect for now the fact that button contacts usually need to be de-bounced to do something like this. One solution:
static uint8_t b1State = 0;
static uint8_t b2State = 0;
static uint8_t b3State = 0;
static uint8_t b4State = 0;
int b1value = digitalRead(A0);
int b2value = digitalRead(A1);
int b3value = digitalRead(A2);
int b4value = digitalRead(A3);
if (b1value ) b1State = !b1State;
if (b2value ) b2State = !b2State;
if (b3value ) b3State = !b3State;
if (b4value ) b4State = !b4State;
so the state vars (b1State, etc) now basically reverse and hold their state variable with each button press. Again, this won't work well unless you denounce your buttons, and its not taking into account that though you may THINK you're pressing all the buttons at the same time, they really happen at slightly different times. Now if you must have all buttons (or whatever combination of buttons) register at the same time, you could consider a 5th "ACTION" button, whose purpose is to start whatever process you want, based on the buttons pressed. In this case, we're not "toggling" button states, but just making them "stick", so the debouce is less necessary.
static uint8_t b1State = 0;
static uint8_t b2State = 0;
static uint8_t b3State = 0;
static uint8_t b4State = 0;
int b1value = digitalRead(A0);
int b2value = digitalRead(A1);
int b3value = digitalRead(A2);
int b4value = digitalRead(A3);
int b5Actionvalue = digitalRead(A4);
if (b1value ) b1State = 1
if (b2value ) b2State = 1;
if (b3value ) b3State = 1;
if (b4value ) b4State = 1;
// and now for your 5th, ACTION button, pressed afterward...
if (b5Actionvalue )
{
// do your LCD output based on the b1State through b4State values
doMyLCDstuff();
// then, clear all your states, to be ready for the next action
b1State = b2State = b3State = b4State =0;
}
Should be simple just to make an array that defines in which order the buttons should be pressed.
const byte PINCODE_LENGTH = 4;
const byte PINCODE[PINCODE_LEGTH] = {A0, A2, A1, A3};
const unsigned int PINCODE_TIMEOUT = 1000;
usigned long lastPincodePress = 0;
byte pincodeIndex = 0;
void loop()
{
//Check if the pincode entering has timed out..
unsigned long currentMilis = millis();
if (currentMillis - lastPincodePress > PINCODE_TIMEOUT) pincodeIndex = 0;
//Read next pincode button
if (readButton(PINCODE[pincodeInbex])) {
lastPinodePress = currentMillis;
pincodeIndex++;
if (pincodeIndex == PINCODE_LENGTH) {
pincodeIndx = 0;
//Successfully received the pincode!
}
}
}
bool readButton(byte btnPin) {
byte hit=255, count=0;
for (byte i = 0; i < PINODE_LENGTH; i++) if (digitalRead(PINCODE[i]) == HIGH) {
hit = i;
count++;
}
retrun (hit = btnPin) && (count == 1);
}
This will read the buttons in the sequence defined by "PINCODE". If no button is pressed for 1 second (1000 milliseconds), the pincode typing is reset to first button. Pseudocode and deliberate typos included for free - hope OP will think, not just copy!