/* Touch Light based on the knock tutorial from arduino.cc: * http://www.arduino.cc/en/Tutorial/Knock * by DojoDave */ int pins[] = { 9,10,11 }; // Putting the LED pins in an array int num_pins = 3; int knockSensor = 0; // knock sensor is plugged into analog pin 0 byte val = 0; // variable to store the value read from the sensor pin int THRESHOLD = 10; // threshold value to decide when the detected sound is a *ping* or not int onLED; // variable to record which LED is on void setup() { int i; for (i = 0; i < num_pins; i++) pinMode(pins[i], OUTPUT); // set each pin as an output Serial.begin(9600); digitalWrite(pins[0], 1); // turning the first LED on onLED = 1; } void loop() { val = analogRead(knockSensor); // reading the sensor if (val >= THRESHOLD) { int i; for (i = 0; i < num_pins; i++) digitalWrite(pins[i], 0); // turning off any LED that is on int randomLED = int(random(3)); // picking a random LED if (randomLED == onLED) { // if it picks the same pin, it will choose a different one if (onLED == 0) randomLED = 1; if (onLED == 1) randomLED = 2; if (onLED == 2) randomLED = 0; } digitalWrite(pins[randomLED], 1); // turn on the random LED onLED = randomLED; Serial.println("*ping*"); } delay(100); }