Created
May 6, 2018 11:01
-
-
Save MelvinStans/4689deb569ed99de488781235015cd97 to your computer and use it in GitHub Desktop.
Piezo Button
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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