Missing the first two tones
Hi
In this program only the last if is executed, no idea why first two are not?
const int numReadings = 30;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = PA6;
int Voltage = 0;
void setup()
{
Serial.begin(115200);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop()
{
int analog_value = analogRead(PA6);
Voltage = getVPP();
total = total - readings[readIndex];
//readings[readIndex] = analogRead(inputPin);
readings[readIndex] = Voltage;
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings) {
readIndex = 0;
}
average = total / numReadings;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if (average > 50 && average < 100 )
{
tone(PA3, 700); // 700 Hz missing
}
if (average > 100 && average < 200 )
{
tone(PA3, 900); // 900 Hz missing
}
if (average > 20 && average < 300 )
{
tone(PA3, 1100);
}
else
{
noTone(PA3);
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Serial.print(" average = "); Serial.print(average);
Serial.println();
}
float getVPP()
{
int result;
int readValue; //value read from the sensor
int maxValue = 0; // store max value here
int minValue = 1024; // store min value here, 4095.0
//int minValue = 4095.0;
uint32_t start_time = millis();
while ((millis() - start_time) < 5)
{
readValue = analogRead(PA6);
if (readValue > maxValue)
{
maxValue = readValue;
}
if (readValue < minValue)
{
minValue = readValue;
}
}
result = (maxValue - minValue);
return result;
}