Arduino - 'setLocked' was not declared in this scope - Page 1
Forum section: electronics / general

author: Antonio00
author_rank: Newbie
timestamp: October 26, 2017, 05:22:32 pm
content: 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:#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 11char* password = "1234"; //create a passwordint pozisyon = 0; //keypad positionconst byte rows = 4; //number of the keypad's rows and columnsconst byte cols = 4;char keyMap   = { //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  = {1, 2, 3, 4}; //pins of the keypadbyte colPins  = {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 outputpinMode(greenLED, OUTPUT);setLocked (true); //state of the password}void loop() {char whichKey = myKeypad.getKey(); //define which key is pressed with getKeylcd.setCursor(0, 0);lcd.print(" Welcome");lcd.setCursor(0, 1);lcd.print(" Enter Password");if (whichKey == '*' || whichKey == '#' || whichKey == 'A' || //define invalid keyswhichKey == '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 ++;}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 degreesmyservo.write(pos); // tell servo to go to position in variable 'pos'delay(15); // waits 15ms for the servo to reach the positionlcd.clear();}delay(100);}void setLocked(int locked) {if (locked) {digitalWrite(redLED, HIGH);digitalWrite(greenLED, LOW);}else {digitalWrite(redLED, LOW);digitalWrite(greenLED, HIGH);}}

author: JoeO
author_rank: Frequent Contributor
timestamp: October 26, 2017, 07:33:42 pm
content: I think you are missing 2 closing braces before the setLocked function. These 2 close out the loop()

author: TK
author_rank: Super Contributor
timestamp: October 26, 2017, 08:36:11 pm
content: 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...

author: rstofer
author_rank: Super Contributor
timestamp: October 26, 2017, 09:15:10 pm
content: 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.

author: Ian.M
author_rank: Super Contributor
timestamp: October 26, 2017, 10:12:12 pm
content: 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. . . .

author: PeterG
author_rank: Frequent Contributor
timestamp: October 26, 2017, 10:43:59 pm
content: 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)

author: alsetalokin4017
author_rank: Super Contributor
timestamp: October 26, 2017, 11:32:54 pm
content: This compiled for me without errors (but some warnings that shouldn't affect functioning) once I corrected a few things:#include Servo.hServo myservoinitialize "pos" variableadd some curly brackets to finish loop() before setLocked function declaration

author: alsetalokin4017
author_rank: Super Contributor
timestamp: October 29, 2017, 06:33:35 pm
content: Er.... you're welcome ?