Programming issues 2

Can someone please tell me why this won't verify, I have worked on this all day and still get the same error 'getDistance' was not declared in this scope

#include <MFRC522.h>
#include <SPI.h>
#include <LiquidCrystal.h>
#include <Keypad.h>

#define buzzer 8
#define trigPin 10
#define echoPin 9

#define RST_PIN   12     // Configurable, see typical pin layout above
#define SS_PIN    11    // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance

/* Set your new UID here! */
#define NEW_UID {0xDE, 0xAD, 0xBE, 0xEF}

MFRC522::MIFARE_Key key;

long duration;
int distance, initialDistance, currentDistance, i;
int screenOffMsg = 0;
String password = "1234";
String tempPassword;
boolean activated = false; // State of the alarm
boolean isActivated;
boolean activateAlarm = false;
boolean alarmActivated = false;
boolean enteredPassword; // State of the entered password to stop the alarm
boolean passChangeMode = false;
boolean passChanged = false;

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keypressed;
//define the cymbols on the buttons of the keypads
char keyMap[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {14, 15, 16, 17}; //Row pinouts of the keypad
byte colPins[COLS] = {18, 19, 20, 21}; //Column pinouts of the keypad

Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS, COLS);
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs=2,enable=3,d4=4,d5=5,d6=,d7=7)

void setup() {
  //
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, world!");

  pinMode(buzzer, OUTPUT); // Set buzzer as an output
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input

  Serial.begin(9600);  // Initialize serial communications with the PC
  while (!Serial);     // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  SPI.begin();         // Init SPI bus
  mfrc522.PCD_Init();  // Init MFRC522 card
  Serial.println(F("Warning: this example overwrites the UID of your UID changeable card, use with care!"));

  // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }
}

// Setting the UID can be as simple as this:
//void loop() {
//  byte newUid[] = NEW_UID;
//  if ( mfrc522.MIFARE_SetUid(newUid, (byte)4, true) ) {
//    Serial.println("Wrote new UID to card.");
//  }
//  delay(1000);
//}

// But of course this is a more proper approach
void loop() {

  // Look for new cards, and select one if present
  if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
    delay(50);
    return;
  }

  // Now a card is selected. The UID and SAK is in mfrc522.uid.

  // Dump UID
  Serial.print(F("Card UID:"));
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte[i], HEX);
  }
  Serial.println();

  // Dump PICC type
  //  MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  //  Serial.print(F("PICC type: "));
  //  Serial.print(mfrc522.PICC_GetTypeName(piccType));
  //  Serial.print(F(" (SAK "));
  //  Serial.print(mfrc522.uid.sak);
  //  Serial.print(")\r\n");
  //  if (  piccType != MFRC522::PICC_TYPE_MIFARE_MINI
  //    &&  piccType != MFRC522::PICC_TYPE_MIFARE_1K
  //    &&  piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
  //    Serial.println(F("This sample only works with MIFARE Classic cards."));
  //    return;
  //  }

  // Set new UID
  byte newUid[] = NEW_UID;
  if ( mfrc522.MIFARE_SetUid(newUid, (byte)4, true) ) {
    Serial.println(F("Wrote new UID to card."));
  }

  // Halt PICC and re-select it so DumpToSerial doesn't get confused
  mfrc522.PICC_HaltA();
  if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
    return;
  }

  // Dump the new memory contents
  Serial.println(F("New UID and contents:"));
  mfrc522.PICC_DumpToSerial(&(mfrc522.uid));

  delay(2000);

  if (activateAlarm);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Alarm will be");
  lcd.setCursor(0, 1);
  lcd.print("activated in");

  int countdown = 8; // 8 seconds count down before activating the alarm
  while (countdown != 0) {
    lcd.setCursor(13, 1);
    lcd.print(countdown);
    countdown--;
    tone(buzzer, 100, 100);
    delay(1000);
  }
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Alarm Activated!");
  initialDistance = getDistance();
  activateAlarm = false;
  alarmActivated = true;
}
if (alarmActivated == true);
currentDistance = getDistance() + 10;
if ( currentDistance < initialDistance);
tone(buzzer, 1000); // Send 1KHz sound signal
lcd.clear();
enterPassword();

if (!alarmActivated);
if (screenOffMsg == 0 );
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("A - Activate");
lcd.setCursor(0, 1);
lcd.print("B - Change Pass");
screenOffMsg = 1;

keypressed = myKeypad.getKey();
if (keypressed == 'A');       //If A is pressed, activate the alarm
tone(buzzer, 1000, 200);
activateAlarm = true;
}
else if (keypressed == 'B') {
  lcd.clear();
  int i = 1;
  tone(buzzer, 2000, 100);
  tempPassword = "";
  lcd.setCursor(0, 0);
  lcd.print("Enter Password");
  lcd.setCursor(0, 1);
  lcd.print(">");
  passChangeMode = true;
  passChanged = true;
  while (passChanged) {
    keypressed = myKeypad.getKey();
    if (keypressed != NO_KEY) {
      if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
          keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
          keypressed == '8' || keypressed == '9' ) {
        tempPassword += keypressed;
        lcd.setCursor(i, 1);
        lcd.print("*");
        i++;
        tone(buzzer, 2000, 100);
      }
    }
    if (i > 5 || keypressed == '#') {
      tempPassword = "";
      i = 1;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Enter Password");
      lcd.setCursor(0, 1);
      lcd.print(">");
    }
    if ( keypressed == '*') {
      i = 1;
      tone(buzzer, 2000, 100);
      if (password == tempPassword) {
        tempPassword = "";
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Set New Password");
        lcd.setCursor(0, 1);
        lcd.print(">");
        while (passChangeMode) {
          keypressed = myKeypad.getKey();
          if (keypressed != NO_KEY) {
            if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
                keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
                keypressed == '8' || keypressed == '9' ) {
              tempPassword += keypressed;
              lcd.setCursor(i, 1);
              lcd.print("*");
              i++;
              tone(buzzer, 2000, 100);
            }
          }
          if (i > 5 || keypressed == '#') {
            tempPassword = "";
            i = 1;
            tone(buzzer, 2000, 100);
            lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print("Set New Password");
            lcd.setCursor(0, 1);
            lcd.print(">");
          }
          if ( keypressed == '*') {
            i = 1;
            tone(buzzer, 2000, 100);
            password = tempPassword;
            passChangeMode = false;
            passChanged = false;
            screenOffMsg = 0;
          }
        }
      }
    }
  }
}
}
}
void
enterPassword() {
  int k = 5;
  tempPassword = "";
  activated = true;
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(" *** ALARM *** ");
  lcd.setCursor(0, 1);
  lcd.print("Pass>");
  while (activated) {
    keypressed = myKeypad.getKey();
    if (keypressed != NO_KEY) {
      if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
          keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
          keypressed == '8' || keypressed == '9' ) {
        tempPassword += keypressed;
        lcd.setCursor(k, 1);
        lcd.print("*");
        k++;
      }
    }
    if (k > 9 || keypressed == '#') {
      tempPassword = "";
      k = 5;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print(" *** ALARM *** ");
      lcd.setCursor(0, 1);
      lcd.print("Pass>");
    }
    if ( keypressed == '*') {
      if ( tempPassword == password ) {
        activated = false;
        alarmActivated = false;
        noTone(buzzer);
        screenOffMsg = 0;
      }
      else if (tempPassword != password) {
        lcd.setCursor(0, 1);
        lcd.print("Wrong! Try Again");
        delay(2000);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print(" *** ALARM *** ");
        lcd.setCursor(0, 1);
        lcd.print("Pass>");
      }
    }
  }
}
// Custom function for the Ultrasonic sensor
long getDistance() {
  //int i=10;

  //while( i<=10 ) {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2;
  //sumDistance += distance;
  //}
  //int averageDistance= sumDistance/10;
  return distance;
}

If You had been reading the very first topic telling how to use this forum You would have used autoformat in the IDE and code tags when pasting. Then we could easily copy Your code and analyze it.

Try long getDistance(void) {........

Please

In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.

Reply #2 & #4, And it most likely show up that you are missing a curly brace somewhere before the declaration of 'GetDistance()'

Welcome

Reindent your code, you will quickly find the problem

You got a few of these in there too. Not doing what you want, almost certainly.

a7

Thank you very much

My apologies

formatted and reposted

You (still) have several of this kind of thing, FIX THEM.

if (!alarmActivated);

And your braces are srsly out of balance, like you have missing braces or extra braces. In your loop() function.

They (may) balance in your other functions, do at a glance, but there's no telling, with the kind of mistakes you are making with basic syntax, whether they are doing what and only what you want them to.

Get on your hands and knees and go through your code looking at where your statements begin and end, and how braces are helping or hurting you.

The "not declared getDistance" is a red herring, not where you need to woory about, the result of the fact that your loop is a syntactic nightmare badly formed.

In the IDE there's a little box with a '-' sign in it at the margin at the beginning of blocks of code. Clicking on one will collapse the block - click on the one at the beginning of you loop() function and you will see a bunch of orphan code that you think is part of the loop but simply is not.

HTH

a7

As it is now, 'loop()' end here :

  alarmActivated = true;
}  // here this is the closing brace of loop
if (alarmActivated == true);

This is what the indentation is showing.
This is probably due to an opening brace missing here :

  alarmActivated = true;
}
if (alarmActivated == true);  // the semicolon should probably be replaced by an opening curly '{'

Fix that, do the ctrl-t thing again and see if you find more of those things.

Thanks everyone for the help. I got it all to verify.

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