A question about notefreq()

Status
Not open for further replies.

vagrant

Member
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 not seeing?


Code:
#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
 
To use as OUTPUT this line should not be commented out:
void setup() {
// pinMode (LedPin, OUTPUT);

Also - there is no loop() code doing a write to that pin? Only in setup() one time?
 
Thanks, I actually caught that. To many revisions floating around. Project is complete (and it actually works)

Thanks again
 
Status
Not open for further replies.
Back
Top