Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <DHT11.h>
#include <Display.h>
#include <TM1637Display.h>

const int BUZZER = 3;
const int RED_LED = 4;
const int GREEN_LED = 5;
const int BLUE_LED = 6;
const int YELLOW_LED = 7;
const int KEY1 = 8;
const int KEY2 = 9;
const int LDR = 16;

int lastkstate = 1;
int mode = 0; // Mode 0 - Start, Mode 1 - Safe, Mode 2 - Unlocking the safe, Mode 3 - Change password
int dispPosition = 0;
int inputNumber = 1;
int password = 1234;

void setup() {
  // put your setup code here, to run once:
  pinMode(RED_LED, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(BLUE_LED, OUTPUT);
  pinMode(YELLOW_LED, OUTPUT);
  pinMode(KEY1, INPUT_PULLUP);
  pinMode(KEY2, INPUT_PULLUP);
  pinMode(LDR, INPUT);
  Display.show("----");
}

void loop() {
  // put your main code here, to run repeatedly:
  int k1state = digitalRead(KEY1);
  int k2state = digitalRead(KEY2);
  if (mode == 0) {
    Display.show("----");
    digitalWrite(RED_LED, HIGH);
    delay(100);
    digitalWrite(GREEN_LED, HIGH);
    delay(100);
    digitalWrite(BLUE_LED, HIGH);
    delay(100);
    digitalWrite(YELLOW_LED, HIGH);
    delay(100);
    digitalWrite(RED_LED, LOW);
    delay(100);
    digitalWrite(GREEN_LED, LOW);
    delay(100);
    digitalWrite(BLUE_LED, LOW);
    delay(100);
    digitalWrite(YELLOW_LED, LOW);
    delay(100);
    mode++;
  }
  if (mode == 1) {
    if (k1state != lastkstate) {
      if (k1state == 0) {
        delay(50);
        dispPosition++;
        inputNumber = 1;
      }
    }
    if (k2state != lastkstate) {
      if (k2state == 0) {
        delay(50);
        inputNumber++;
      }
    }
    delay(50);
    lastkstate = k2state;
    if (inputNumber > 4) {
      inputNumber = 1;
    }
    if (inputNumber == 1)Display.showCharAt(dispPosition, '1');
    if (inputNumber == 2)Display.showCharAt(dispPosition, '2');
    if (inputNumber == 3)Display.showCharAt(dispPosition, '3');
    if (inputNumber == 4)Display.showCharAt(dispPosition, '4');
    delay(50);
    lastkstate = k1state;
    if (dispPosition > 3) {
      mode++;
    }
  }
  if (mode == 2) {
    if
  }
}


What I have tried:

Hi, Arduino newbie here...I have an assignment to make a safe with the Arduino Rich Shield.. In mode 2 I need to check whether the value on the display matches the pre-set password but I can't seem to find a way to compare the two numbers...I would really appreciate it if someone helped me with this. Thank you in advance
Posted
Updated 3-Oct-20 22:19pm
v2
Comments
Richard MacCutchan 4-Oct-20 3:39am    
What is wrong with an if statement?

1 solution

should be really easy to compare two numbers
C++
if( input == passcode ) {
// check sucessful
} else {
// handle passcode error
}
You may also look for a do while loop like
C++
do {
 // some code to enter passcode
 input = getPasscode();
} while( input != passcode )
but here is the correct passcode the exit condition for the loop ;-)
 
Share this answer
 
Comments
TheJoKr Channel 4-Oct-20 13:13pm    
I tried if (mode == 2) {
if ( input == passcode ) {
digitalWrite(GREEN_LED, HIGH);
} else {
digitalWrite(RED_LED, HIGH);
}
}

but for some reason only the red led was turning on no matter whether the code was wrong or right..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900