Two copies of the same code. One works, the other doesn't

I am doing an online class and currently I am learning how to use a 4x4 keypad as a passcode. I copied the example code the class gave me perfectly and when I went to verify it gave me the: "'validatePIN' was not declared in this scope" validatePIN is never declared in the example code. Then I decided to copy paste the example code into a sketch and it verified where my code did not. I have no idea how this could happen. If I made a mistake and my code is not the same please let me know. Here is the code.

#include <Keypad.h>
#include "Arduino.h"
const byte ROWS = 4;
const byte COLS = 4;

const byte PIN_LENGTH = 4; //pin code is 4 button presse
char current_pin[PIN_LENGTH] = { '0', '0', '0', '0' }; 
/*
 * Character Type (char)
 * 
 * In Arduino C++ we have previously used strings of characters using double quotes
 * ("my string"). But for a single character we need to use single quotes to indicate that there 
 * is just ONE character ('A').
 */
 const char BUTTONS[ROWS][COLS] = {
  {'1', '2', '3', 'A' }, 
  {'4', '5', '6', 'B' },
  {'7', '8', '9', 'C' },
  {'*', '0', '#', 'D' },
};

const byte ROWS_PINS[ROWS] = {5, 4, 3, 2 };
const byte COLS_PINS[COLS] = {6, 7, 8, 9 };
//Define row and column pins driving the keypad

Keypad heroKeypad = Keypad(makeKeymap(BUTTONS), ROWS_PINS, COLS_PINS, ROWS, COLS );

const byte BUZZER_PIN = 10;




void setup() {
  pinMode(BUZZER_PIN, OUTPUT);

  Serial.begin(9600);
  delay(200); // delay a brief period to let things settle before displaying prompt.
  Serial.println("Press * to enter new PIN or # to access the system.");
}

void loop() {
 char button_character = heroKeypad.waitForKey();

 //serial.println(button_character);
 tone(BUZZER_PIN, 880, 100);

 if (button_character == '#') { //button to access the system
  bool access_allowed = validatePIN();
  if (access_allowed) {
    Serial.println ("Welcome, authorized user. You may now begin using the system.");
  }} else {
    Serial.println("Access Denied. ");
    Serial.println("\nPress * to enter new PIN or # to access the system. ");
  }
 }

 if (button_character == '*') { //button to change PIN
  bool access_allowed = validatePIN();

  if (access_allowed) {
    Serial.println("WELCOME, Authorized user. Please enter a new PIN: ");

    for (int i = 0; i < PIN_LENGTH; i++) {
      button_character = heroKeypade.waitForKey();
      tone(BUZZER_PIN, 880, 100);

      curren_pin[i] = button_character;
      Serial.print("*");
    }

    Serial.println(); // add new line after last asterisk so next message is on next line
    Serial.println("PIN Successfully Changed! ");
  } else{
    Serial.println("ACCESS DENIED. Cannot change PIN without the old or default PIN.")
    Serial.println("\nPress * to enter new PIN or # to access the system.");
    // \n means "new line"
    
    }
  }
}

/*
 * This function prompts the used ot enter a PIN and returns true or false depending
 * on whether the PIN mathces our saved PIN.
 * 
 * NOTE: this function indroduces the concept of a function that returns a signle 
 *       value. First, we must indicate what type of value that will be returned
 *       in the initial fintion declaration. the "bool" at the beginning of
 *       the declaration indicates that this function will return a "boolean" value
 *       of either true or false. 
 *     
 */

 bool validatePIN() {
  Serial.println("Enter PIN to continue.");

  for (int i = 0; i < PIN_LENGTH; i++) {
    char button_character = heroKeypad.waitForKey();
    tone (BUZZER_PIN, 880, 100);

    if (current_pin[i] != button_character) {
      Serial.prinln();
      Serial.print("WRONG PIN DIGIT: ");
      Serial.println(button_character);
      return false; //reutrn false and exit function
    }
    Serial.print("*");
  }

  Serial.println(); //add new lline afer last asterisk so next message is on new line
  Serial.println("Deivice Successfully Unlocked!");
  return true;
 }
    
  }

And this is what the example lesson looks like:


#include "Arduino.h"

// Include Keypad library#include <Keypad.h>
#include <Keypad.h>

// Our HERO keypad has 4 rows, each with 4 columns.
const byte ROWS = 4;
const byte COLS = 4;

const byte PIN_LENGTH = 4;    // PIN code is 4 button presses
char current_pin[PIN_LENGTH] = { '0', '0', '0', '0' }; // Initial PIN is four zeros.

// Define what characters will be returned by each button
const char BUTTONS[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};

// Define row and column pins connected to the keypad
const byte ROW_PINS[ROWS] = { 5, 4, 3, 2 };
const byte COL_PINS[COLS] = { 6, 7, 8, 9 };

Keypad heroKeypad = Keypad(makeKeymap(BUTTONS), ROW_PINS, COL_PINS, ROWS, COLS);

const byte BUZZER_PIN = 10;  // pin 10 drives the buzzer

void setup() {
  pinMode(BUZZER_PIN, OUTPUT);

  Serial.begin(9600);  // Begin monitoring via the serial monitor
  delay(200);          // Delay a brief period to let things settle before displaying prompt.
  Serial.println("Press * to enter new PIN or # to access the system.");
}

void loop() {
  char button_character = heroKeypad.waitForKey();

  // Serial.println(button_character);
  tone(BUZZER_PIN, 880, 100);

  if (button_character == '#') {  // button to access system
    bool access_allowed = validatePIN();
    if (access_allowed) {
      Serial.println("Welcome, authorized user. You may now begin using the system.");
    } else {
      Serial.println("Access Denied.");
      Serial.println("\nPress * to enter new PIN or # to access the system.");
    }
  }

  if (button_character == '*') {  // button to change PIN
    bool access_allowed = validatePIN();

    if (access_allowed) {
      Serial.println("Welcome, authorized user. Please Enter a new PIN: ");

      for (int i = 0; i < PIN_LENGTH; i++) {
        button_character = heroKeypad.waitForKey();
        tone(BUZZER_PIN, 880, 100);

        current_pin[i] = button_character;
        Serial.print("*");
      }

      Serial.println();  // add new line after last asterisk so next message is on next line
      Serial.println("PIN Successfully Changed!");
    } else {
      Serial.println("Access Denied. Cannot change PIN without the old or default.");
      Serial.println("\nPress * to enter new PIN or # to access the system.");
    }
  }
}

/*
 * This function prompts the user to enter a PIN and returns true or false depending
 * on whether the PIN matches our saved PIN.
 *
 * NOTE: this function introduces the concept of a function that returns a single
 *       value.  First, we must indicate what type of value that will be returned
 *       in the initial function declaration.  The "bool" at the beginning of
 *       the declaration indicates that this function will return a "boolean" value
 *       of either true or false.
 */

bool validatePIN() {
  Serial.println("Enter PIN to continue.");

  for (int i = 0; i < PIN_LENGTH; i++) {
    char button_character = heroKeypad.waitForKey();
    tone(BUZZER_PIN, 880, 100);

    if (current_pin[i] != button_character) {
      Serial.println();  // start next message on new line
      Serial.print("WRONG PIN DIGIT: ");
      Serial.println(button_character);
      return false;  // return false and exit function
    }
    Serial.print("*");
  }

  Serial.println();  // add new line after last asterisk so next message is on next line
  Serial.println("Device Successfully Unlocked!");
  return true;
}

I will try that, also, what are braces?

just tried it, it said "no changes necessary for autoformat"

Check that each function has a single opening brace and a single closing brace (HINT : they don't)

If you Auto format your sketch in the IDE the extra closing braces will show up easier

You also have code outside of any function such as

if (button_character == '*')
{  //button to change PIN
    bool access_allowed = validatePIN();

    if (access_allowed)
etc, etc

Where does the loop() function end ?
Where should it end ?

That was a typo. That was the issue. Now it verifies after some fixed spelling mistakes. Thank you for helping me fix this and teaching me Command T!

Yes it is.

 bool validatePIN() {
  Serial.println("Enter PIN to continue.");

  for (int i = 0; i < PIN_LENGTH; i++) {

It's a function, but you messed up somewhere and it isn't presented properly, most likely because, as has been pointed out, a mismatch or extra or missing brace.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.