Author Topic: Arduino - 'setLocked' was not declared in this scope  (Read 3873 times)

0 Members and 1 Guest are viewing this topic.

Offline Antonio00Topic starter

  • Newbie
  • Posts: 1
  • Country: hr
Arduino - 'setLocked' was not declared in this scope
« on: October 26, 2017, 05:22:32 pm »
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:

Code: [Select]
#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);
      }
    }
« Last Edit: October 26, 2017, 05:24:26 pm by Antonio00 »
 

Offline JoeO

  • Frequent Contributor
  • **
  • Posts: 527
  • Country: us
  • I admit to being deplorable
Re: Arduino - 'setLocked' was not declared in this scope
« Reply #1 on: October 26, 2017, 07:33:42 pm »
I think you are missing 2 closing braces before the setLocked function.  These 2 close out the loop()
The day Al Gore was born there were 7,000 polar bears on Earth.
Today, only 26,000 remain.
 

Offline TK

  • Super Contributor
  • ***
  • Posts: 1722
  • Country: us
  • I am a Systems Analyst who plays with Electronics
Re: Arduino - 'setLocked' was not declared in this scope
« Reply #2 on: October 26, 2017, 08:36:11 pm »
The first FOR loop is not closed before the 2nd FOR loop... I think you are missing closing brackets but they are not necessarily before the setLocked function definition, they seem to be missing somewhere else...
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9893
  • Country: us
Re: Arduino - 'setLocked' was not declared in this scope
« Reply #3 on: October 26, 2017, 09:15:10 pm »
I'm not sure about the Arduino IDE but in the real world, setLocked() would need a forward declaration before main() OR it would need to be moved to a position in the file above main()

It is not declared in the scope because it is not declared before it is used.
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12875
Re: Arduino - 'setLocked' was not declared in this scope
« Reply #4 on: October 26, 2017, 10:12:12 pm »
The Arduino IDE parses the .ino file to generate a .cpp file and add auto-generated forward declarations for all functions and global variables.  Unfortunately it's code munging routine barfs badly on quite a few normal C++ or C constructs. . . .
 

Offline PeterG

  • Frequent Contributor
  • **
  • Posts: 831
  • Country: au
Re: Arduino - 'setLocked' was not declared in this scope
« Reply #5 on: October 26, 2017, 10:43:59 pm »

From what i can see you are calling setLocked with a boolean variable when the setLocked function requires an Integer.

Try changing the function from void setLocked(int locked) to void setLocked(boolean locked)

Testing one two three...
 

Offline alsetalokin4017

  • Super Contributor
  • ***
  • Posts: 2055
  • Country: us
Re: Arduino - 'setLocked' was not declared in this scope
« Reply #6 on: October 26, 2017, 11:32:54 pm »
This compiled for me without errors (but some warnings that shouldn't affect functioning) once I corrected a few things:

#include Servo.h
Servo myservo
initialize "pos" variable
add some curly brackets to finish loop() before setLocked function declaration


The easiest person to fool is yourself. -- Richard Feynman
 

Offline alsetalokin4017

  • Super Contributor
  • ***
  • Posts: 2055
  • Country: us
Re: Arduino - 'setLocked' was not declared in this scope
« Reply #7 on: October 29, 2017, 06:33:35 pm »
Er.... you're welcome ?       :rant:
The easiest person to fool is yourself. -- Richard Feynman
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf