Now the whole point is to learn, right? Now what do we want to happen? See the flowchart above. Let's get started by adding all of the pins.
This is just setting up all the pins. I'm not going to waste any time explaining this.
Setting up the the outputs.buttonState = digitalRead(button1);
This is setting the words 'buttonState' the the digital reading at pin button1, which is pin A4. Since it's using the digitalRead function 'buttonSate' can either be HIGH or LOW.int analog1 = analogRead(A0);
This is for taking an analog reading at pins A0. The analog reading is 0-1023.int pot1 = analog1 * (10.0 / 1023.0);
Convert the analog reading from pin A0 to 0-10 from 0-1023
The rest of the code is for the other analog readings on the others pins A0-A3.
if (pot1 == 4 && pot2 == 5 &&
pot3 == 3 && pot4 == 6)
Now this is where you need to put you your own combination in. I'm going to make mine 4536.
This is to test the following: If pot1 is equal to 4, and if pot2 is equal to 5, and if pot3 is equal to 6, and if pot4 is equal to 1, do the following. Change the numbers pot1,2,3,and 4 equal to create your own code.
Let's add another if statementelseif
(buttonState == HIGH
) Is the digital reading at pin A4 high??? Yes, it is! So do. . .
If either of these if statements aren't true, then do the following:else
Now you may have noticed I added a delay in the button block, but not the pot block. Well the reason why I did this is so that you don't have to hold the button. If you look over the code, you will see that there are no delays, so Arduino is constantly doing the loop over and over and over again at a very fast speed. So the code says, "if the button is high, unlock the door'. Well if you release the button, the loop is looped and it sees the the button is not HIGH, so it locks the door. With the delay, once it detects the button is pushed, it will preform our action (unlocking the door, changing the LEDS), then is wait for 5 seconds before running the loop again. If you don't understand, try it. Take out the delay and see what happens.