// // Using a piezoelectric element as a touch sensor. // // Connect a piezo element to Port 1 pin 4 and ground. // When you tap on it, a message is printed, and the green LED is toggled. // We then use the piezo element for output, and beep. // // To make the beeps louder, place (or glue) the piezo element to the bottom of a paper // cup to act as a diaphragm and megaphone. // volatile int was_tapped = false; // // The function felt_tap() is called whenever someone taps on the piezo element // void felt_tap( void ) { was_tapped = true; } void beep( void ) { detachInterrupt( P1_4 ); pinMode( P1_4, OUTPUT ); for( int x = 0; x < 1000; x++ ) { P1OUT ^= BIT4; delay( 1 ); } pinMode( P1_4, INPUT ); attachInterrupt( P1_4, felt_tap, RISING ); } void setup( void ) { Serial.begin( 9600 ); pinMode( GREEN_LED, OUTPUT ); attachInterrupt( P1_4, felt_tap, RISING ); // // Just for fun, use the piezoelectric element to make a short beep at first... // beep(); } long unsigned int old_millis = 0; int count = 0; void loop( void ) { if( was_tapped && millis() < old_millis+100 ) { Serial.print( count++ ); Serial.println( " Someone tapped!" ); was_tapped = false; P1OUT ^= BIT6; if( P1OUT & BIT6 ) // Just for fun, beep when LED lights beep(); } old_millis = millis(); }