For my lab I am having trouble creating a software algorithm that will continuou
ID: 3865143 • Letter: F
Question
For my lab I am having trouble creating a software algorithm that will continuously detect the fundamental rate of a sine waveform from the function generator through the analog pin A1 of the Arduino UNO and then display the that rate in BPM on the lcd. For triangle and square waveforms I calculated the period by using the millis function to find the differnece between when the period started to the currrent time and from there calculated the BPM, but im finding its a lot harder for a sine waveform. So I was wondering to get help in creating the Arduino IDE software algorithm/ code to detect the rate of a sine waveform. Thanks
Explanation / Answer
Rate is calculated by setting up a timer that increments each time the interrupt executes. Code is represented below:-
prevData = newData;/ /store previous value
newData = ADCH; //get value from A0
if (prevData < 127 && newData >= 127) {//if increasing and crossing midpoint
period = timer; //get period from current timer value
timer = 0; //reset timer
}
timer++; //increment timer
Then, in main loop:-
frequency = 38462/period;//timer rate/period
//print results
Serial.print(frequency);
Serial.println(" hz");
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.