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
#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;
}
}