1

I am a beginner when it comes to Arduino and tinkercad, I'm not really sure what the problem is here but I am trying to create a keypad and LCD lock. At first, it said that setlock was not declared in the scope but then I added 'boolean setLocked' but then the error just changed to saying that setLocked cannot be used as a function. Any help will be extremely appreciated and please talk to me as if I am very dumb lol I am just starting this course with no prior knowledge. Thanks!

boolean setLocked;
    #include <LiquidCrystal.h>
    #include <Keypad.h>

    #define redLED 10
    #define greenLED 11

    char* password="1212";
    int positions = 0;

    const byte rows = 4;
    const byte columns = 4;

    char keyMap [rows] [columns] = {
      {'1', '2', '3', 'A'},
      {'4', '5', '6', 'B'},
      {'7', '8', '9', 'C'},
      {'*', '0', '#', 'D'}  
    };

    byte rowPins [rows] = {1, 2, 3, 4};
    byte columnsPins [columns] = {5, 6, 7, 8};

    Keypad myKeypad = Keypad (makeKeymap(keyMap), rowPins, columnsPins, rows, columns);

    LiquidCrystal lcd (A0, A1, A2, A3, A4, A5);

    void setup() {

      lcd.begin(16, 2);
      pinMode(redLED, OUTPUT);
      pinMode(greenLED, OUTPUT);
      setLocked(true);
    }

    void loop() {

      char whichKey = myKeypad.getKey();

      lcd.setCursor (0,0);
      lcd.print ("Hello");
      lcd.setCursor(0,1);
      lcd.print("Please enter the password");

      if(whichKey == '*' || whichKey =='#'|| whichKey =='A' || whichKey =='B'|| whichKey =='C' || whichKey =='D') {
        positions=0;
        setLocked(true);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("The password is incorrect");
        delay(500);
        lcd.clear();    
        }

        if(whichKey == password [positions]) {
          positions ++;
          }

          if(positions == 4){
            setLocked(false);
            lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print("!Correct!");
            delay(3000);
            lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print("End Arduino");
            lcd.setCursor(0, 1);
            lcd.print("Thank you for trying me out")
            delay(5000);
            lcd.clear();     
            }
            delay(100);
    }        
    void setLocked(int locked)
      if(locked){
        digitalWrite(redLED, HIGH);
        digitalWrite(greenLED, LOW);
        }     
        else{
        digitalWrite(redLED, LOW);
        digitalWrite(greenLED, HIGH);
          }
    }
  • What do you think `boolean setLocked;` does? – Joseph Sible-Reinstate Monica Jun 11 '20 at 04:24
  • 3
    change the first line to `void setLocked(int locked);` – M.M Jun 11 '20 at 04:24
  • A function cannot be used in your code until it has first been declared (reading your code from top to bottom). If you look, you do not *define* `setLocked` until the very end of your code, but attempt to use it before that. C/C++ allow you to *declare* a function (which is a promise the function will be *defined* somewhere later in your code). So you can add a *forward-declaration* (a function prototype) as @M.M details above. Now that `setLocked` has been *declared* at the top, you are free to use it anywhere in the source file -- even though it isn't *defined* until the very end. – David C. Rankin Jun 11 '20 at 05:18

1 Answers1

2

Your compiler is right. setLocked is not declared. If you add a function in your sketch (from top to bottom) after you call it, you had to add a prototype for the function at the top:

void setLocked(int locked);

Now you can use the function everywhere in your sketch.

Mike
  • 4,041
  • 6
  • 20
  • 37