Project 12 - checkForKnock was not declared in this scope

The error message seems to be saying that I must declare the function before calling it, but the instructions tell me to declare this particular function after calling it. I am to call it on line 40, but declare it on line 57.

But I keep getting the error message 'checkForKnock was not declared in this scope.'

What is going on?

What project 12 ?

What is going on?

Please post the actual code that you are compiling. Before you do please Auto Format it in the IDE and after copying into a post here add [­code­]Your code here[­/code­] tags to it.

#include <Servo.h>
Servo myServo;
const int piezo = A0;
const int switchPin = 2;
const int yellowLed = 3;
const int greenLed = 4;
const int redLed = 5;
int knockVal;
int switchVal;
const int quietKnock = 10;
const int loudKnock = 100;

boolean locked = false;
int numberOfKnocks = 100;

void setup() {
// put your setup code here, to run once:
myServo.attach(9);
pinMode(yellowLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(switchPin, INPUT);
Serial.begin(9600);
digitalWrite(greenLed , HIGH);
myServo.write(0);
Serial.println(“The box is unlocked!”);
}

void loop() {
if (locked == false) {
switchVal = digitalRead(switchPin);
if (switchVal == HIGH) {
locked = true;
digitalWrite(greenLed, LOW);
digitalWrite(redLed, HIGH);
myServo.write(90);
Serial.println(“The box is locked!”);
delay (1000);
}
}
if (locked == true) {
knockVal = analogRead(piezo);
if (numberOfKnocks < 3 && knockVal > 0) {
if (checkForKnock(knockVal) == true) {
numberOfKnocks++;
}
Serial.print(3 - numberOfKnocks);
Serial.println(" more knocks to go");
if (numberOfKnocks >= 3) {
locked = false;
myServo.write(0);
delay(20);
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, LOW);
Serial.println(“The box is unlocked!”);
numberOfKnocks = 0;
}
}
}
boolean checkForKnock(int value) {
if (value > quietKnock && value < loudKnock) {
digitalWrite(yellowLed, HIGH);
delay(50);
digitalWrite(yellowLed, LOW);
Serial.print("Valid knock of value ");
Serial.println(value);
return true;
}
else {
Serial.print("Bad knock value ");
Serial.println(value);
return false;
}
}
}

Any ideas?

WesHedrick: Any ideas?

Didn't read UKHeliBob's comment, did you?

I wish they wouldn't teach newbies to rely on the IDE to generate function prototypes for them. Especially when it doesn't always work.

OP. Add this line above setup function: boolean checkForKnock(int); and see if it compiles.

The real problem is that ‘checkForKnock()’ has been defined inside the ‘loop()’.

@WesHedrick, if you get into the habit of using “Auto Format”, problems like this will stand out.
After moving it outside ‘loop()’, the code compiles fine. even without a forward declaration:-

#include <Servo.h>
Servo myServo;
const int piezo = A0;
const int switchPin = 2;
const int yellowLed = 3;
const int greenLed = 4;
const int redLed = 5;
int knockVal;
int switchVal;
const int quietKnock = 10;
const int loudKnock = 100;

boolean locked = false;
int numberOfKnocks = 100;

void setup()
{
    // put your setup code here, to run once:
    myServo.attach(9);
    pinMode(yellowLed, OUTPUT);
    pinMode(redLed, OUTPUT);
    pinMode(greenLed, OUTPUT);
    pinMode(switchPin, INPUT);
    Serial.begin(9600);
    digitalWrite(greenLed , HIGH);
    myServo.write(0);
    Serial.println("The box is unlocked!");
}

void loop()
{
    if (locked == false)
    {
        switchVal = digitalRead(switchPin);
        if (switchVal == HIGH)
        {
            locked = true;
            digitalWrite(greenLed, LOW);
            digitalWrite(redLed, HIGH);
            myServo.write(90);
            Serial.println("The box is locked!");
            delay (1000);
        }
    }
    if (locked == true)
    {
        knockVal = analogRead(piezo);
        if (numberOfKnocks < 3 && knockVal > 0)
        {
            if (checkForKnock(knockVal) == true)
            {
                numberOfKnocks++;
            }
            Serial.print(3 - numberOfKnocks);
            Serial.println(" more knocks to go");
            if (numberOfKnocks >= 3)
            {
                locked = false;
                myServo.write(0);
                delay(20);
                digitalWrite(greenLed, HIGH);
                digitalWrite(redLed, LOW);
                Serial.println("The box is unlocked!");
                numberOfKnocks = 0;
            }
        }
    }
}

boolean checkForKnock(int value)
{
    if (value > quietKnock && value < loudKnock)
    {
        digitalWrite(yellowLed, HIGH);
        delay(50);
        digitalWrite(yellowLed, LOW);
        Serial.print("Valid knock of value ");
        Serial.println(value);
        return true;
    }
    else
    {
        Serial.print("Bad knock value ");
        Serial.println(value);
        return false;
    }
}

And if you get into the habit of using code tags when you post code, as I’ve done, you’ll make potential helpers much happier and more likely to actually put in the time to help you. :wink:

You can’t define one function inside another one. Move checkforknock outside the loop().

quick question: how and where does the code read in the numberOfKnocks?

quynguyen219:
quick question: how and where does the code read in the numberOfKnocks?

    knockVal = analogRead(piezo);

reads the value from the knock sensor

     if (checkForKnock(knockVal) == true)
      {
        numberOfKnocks++;

checkForKnock() returns true if the value was high enough to have been a knock and if so numberOfKnocks is incremented