Arduino - 'setLocked' was not declared in this scope

I'm a beginner in Arduino and I am trying to make a safe using a keypad, lcd and servo. I'm using 2 codes combined from 2 different videos. Everything was working fine until I typed code for the servo motor( under if line). Now when I want to compile it's giving me error "setLocked was not declared in this scope". Help please.
This is my code:

#include <Keypad.h>

#include <LiquidCrystal.h> //include LCD library (standard library)
#include <Keypad.h> //include keypad library

#define redLED 10 //define the LED pins
#define greenLED 11

char* password = "1234"; //create a password
int pozisyon = 0; //keypad position

const byte rows = 4; //number of the keypad's rows and columns
const byte cols = 4;

char keyMap [rows] [cols] = { //define the symbols on the buttons of the keypad

  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins [rows] = {1, 2, 3, 4}; //pins of the keypad
byte colPins [cols] = {5, 6, 7, 8};

Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, rows, cols);

LiquidCrystal lcd (A0, A1, A2, A3, A4, A5); // pins of the LCD. (RS, E, D4, D5, D6, D7)

void setup() {

  lcd.begin(16, 2);
  pinMode(redLED, OUTPUT);  //set the LED as an output
  pinMode(greenLED, OUTPUT);
  setLocked (true); //state of the password
}

void loop() {

  char whichKey = myKeypad.getKey(); //define which key is pressed with getKey

  lcd.setCursor(0, 0);
  lcd.print("    Welcome");
  lcd.setCursor(0, 1);
  lcd.print(" Enter Password");

  if (whichKey == '*' || whichKey == '#' || whichKey == 'A' ||      //define invalid keys
      whichKey == 'B' || whichKey == 'C' || whichKey == 'D') {

    pozisyon = 0;
    setLocked (true);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("  Invalid Key!");
    delay(1000);
    lcd.clear();
  }
  if (whichKey == password [pozisyon]) {

    pozisyon ++;
  }
  if (pozisyon == 4) {
    setLocked (false);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("*** Verified ***");
    delay(2000);
    for (pos = 0; pos <= 180; pos += 1) {
      myservo.write(pos);
      delay(15);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Unlocked");
      lcd.setCursor(0, 1);
      lcd.print("--------");
      delay(7000);
      for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
        myservo.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                       // waits 15ms for the servo to reach the position
    lcd.clear();
      }
      delay(100);
    }

    void setLocked(int locked) {
      if (locked) {
        digitalWrite(redLED, HIGH);
        digitalWrite(greenLED, LOW);
      }
      else {
        digitalWrite(redLED, LOW);
        digitalWrite(greenLED, HIGH);
      }
    }

Hi. Nice job using Code Tags in your first post!!!

In the IDE type ctrl-t to have it properly format the indents in your code. Then look at your curly brackets. For every OPEN curly bracket (‘{‘) there needs to be a matching CLOSE curly bracket (‘}’). I have a feeling you have one or more unpaired brackets.

1 Like

If you put the cursor to the immediate right of any {, }, ( or ) the matching symbol (as the IDE sees matches) will be highlighted if there is a matching symbol. Also, if you put every { and } on its own line, mismatches are easier to see, especially after an autoformat operation.

If(something)
{
  doThis();
}
else
{
  doThat();
}

As said, check your curlies. It looks like loop() is not 'closed' and hence setLocked is defined inside loop() which is what the compiler why the compiler can't find it.

As a side note, is there a reason why you clear the lcd 180 times in a row? I suspect that this indicates that the for loops inside loop are missing some '}'

gfvalvo:
Hi. Nice job using Code Tags in your first post!!!

thanks :smiley:

sterretje:
As a side note, is there a reason why you clear the lcd 180 times in a row? I suspect that this indicates that the for loops inside loop are missing some '}'

It was probably a mistake, I have corrected it.

So I did everything you said, every loop is now closed and it's still showing this error. I think it might have something to do with the servo motor code.

#include <Keypad.h>

#include <LiquidCrystal.h> //include LCD library (standard library)
#include <Keypad.h> //include keypad library

#define redLED 10 //define the LED pins
#define greenLED 11

char* password = "1234"; //create a password
int pozisyon = 0; //keypad position

const byte rows = 4; //number of the keypad's rows and columns
const byte cols = 4;

char keyMap [rows] [cols] = { //define the cymbols on the buttons of the keypad

  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins [rows] = {1, 2, 3, 4}; //pins of the keypad
byte colPins [cols] = {5, 6, 7, 8};

Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, rows, cols);

LiquidCrystal lcd (A0, A1, A2, A3, A4, A5); // pins of the LCD. (RS, E, D4, D5, D6, D7)

void setup() {

  lcd.begin(16, 2);
  pinMode(redLED, OUTPUT);  //set the LED as an output
  pinMode(greenLED, OUTPUT);
  setLocked (true); //state of the password
}

void loop() {

  char whichKey = myKeypad.getKey(); //define which key is pressed with getKey

  lcd.setCursor(0, 0);
  lcd.print("    Welcome");
  lcd.setCursor(0, 1);
  lcd.print(" Enter Password");

  if (whichKey == '*' || whichKey == '#' || whichKey == 'A' ||      //define invalid keys
      whichKey == 'B' || whichKey == 'C' || whichKey == 'D') {

    pozisyon = 0;
    setLocked (true);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("  Invalid Key!");
    delay(1000);
    lcd.clear();
  }
  if (whichKey == password [pozisyon]) {

    pozisyon ++;
  }
  if (pozisyon == 4) {
    setLocked (false);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("*** Verified ***");
    delay(2000);
    for (pos = 0; pos <= 180; pos += 1) {
      myservo.write(pos);
      delay(15);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Unlocked");
      lcd.setCursor(0, 1);
      lcd.print("--------");
      delay(7000);
      for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
        myservo.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                       // waits 15ms for the servo to reach the position
      }
      delay(100);
    }
    lcd.clear();
  }


  void setLocked(int locked) {
    if (locked) {
      digitalWrite(redLED, HIGH);
      digitalWrite(greenLED, LOW);
    }
    else {
      digitalWrite(redLED, LOW);
      digitalWrite(greenLED, HIGH);
    }
  }

It is MUCH easier to see what } goes with what { if you put the { on a line by itself.

The loop() function is NOT closed. Use Tools + Auto Format. If it fails, you KNOW the curly braces are not matched.

Once you fix the brackets, you need to define your variables 'pos' and 'myservo'.

Shouldn't I see

#include <Servo.h>
Servo myservo;

somewhere?

Jacques

gfvalvo:
Once you fix the brackets, you need to define your variables 'pos' and 'myservo'.

jbellavance:
Shouldn't I see

#include <Servo.h>

Servo myservo;


somewhere?

Jacques

Thanks to both of you. I fixed the brackets and defined variables and now is everything working! :smiley: