// these constants won't change: const int redPin = 9; // Red LED, connected to digital pin 9 const int grnPin = 10; // Green LED, connected to digital pin 10 const int bluPin = 11; // Blue LED, connected to digital pin 1 const int knockSensor = A0; // the piezo is connected to analog pin 0 const int threshold = 100; // threshold value to decide when the detected sound is a knock or not // 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 // Set up the LED outputs void setup() { pinMode(redPin, OUTPUT); // sets the pins as output pinMode(grnPin, OUTPUT); pinMode(bluPin, OUTPUT); Serial.begin(9600); // ...set up the serial ouput } void loop() { // 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(redPin, ledState); digitalWrite(grnPin, ledState); digitalWrite(bluPin, ledState); delay(100); // delay to avoid overloading the serial port buffer } }