Vibration and Sound measurement through LCD

Thread Starter

Dualfire

Joined Dec 5, 2012
10
I have been working on my project, where I am using a piezo vibration sensor and an electret microphone connected to an LCD through an Arduino microprocessor.

I need to tweak it a little and hope that you guys could help me.

I was told that for the sound portion I need to have a sampling rate of at least 40kHz (assuming I want to measure up to 20kHz, and I learned from my comm class something like fs = 2f). How do I apply it to the Arduino? Through some research I found that I need to set the prescaler to 16? How do I apply that to my code?

When running the code below with only vib() inside the void loop(), it outputs good values . But when running this code like below, I get an erroneous output for the vibration portion, because it outputs the values over time, (instead of outputting it every time I touch the vib sensor). It summarizes the values somehow and gives me a strange output that seems to be almost completely random.
I do want the output to be timed, instead of having to touch it every time, but how can I fix the output? Can you guys help me please?

Thank you so much!



Rich (BB code):

#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

const int ledPin = 13;      // led connected to digital pin 13
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 5;   // threshold value to decide when the detected sound is a knock or not

int sensorReading = 0;      // variable to store the value read from the sensor pin
int ledState = LOW;         // variable used to store the last LED status, to toggle the light
int sum = 0;
int values = 10;
int i = 0;

const int sampleWindow = 50;              // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;



void setup() {
  
  lcd.begin(16, 2);
  pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
  Serial.begin(9600);       // use the serial port
}

void vib()
{
  // read the sensor and store it in the variable sensorReading:
  sensorReading = analogRead(knockSensor);    

  // if the sensor reading is greater than the threshold:
  if (sensorReading >= threshold) {
    // toggle the status of the ledPin:
    ledState = !ledState;  
    // update the LED pin itself:        
    digitalWrite(ledPin, ledState);


    if (i >= values)
    {
    i = 0; 
    sum = sum/values;
    //Serial.println(sum);
    lcd.setCursor(0, 1);
    lcd.print(sum);
    sum = 0;
    }
    else
    { 
    sum += sensorReading;
    i++;
    }
  }
  delay(10);  
}



void Micro()
{
unsigned long startMillis= millis();      // Start of sample window
unsigned int peakToPeak = 0;              // peak-to-peak level
 
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
 
// collect data for 50 mS
while (millis() - startMillis < sampleWindow)
{
sample = analogRead(1);
//Serial.println(sample);
  if (sample < 1024)                        // toss out spurious readings
    {
    if (sample > signalMax)
    {
    signalMax = sample;                       // save just the max levels
    }
  else if (sample < signalMin)
    {
    signalMin = sample;                       // save just the min levels
    }
  }
}
peakToPeak = signalMax - signalMin;       // max - min = peak-peak amplitude

double volts = (peakToPeak * 3.3) / 1024; // convert to volts

delay(100); 
//Serial.println(volts);
lcd.setCursor(0, 0);
lcd.print(volts);
}



void loop() {
  Micro();
  vib();  
}
 
Top