This is outputting a large number that is the same for different 'tone' intervals.
I don't have a clue what I'm doing wrong. I'm a rank beginner at 'C'.
I don't have a clue what I'm doing wrong. I'm a rank beginner at 'C'.
Code:
const int AudioThreshold = 400; // compare level for audio signal
const int analogPin = 0; // Analog input pin for audio morse code
unsigned long startTime = 0; // analog pin goes high, start timer
unsigned long currentTime = 0; // while analog pin is high keep counting milliseconds
unsigned long dotTime = 0; // analog pin is now low so compute on time by subtracting start time from stop time
unsigned long wpm = 0; // convert dot time to words per minute
void setup()
{
Serial.begin(9600);
Serial.println("Morse event timer");
}
void loop()
{
while (analogRead(analogPin) < AudioThreshold) // wait for tone
{}
startTime = millis();
while (analogRead(analogPin) > AudioThreshold) // while tone is heard do nothing
{}
currentTime = millis(); // record time that tone stops
dotTime = currentTime - startTime; // in milliseconds how long the tone sounded
wpm = 1200 / dotTime; // convert and report in wpm
Serial.println(wpm);
delay(1000); // for now, to limit garbage on screen
}