Advertisement
ffpaladin

Piezo Histogram Demo

Dec 29th, 2013
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. // borrowed code, fidgeted around
  2.  
  3. const int ledPin = 7;      // led connected to digital pin 7
  4. const int knockSensor = 1 ; //A0; // the piezo is connected to analog pin 0
  5. const int threshold = 5000;  // threshold value to decide when the detected sound is a knock or not
  6.  
  7. const int smoothKick = 100 ;
  8.  
  9. // these variables will change:
  10. int sensorReading = 0;      // variable to store the value read from the sensor pin
  11. int ledState = LOW;         // variable used to store the last LED status, to toggle the light
  12.  
  13. int smooth = 0 ;
  14.  
  15. void setup() {
  16.  pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
  17.  Serial.begin(9600);       // use the serial port
  18. }
  19.  
  20. void loop() {
  21.   // read the sensor and store it in the variable sensorReading:
  22.   sensorReading = analogRead(knockSensor);    
  23.  
  24.     smooth = smooth - smooth/2 ;
  25.  
  26.   //if (sensorReading>10)
  27.   {
  28.     smooth += sensorReading ;
  29.   }
  30.  
  31.   // if the sensor reading is greater than the threshold:
  32.   //if (sensorReading >= threshold)
  33.   {
  34.     // toggle the status of the ledPin:
  35.     //ledState = !ledState;  
  36.     // update the LED pin itself:        
  37.     bool on = smooth>smoothKick ;
  38.    
  39.     digitalWrite(ledPin, on ? HIGH : LOW );
  40.    
  41.     // send the string "Knock!" back to the computer, followed by newline
  42.     //Serial.println("Knock" );
  43.     //Serial.println(sensorReading);
  44.     //Serial.println(smooth);
  45.    
  46.     if (smooth>100)
  47.     {
  48.       for( int i=0; i<smooth/10; ++i )
  49.       {
  50.         Serial.print("*");
  51.       }
  52.       Serial.println( on ? "+" : ".");
  53.     }
  54.   }
  55.   //delay(100);  // delay to avoid overloading the serial port buffer
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement