Hi,
Using two piezo elements, I am trying to detect if a "knock" is closer to the first, or the second piezo element. Both are 2 meters apart from each other and attached to a table. Any time I knock with my finger on the table and use the analogRead function, I can tell both register the knock just fine.
However, it looks like the analogRead function takes a lot of time to return a value. This means I am not able to tell which of the two piezo elements registered the knock first and thus cannot tell if the knock was closer to one or the other.
I read about the AnalogReadFast library which can speed up the analogRead method a lot, but it does not seem to support my Arduino Nano Every (because it runs on mega avr architecture and not avr or samd). I also read an article about lowering the prescale factor but that throws errors on my board too.
Does anyone know what I need to get around these limitations are any other way for me to tell if the "knock" was closer to the first or the second piezo element? Maybe there is a way to process the output as a digital value somehow and use (the much faster) digitalRead instead?
const byte sensor1 = A1;
const byte sensor2 = A2;
int sensor1threshold = 0;
int sensor2threshold = 0;
unsigned long sensor1Time = 0;
unsigned long sensor2Time = 0;
unsigned long lastDetection = 0;
void setup() {
Serial.begin(9600);
Serial.println("Determining threshold");
delay(1000);
// Determine threshold for Sensor 1
for (int i=0; i < 100; i++)
{
int tempReading = analogRead(sensor1);
if (tempReading > sensor1threshold)
{
sensor1threshold = tempReading + 1;
}
delay(10);
}
Serial.print("Sensor 1 threshold: ");
Serial.println(sensor1threshold);
// Determine threshold for Sensor 2
for (int i=0; i < 100; i++)
{
int tempReading = analogRead(sensor2);
if (tempReading > sensor2threshold)
{
sensor2threshold = tempReading + 1;
}
delay(10);
}
Serial.print("Sensor 2 threshold: ");
Serial.println(sensor2threshold);
Serial.println("Start");
}
void loop() {
// Get reading from sensors
int sensor1Reading = analogRead(sensor1); // <--- Note, this line takes ~100ms to process, which is
int sensor2Reading = analogRead(sensor2);
// Save current time
unsigned long currentTime = micros();
// Detect if either sensor passed its threshold value
if (sensor1Reading > sensor1threshold)
{
sensor1Time = currentTime;
}
if (sensor2Reading > sensor2threshold)
{
sensor2Time = currentTime;
}
// Ensure to filter out echos
if (lastDetection < currentTime)
{
if (sensor1Time + 1000 > currentTime && sensor2Time + 1000 > currentTime)
{
// Pretend as if this detection happened 0.5 seconds in the future, so "echos" won't trigger another knock
lastDetection = currentTime + 500000;
// Check where the ball landed
if (sensor1Time < sensor2Time)
{
Serial.print("CLOSER TO SENSOR 1");
}
if (sensor1Time > sensor2Time)
{
Serial.print("CLOSER TO SENSOR 2");
}
if (sensor1Time == sensor2Time)
{
Serial.print("EQUAL DISTANCE ");
}
Serial.print(" - ");
Serial.print(sensor1Time);
Serial.print(" - ");
Serial.print(sensor2Time);
Serial.println();
}
}
delay(1);
}
Note: I do realize that the speed of sounds through wood is much higher than through air. Even if the analogRead function can get a value in 10ms, it might still be too slow for my intended purposes. However, I might experiment some with using rubber between the piezo elements and the table to slow down the waves, but am not yet sure how well it would be able to register the knock.