Missing the first two tones

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

The if conditions are not met.

actually may be not in that specific case.

if you look at your 3 if statements, they check if the average is within an interval

1st if: average within [50, 100]
2nd if: average within [100, 200]
3rd if: average within [20, 300]

so the last if is true when the first or second case is true.

the tone function without a duration lasts until the next tone command or a noTone() command is issued.
so what happens, is that when the first or second if is triggered you start a 700Hz or 900Hz tone, but microseconds laters you enter the third if which asks for another tone at 1100 Hz and you never got a chance to hear the 700Hz or 900Hz tone.

➜ use an else clause if you want to be exclusive

  if (average > 50 && average < 100 ) tone(PA3, 700);  
  else if (average > 100 && average < 200 ) tone(PA3, 900);
  else if (average > 20 && average < 300 ) tone(PA3, 1100);
  else noTone(PA3);

Yes, they are, I am changing signal amplitude from 0 to 1000 and only last if is working,

see my post, your if statements are working, but you don't hear them.

I'm going to check it now.

Incidentally, this is not a valid measurement average until "numReadings" measurements have been accumulated:

Thanks, else is working.

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