Skip to content

Instantly share code, notes, and snippets.

@MelvinStans
Created May 6, 2018 11:01
Show Gist options
  • Save MelvinStans/4689deb569ed99de488781235015cd97 to your computer and use it in GitHub Desktop.
Save MelvinStans/4689deb569ed99de488781235015cd97 to your computer and use it in GitHub Desktop.
Piezo Button
const int piezoPin = A0;
const int threshold = 600;
int sensorReading = 0;
int outputPin = 2;
void setup() {
pinMode(piezoPin, INPUT);
pinMode(outputPin, INPUT);
Serial.begin(9600);
}
int buzzDelay = 2000;
int buzzLength = 30;
void loop() {
pinMode(outputPin, INPUT);
sensorReading = analogRead(piezoPin);
Serial.println(sensorReading);
if (sensorReading >= threshold){
Serial.println("Pressed!");
pinMode(outputPin, OUTPUT);
for(int b = 0; b < buzzLength; b++ ){
digitalWrite(outputPin, HIGH);
delayMicroseconds(buzzDelay);
digitalWrite(outputPin, LOW);
delayMicroseconds(500);
}
pinMode(outputPin, INPUT);
}
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment