arduino project: push security

Hello,

My question is how do I make a program where I use four push buttons in a certain order to light up a green led and if that order is not correct, a red led will light up.

What experience of using the Arduino do you have ?

Is this a school assignment ?

I have been programming java for a while and did some arduino project in the past with bluetooth.
No I just wanted to make something like that

So, do you know how to read the state of an input and take different actions based on its value ?

I presume that you know what an array is and understand how an array could hold the target series

yes I know to read inputs and do something with the incomming data and I know arrays but it's a bit new for me

OK. A suggested way forward

Put the input pin numbers in an array in the order in which they should be pressed. Set them all to INPUT_PULLUP in pinMode() and wire the inputs to be taken to GND when the buttons are pressed

Set a counter to zero

Read each of the input pins in turn in a for loop. If one of them is LOW (ie pressed) save the value of the for loop variable

After the for loop compare the value of the saved for loop variable with the value of counter. If they match, then the input was correct so increment counter and read the inputs again. Keep repeating this. If counter and the saved for loop variable are both 3 then all 4 buttons have been pressed in the correct order. Do whatever you want to indicate success

If at any time the value of counter and the saved for loop variable do not match then a wrong entry has been made. Do whatever you want to indicate failure and set counter back to zero to reset back to the start of entry

I typed this in a hurry so the logic may not be completely correct but it should give you something to go on

This is how I would do it (pseudo code):

pass_sequence = (5, 4, 6, 3) //Pin numbers with buttons attached
push_sequence = (0, 0, 0, 0)
push_index = 0

setup (
  for each pin in pass_sequence: pin_mode(pin, input)
)

loop (
  for each pin in pass_sequence: (
    if read_state(pin) equals pushed: (
      push_sequence(push_index) = pin
      push_index = push_index + 1
      if push_index equals sequense_size: (
        if pass_sequence equals push_sequence: flash_green()
        else: flash_red()
        push_index = 0
      )
    )
  )
)

Thankyou guy's it's a big help

I've thrown some code together with some stuff ripped from some old projects, which you may use as inspiration. It does input debouncing, button state change detection and checks the sequence pressed. I've not done anything but quick tests in a simulator. I've not added many comments except where something needs to be changed, but feel free to ask if any elaboration is needed.

A word of warning, I'm very much an exclusive C programmer, which is very obvious looking at code.

security-thing.zip (2.24 KB)

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