Like many before: function not declared

I keep getting that my functions are not declared. As far as I know, function do not have to be declared ( if it is good practice in C I am willing to do it, btw). First my code compiled an ran, but now it does not compile and I keer getting the famous arror "xxxx is not declared in this scope" (in my case: 'freqSet' was not declared in this scope).

Below my code for the Arduino UNO. The overall intention of this sketch is that the freqTest function is called if the freqPin is high, which sets the devider. If someone is detected within 50 cm of the sensor and noone was detected in the previous run, the bingoTest function must be called.

Possebly there is a lot wrong with the code, but I keep experimenting and thereby learning (however advice is always welcom). But I absolutely need help with solving the "not declared" problem. THANK YOU!!!

The many serial.print statements are for monitoring the code, a sort of human debugging :slight_smile:

#include <NewPing.h>

int trigPin = 11; // select the trigger pin for ultrasone sensor
int echoPin = 12; // select the echo pin for ultrasone sensor
int freqPin = A0; // select the input pin for the frequency setting potentiometer
int alarmPin = 13; // select the pin for the LED
int freqsetPin = 3; // select the pin for button for setting the frequency
int freqValue = 20; // variable to store the value coming from the frequncy setting
int bingoValue = 0;
int currentState = 0;
int previousState = 0;

void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(freqPin, INPUT);
pinMode(alarmPin, OUTPUT);
pinMode(freqsetPin, INPUT);
Serial.begin (9600);
delay (1000);
}

void loop()
{
delay (5000);
if (digitalRead(freqsetPin) == 1)
{
freqSet();
}
Serial.println("Measuring distance");
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1; // convert the time into a distance
Serial.print("The distance is ");
Serial.print(distance);
Serial.println(" cm");

if (distance <= 50)
{
currentState = 1;
Serial.print("currentState = ");
Serial.println(currentState);
}
else
{
Serial.println("Else");
currentState = 0;
previousState = 0;
}

if (currentState = 1)
{
if (previousState = 0)
{
Serial.println("bingoTest");
bingoTest();
}
}

void freqSet()
{
Serial.println("Setting frequency");
int readValue = analogRead(freqPin);
freqValue = map(readValue, 0, 1023, 2, 20);
}

void bingoTest()
{
unsigned long bingo = millis();
bingoValue = (bingo / freqValue);
if (bingoValue % freqValue == 0)
{
Serial.println("Person detected");
digitalWrite(alarmPin, HIGH);
delay (2000);
digitalWrite(alarmPin, LOW);
previousState = 1;
}
}

Count your "{" and "}"...

Regards,
Ray L.

You can't define a function like freqSet() INSIDE another function like loop(). The other errors that you didn't quote told you that you were trying to put function definitions where they were not legal.

Steve

All executable code must be in a function. No function may be inside another function. Do what RayLivingston suggested. Start at the top of the code and go down. Count +1 for each { and count -1 for each } . You start at zero and should end at zero. You should NEVER go negative, and you should be at zero at the start of every function

If the count is wrong, you need to fix the { or } .

Even easier, put each { and } on its own line and Auto Format the code in the IDE. Now look at the pairs of {}. Is the last one on the left margin and the one before it tabbed in ? They should be

You can even configure Auto Format to move the { and } onto their own line if you want so you don't have to do it.

  if (currentState = 1)
  {
    if (previousState = 0)

Oops2

Please remember to use code tags when posting code.

Assignment is one equals sign =.
Comparison is two equals signs ==.

Also, states are often defined by an enum so that numbers do not have to be remembered.

Maybe I missed it, but I do not see how previousState can be anything other than zero. What is the point?

First thanks to all for the most of all quick and also constructive responses. It was my very first question on the forum. So a nice experience. In the 80's I did a training in assembler (Z80 with the Microprofessor). That's all. Since some time I am tinkering with Arduino.

Regarding the question about 'previousState'. It might be that some 'logic' was lost in me trying all kind of things to get it working. I'll check tomorrow the things you all suggested and I will check the logic and post an update. But briefly, but most lightly clear allready:

currentState = 1 if someone is detected / within range
previousState = is 1 if currentState has not yet been 0. To check currentState against to prevent repeating alarm (Bingo) on same person.

I first drawn a flowchart in Visio to "get the picture" and worked from there. I try to remind me to post both, flowchart and hopefully working code.

As promissed: here the flowchart (as attachment) and the now compiling code.
Thanks again and any comment is welcom.

#include <NewPing.h>

int trigPin = 11;       // select the trigger pin for ultrasone sensor
int echoPin = 12;       // select the echo pin for ultrasone sensor
int freqPin = A0;       // select the input pin for the frequency setting potentiometer
int alarmPin = 13;      // select the pin for the LED
int freqsetPin = 3;     // select the pin for button for setting the frequency
int freqValue = 20;     // variable to store the value coming from the frequncy setting
int bingoValue = 0;     // Is millis devided by the set frequency value
int currentState = 0;   // Is one if a person is detected
int previousState = 0;  // Remains one if a person was detected until current value goes zero
int duration = 0;       // Duration of echo from ultrasone range finder
int distance = 0;       // Duration of echo US range finder translated into distance

void setup()
{
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(freqPin, INPUT);
  pinMode(alarmPin, OUTPUT);
  pinMode(freqsetPin, INPUT);
  Serial.begin (9600);
  delay (1000);
}

void loop()
{
  if (digitalRead(freqsetPin) == 0)   // Checks for need to reset frequency or start with standard 1:20
  {
    freqSet();
  }
  measure ();                         // Calls function for measuring to astablish if person is present
  if (distance <= 50)
  {
    currentState = 1;                 // If person is in range, set to one
  }
  else
  {
    Serial.println("Else");           // If person not in range set to zero and reset previousstate to zero
    currentState = 0;
    previousState = 0;
  }

  if (currentState = 1)
  {
    if (previousState = 0)            // If zero continu. If previous state is still one chances are it is still the same person, so skip bingotest
    {
      bingoTest();
    }
  }
}

  void measure ()
  {
    Serial.println("Measuring distance");
    long duration, distance;
    digitalWrite(trigPin, LOW);
    delayMicroseconds(5);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    pinMode(echoPin, INPUT);
    duration = pulseIn(echoPin, HIGH);
    distance = (duration / 2) / 29.1;     // convert time into distance
    Serial.print("The distance is ");
    Serial.print(distance);
    Serial.println(" cm");
  }


  void freqSet()
  {
    Serial.println("Setting frequency");
    int readValue = analogRead(freqPin);
    freqValue = map(readValue, 0, 1023, 2, 20);
  }

  void bingoTest()
  {
    Serial.println("Change generation");
    unsigned long bingo = millis();
    bingoValue = (bingo / freqValue);
    if (bingoValue % freqValue == 0)
    {
      Serial.println("Bingo");
      digitalWrite(alarmPin, HIGH);
      delay (2000);
      digitalWrite(alarmPin, LOW);
      previousState = 1;
    }
  }

  if (currentState = 1)
  {
    if (previousState = 0)

See reply #5

Yep, dumb, == :-[
But it compiled ! :smiley:

Thnx

FredBD:
Yep, dumb, == :-[
But it compiled ! :smiley:

Yep, the compiler only checks for legal syntax, it doesn't check if it's sensible!

Yet another case of these blasted computers only doing what you asked not what you really wanted.

Steve

slipstick:
Yet another case of these blasted computers only doing what you asked not what you really wanted.

Yep, one day perhaps we will get a computer that does what we want it to do rather than what we tell it to do.

Maybe a long wait though.

Wow :o , that will expose a lot op people....

FredBD:
Yep, dumb, == :-[
But it compiled ! :smiley:

Thnx

Well, it generated a warning, but you didn't read the warnings...

( This is why you should never turn off compiler warnings... The IDE allows them to be hidden, which is bad practice )

If you mean the orange text showing up at the bottom; I did not turn that off or hidden it but, and there you are right,..... I did not read it. Lesson learned (till it happens again - human, only human).

As promised earlier, here the working code (in the previous version only the compiling issue was solved). The various Print statements are for checking the working via the serial monitor and can be removed:

#include <NewPing.h>

int trigPin = 11;       // select the trigger pin for ultrasone sensor
int echoPin = 12;       // select the echo pin for ultrasone sensor
int freqPin = A0;       // select the input pin for the frequency setting potentiometer
int alarmPin = 13;      // select the pin for the LED
int freqsetPin = 3;     // select the pin for button for setting the frequency
long freqValue = 20;     // variable to store the value coming from the frequncy setting
int bingoValue = 0;     // Is millis devided by the set frequency value
int currentState = 0;   // Is one if a person is detected
int previousState = 0;  // Remains one if a person was detected until current value goes zero
long duration = 0;       // Duration of echo from ultrasone range finder
long distance = 0;       // Duration of echo US range finder translated into distance

void setup()
{
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(freqPin, INPUT);
  pinMode(alarmPin, OUTPUT);
  pinMode(freqsetPin, INPUT);
  Serial.begin (9600);
  delay (1000);
}

void loop()
{
  delay (3000);
  if (digitalRead(freqsetPin) == 1)   // Checks for need to reset frequency or start with standard 1:20
  {
    Serial.println("freqSet called");
    freqSet();
  }
  measure();                         // Calls function for measuring to astablish if person is present
  
  if (distance <= 50)
  {
    currentState = 1;                 // If person is in range, set to one
    Serial.print("Person in range ");
    Serial.println(currentState);
    if (previousState == 0)            // If zero continu. If previous state is still one chances are it is still the same person, so skip bingotest
    {
      Serial.println("bingoTest");
      bingoTest();
      previousState = 1;
    }
  }
  else
  {
    Serial.println("Person out of range");           // If person not in range set to zero and reset previousstate to zero
    currentState = 0;
    previousState = 0;
  }
}

  void measure()
  {
    Serial.println("Measuring distance");
    digitalWrite(trigPin, LOW);
    delayMicroseconds(5);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    pinMode(echoPin, INPUT);
    duration = pulseIn(echoPin, HIGH);
    distance = (duration/2) / 29.1;
    Serial.print("The distance is ");
    Serial.print(distance);
    Serial.println(" cm");
  }


  void freqSet()
  {
    int readValue = analogRead(freqPin);
    freqValue = map(readValue, 0, 1023, 2, 20);
    Serial.print("De frequency is set at: ");
    Serial.println(freqValue);
  }

  void bingoTest()
  {
    long bingo = millis();
    bingoValue = (bingo / freqValue);
    if (bingoValue % freqValue == 0)
    {
      Serial.println("Bingo");
      digitalWrite(alarmPin, HIGH);
      delay (2000);
      digitalWrite(alarmPin, LOW);
      previousState = 1;
    }
  }