Natural frequency yields very small numbers in Hz is there something wrong with the formula?
How can I plot the output of every sensor's natural freq. or extract the data into excel?
#define NUM_SENSORS 4 // Number of piezoelectric sensors
int sensorPins[NUM_SENSORS] = {A0, A1, A2, A3}; // Analog input pins for each sensor
int knockThreshold = 5; // Threshold for knock detection
int knockDuration = 10; // Duration of knock pulse in milliseconds
int knockInterval = 5; // Interval between knock pulses in milliseconds
int knockCount = 0; // Counter for knock pulses
int p = 0;
int loopCount = 20;
float a[NUM_SENSORS][20] = {0};
unsigned long lastKnockTime = 0; // Time of last knock pulse
unsigned long lastSampleTime = 0; // Time of last sensor sample
unsigned long sampleInterval = 10; // Interval between sensor samples in milliseconds
int sensorValues[NUM_SENSORS]; // Array to store sensor values
float freq[NUM_SENSORS]; // Natural frequency for each sensor
void setup() {
Serial.begin(9600); // Initialize serial communication
}
float calculateNaturalFrequency(int sensorValue, int knockCount, int knockInterval) {
// Check for knock pulse
if (knockCount == 0 || millis() - lastKnockTime > knockInterval) {
return 0; // No knock pulse detected
}
// Check if sensor value is above threshold
if (sensorValue < knockThreshold) {
return 0; // Sensor value below threshold
}
// Calculate time between knock pulse and peak in sensor value
int peakTime = millis() - lastKnockTime;
float peakValue = sensorValue / 1024.0 * 5.0; // Convert sensor value to voltage
float naturalFrequency = (1.0 / (2.0 * PI * sqrt(abs((peakTime * peakTime) - (knockInterval * knockInterval)) * sqrt(peakValue))); // Calculate natural frequency
return naturalFrequency;
}
void printReport(){
int c[NUM_SENSORS] = {0};
float sum[NUM_SENSORS] = {0.0, 0.0, 0.0, 0.0};
float average[NUM_SENSORS] = {0.0, 0.0, 0.0, 0.0};
if (p == loopCount-1){
for (int i = 0; i < p; i++){
for (int j= 0; j < NUM_SENSORS; j++) {
if (a[j][i] != 0.00){
sum[j] += a[j][i];
c[j] += 1;
}
}
}
p = 0;
Serial.println(" count nonzero readings :---------------------------------- ");
for (int j= 0; j < NUM_SENSORS; j++) {
Serial.print("------------------Count Nonzero values of sensor ");
Serial.print(j);
Serial.print(": ");
Serial.println(c[j]);
average[j] = (float) sum[j]/ c[j];
Serial.print("------------------average Nonzero values of sensor ");
Serial.print(j);
Serial.print(": ");
Serial.println(average[j]);
}
}
}
void loop() {
// Check for knock pulse
if (millis() - lastKnockTime > knockDuration) {
lastKnockTime = millis(); // Set time of last knock pulse
knockCount++; // Increment knock pulse counter
}
// Take sensor sample at regular intervals
if (millis() - lastSampleTime > sampleInterval) {
// Read sensor values into array
for (int i = 0; i < NUM_SENSORS; i++) {
sensorValues[i] = analogRead(sensorPins[i]);
}
// Calculate natural frequency for each sensor
for (int i = 0; i < NUM_SENSORS; i++) {
freq[i] = calculateNaturalFrequency(sensorValues[i], knockCount, knockInterval);
a[i][p] = freq[i];
/*if( sensorValues[i] > 0 ){
}*/
}
// Output natural frequencies to serial port
if((freq[0]+freq[1]+freq[2]+freq[3])!=0.0){
Serial.println("ooooooooooooooooooooooooooooooooooooooooo");
for (int i = 0; i < NUM_SENSORS; i++) {
Serial.print("Sensor ");
Serial.print(i+1);
Serial.print(" Natural freq : ");
Serial.println(freq[i]);
Serial.println(" Hz");
pow(sensorValues[i],millis());
}
}
p = p + 1;
// Serial.print("Loop Number : ");
// Serial.println(p);
delay(100);
lastSampleTime = millis(); // Set time of last sensor sample
}
}
No, no. Test and open "Serial plotter" instead of Serial monitor. It's some time since I plotted..... Print the different variables using Serial.print. End the sequence with a Serial.println.