Skip to content

Instantly share code, notes, and snippets.

@riebschlager
Created January 10, 2017 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riebschlager/5404f82637a771be6ece374c23d0eae8 to your computer and use it in GitHub Desktop.
Save riebschlager/5404f82637a771be6ece374c23d0eae8 to your computer and use it in GitHub Desktop.
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