Created
January 10, 2017 17:34
-
-
Save riebschlager/5404f82637a771be6ece374c23d0eae8 to your computer and use it in GitHub Desktop.
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 = 9; // PW socket on sensor board | |
const int relayPin = 10; // | |
const int threshold = 48; // | |
const bool DEBUG = false; | |
int ticks = 0; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(sensorPin, INPUT); | |
pinMode(relayPin, OUTPUT); | |
digitalWrite(relayPin, LOW); | |
} | |
void loop() { | |
long distance = pulseIn(sensorPin, HIGH) / 147; | |
Serial.println(distance); | |
if (distance < threshold && distance != 0) { | |
ticks++; | |
if (ticks > 2) { | |
ticks = 0; | |
if (DEBUG) { | |
Serial.println("Trigger"); | |
Serial.println(distance); | |
Serial.println("--------------------------"); | |
} else { | |
digitalWrite(relayPin, HIGH); | |
Keyboard.write(49); | |
} | |
delay(10 * 1000); | |
} | |
} | |
else { | |
digitalWrite(relayPin, LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment