a question about notefreq1

I am trying to get a pin high on pin 20 when the program “hears” a frequency of 3000 Hz. The program is putting the readings out to the monitor as expected. The problem that I’m having is: that my “if” statement isn’t sending the pin high. Maybe I misunderstand the output from notefreq1.read() but it seems pretty straight forward.

What am I doing wrong?

#include <Audio.h>
#include <Wire.h>
#include <SerialFlash.h>

int LedPin = 20; // LED Pin
int ListenFrequency = 3000; // Threshold frequency


// GUItool: begin automatically generated code
AudioInputI2S            i2s1;           //xy=352,135
AudioAnalyzeNoteFrequency notefreq1;      //xy=600,228
AudioOutputI2S           i2s2;           //xy=626,144
AudioConnection          patchCord1(i2s1, 0, notefreq1, 0);
AudioConnection          patchCord2(i2s1, 0, i2s2, 0);
AudioConnection          patchCord3(i2s1, 0, i2s2, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=525,305
// GUItool: end automatically generated code

//---------------------------------------------------------------------------------------
void setup() {
  pinMode (LedPin, OUTPUT);
  digitalWrite (LedPin, LOW);
   
  AudioMemory(30);
  Serial.begin(9600);
   
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.6);
  sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
  sgtl5000_1.micGain(50);
  delay(1000);
    /*
     *  Initialize the yin algorithm's absolute
     *  threshold, this is good number.
     */
    notefreq1.begin(.15);

    if (notefreq1.read() >= ListenFrequency){
    digitalWrite (LedPin, HIGH); 
    }
    while (notefreq1.read()< ListenFrequency) {
      digitalWrite (LedPin, LOW);
    }
}

void loop() {
    // read back fundamental frequency
    if (notefreq1.available()) {
       float note = notefreq1.read();
       float prob = notefreq1.probability();
       int cpuusage = AudioProcessorUsageMax();
        Serial.printf("CPUusage: %i | Note: %3.2f | Probability: %.2f\n", cpuusage, note, prob);
    }

}
}

Thanks for any input

Sorry about ASS-YOU-ME-ING that everything else is good within the sketch

What am I doing wrong?

Posting only a snippet?

Assuming that there's always something to read from your unspecified object?

Your if statement isn't necessarily acting on the same data that's being printed to the Serial Monitor since it uses a second read.

I've tried using the "if" against the "float" output and the raw notefreq1.read(). The output to the serial monitor is not needed and I'm only using it to verify that I am getting readings (which I am). I've been away from this for over 20 years, so I'm sure that I'm not seeing an obvious flaw.

Thanks again

Oh, and the complete code is now posted in the OP

That code is obviously for a Teensy 3.x with audio board so you would be better off asking this question in the Teensy Forums - in this case the Audio Projects forum.

Pete

vagrant1963:
The problem that I’m having is: that my “if” statement isn’t sending the pin high. Maybe I misunderstand the output from notefreq1.read() but it seems pretty straight forward.

What am I doing wrong?

You only set the LED pin in setup(). You don’t set it in loop() so it doesn’t matter how often you read the frequency.

Thanks all,
Project is complete (and it actually works)

Thanks again