Vibration and Sound measurement through LCD

Discussion in 'The Projects Forum' started by Dualfire, Apr 4, 2013.

  1. Dualfire

    Thread Starter New Member

    Dec 5, 2012
    10
    0
    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!



    Code ( (Unknown Language)):
    1.  
    2. [SIZE=1]
    3. #include <LiquidCrystal.h>
    4.  
    5. LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
    6.  
    7. const int ledPin = 13;      // led connected to digital pin 13
    8. const int knockSensor = A0; // the piezo is connected to analog pin 0
    9. const int threshold = 5;   // threshold value to decide when the detected sound is a knock or not
    10.  
    11. int sensorReading = 0;      // variable to store the value read from the sensor pin
    12. int ledState = LOW;         // variable used to store the last LED status, to toggle the light
    13. int sum = 0;
    14. int values = 10;
    15. int i = 0;
    16.  
    17. const int sampleWindow = 50;              // Sample window width in mS (50 mS = 20Hz)
    18. unsigned int sample;
    19.  
    20.  
    21.  
    22. void setup() {
    23.  
    24.   lcd.begin(16, 2);
    25.   pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
    26.   Serial.begin(9600);       // use the serial port
    27. }
    28.  
    29. void vib()
    30. {
    31.   // read the sensor and store it in the variable sensorReading:
    32.   sensorReading = analogRead(knockSensor);    
    33.  
    34.   // if the sensor reading is greater than the threshold:
    35.   if (sensorReading >= threshold) {
    36.     // toggle the status of the ledPin:
    37.     ledState = !ledState;  
    38.     // update the LED pin itself:        
    39.     digitalWrite(ledPin, ledState);
    40.  
    41.  
    42.     if (i >= values)
    43.     {
    44.     i = 0;
    45.     sum = sum/values;
    46.     //Serial.println(sum);
    47.     lcd.setCursor(0, 1);
    48.     lcd.print(sum);
    49.     sum = 0;
    50.     }
    51.     else
    52.     {
    53.     sum += sensorReading;
    54.     i++;
    55.     }
    56.   }
    57.   delay(10);  
    58. }
    59.  
    60.  
    61.  
    62. void Micro()
    63. {
    64. unsigned long startMillis= millis();      // Start of sample window
    65. unsigned int peakToPeak = 0;              // peak-to-peak level
    66.  
    67. unsigned int signalMax = 0;
    68. unsigned int signalMin = 1024;
    69.  
    70. // collect data for 50 mS
    71. while (millis() - startMillis < sampleWindow)
    72. {
    73. sample = analogRead(1);
    74. //Serial.println(sample);
    75.   if (sample < 1024)                        // toss out spurious readings
    76.     {
    77.     if (sample > signalMax)
    78.     {
    79.     signalMax = sample;                       // save just the max levels
    80.     }
    81.   else if (sample < signalMin)
    82.     {
    83.     signalMin = sample;                       // save just the min levels
    84.     }
    85.   }
    86. }
    87. peakToPeak = signalMax - signalMin;       // max - min = peak-peak amplitude
    88.  
    89. double volts = (peakToPeak * 3.3) / 1024; // convert to volts
    90.  
    91. delay(100);
    92. //Serial.println(volts);
    93. lcd.setCursor(0, 0);
    94. lcd.print(volts);
    95. }
    96.  
    97.  
    98.  
    99. void loop() {
    100.   Micro();
    101.   vib();  
    102. }[/SIZE]
    103.  
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    I've look a bit at this... sorry I didn't see anything obvious.
     
Loading...