Last active
February 11, 2018 12:33
-
-
Save hyl392/89a6ec6ec6048540b7b143b29a2ef3ae to your computer and use it in GitHub Desktop.
Thief Prevention
This file contains 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 sensorPin = A0; | |
const int ledPin = 9; | |
int sensorValue = 0; | |
int sensorMin = 1023; | |
int sensorMax = 0; | |
int buzzer = 8; | |
void setup() { | |
pinMode(13, OUTPUT); | |
pinMode(buzzer, OUTPUT); | |
digitalWrite(13, HIGH); | |
Serial.begin(9600); | |
while (millis() < 1000) { | |
sensorValue = analogRead(sensorPin); | |
if (sensorValue > sensorMax) { | |
sensorMax = sensorValue; | |
} | |
if (sensorValue < sensorMin) { | |
sensorMin = sensorValue; | |
} | |
} | |
digitalWrite(13, LOW); | |
} | |
void loop() { | |
// read the sensor: | |
sensorValue = analogRead(sensorPin); | |
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255); | |
sensorValue = constrain(sensorValue, 0, 255); | |
int val = 255-sensorValue; | |
Serial.println(val); | |
if (val==255){ | |
tone(buzzer,1000); | |
} | |
else{ | |
noTone(buzzer); | |
} | |
analogWrite(ledPin, val); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment