Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // borrowed code, fidgeted around
- const int ledPin = 7; // led connected to digital pin 7
- const int knockSensor = 1 ; //A0; // the piezo is connected to analog pin 0
- const int threshold = 5000; // threshold value to decide when the detected sound is a knock or not
- const int smoothKick = 100 ;
- // these variables will change:
- 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 smooth = 0 ;
- void setup() {
- pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
- Serial.begin(9600); // use the serial port
- }
- void loop() {
- // read the sensor and store it in the variable sensorReading:
- sensorReading = analogRead(knockSensor);
- smooth = smooth - smooth/2 ;
- //if (sensorReading>10)
- {
- smooth += sensorReading ;
- }
- // 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:
- bool on = smooth>smoothKick ;
- digitalWrite(ledPin, on ? HIGH : LOW );
- // send the string "Knock!" back to the computer, followed by newline
- //Serial.println("Knock" );
- //Serial.println(sensorReading);
- //Serial.println(smooth);
- if (smooth>100)
- {
- for( int i=0; i<smooth/10; ++i )
- {
- Serial.print("*");
- }
- Serial.println( on ? "+" : ".");
- }
- }
- //delay(100); // delay to avoid overloading the serial port buffer
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement