Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int Green_LED = 5;
- int Red_LED = 4;
- int ButtonInput = 9;
- int ButtonConfirm = 8;
- int Digit = 0;
- int InputBegins = 0;
- uint32_t counter1 = 0;
- uint32_t counter2 = 0;
- int arrayDigits[4] = {0, 0, 0, 0};
- int Pos = 0;
- int arrayCorrectCode[4] = {2, 4, 3, 1};
- // first I set up all the needed integers. Also I used something I know from C, which is Array.
- // if I have an int , the array can store multiple ints which makes life easier when I have to do the code.
- void setup() {
- pinMode(Green_LED, OUTPUT);
- pinMode(Red_LED, OUTPUT);
- pinMode(ButtonInput, INPUT_PULLUP);
- pinMode(ButtonConfirm, INPUT_PULLUP);
- Serial.begin(9600);
- // of course I always need to set up my leds and buttons and if something goes wrong I need the Serial Monitor.
- }
- void loop() {
- if (digitalRead(ButtonInput) == LOW && InputBegins == 0) {
- digitalWrite(Green_LED, HIGH);
- digitalWrite(Red_LED, HIGH);
- delay(1000);
- digitalWrite(Green_LED, LOW);
- digitalWrite(Red_LED, LOW);
- InputBegins = 1;
- // first as is shown in the instructions I have to make the LEDs light up and then peace out.
- }
- if (Pos < 4) {
- Serial.println(Pos);
- if (digitalRead(ButtonInput) == LOW) {
- if (counter1 == 0) {
- Digit = Digit + 1;
- digitalWrite(Green_LED, HIGH);
- }
- counter1 += 1;
- } else {
- digitalWrite(Green_LED, LOW);
- counter1 = 0;
- }
- if (digitalRead(ButtonConfirm) == LOW) {
- if (counter2 == 0) {
- digitalWrite(Red_LED, HIGH);
- arrayDigits[Pos] = Digit;
- Digit = 0;
- Pos += 1;
- }
- counter2 += 1;
- } else {
- digitalWrite(Red_LED, LOW);
- counter2 = 0;
- }
- } else {
- digitalWrite(Red_LED, LOW);
- if (arrayDigits[0] == arrayCorrectCode[0] && arrayDigits[1] == arrayCorrectCode[1] && arrayDigits[2] == arrayCorrectCode[2] && arrayDigits[3] == arrayCorrectCode[3]) {
- digitalWrite(Green_LED, HIGH);
- delay(2000);
- digitalWrite(Green_LED, LOW);
- InputBegins = 0;
- arrayDigits[0] = 0;
- arrayDigits[1] = 0;
- arrayDigits[2] = 0;
- arrayDigits[3] = 0;
- Pos = 0;
- }
- else {
- digitalWrite(Red_LED, HIGH);
- delay(2000);
- digitalWrite(Red_LED, LOW);
- InputBegins = 0;
- arrayDigits[0] = 0;
- arrayDigits[1] = 0;
- arrayDigits[2] = 0;
- arrayDigits[3] = 0;
- Pos = 0;
- // here is where I have used the array so that I can set up my code and then I can compare if the code matches or not.
- // if according to the array I have set up the code matches the LED at the end will be Green as in entry granted.
- // if it is wrong the LED will be red.
- }
- }
- delay(50);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement