Skip to content

Instantly share code, notes, and snippets.

@blister
Created January 20, 2019 22: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 blister/70953a3127ebf85aa72aed0d2044185a to your computer and use it in GitHub Desktop.
Save blister/70953a3127ebf85aa72aed0d2044185a to your computer and use it in GitHub Desktop.
Guard Dog Tutorial for Robot Scholar
void loop() {
delay(50);
// See if an object is in front of us
int distance = msToCm(ping());
//Serial.println(distance);
if ( distance < detectionDistance ) {
// get a random song
playNote(NOTE_G5, 100, .1);
}
}
long ping() {
long duration;
// clear ping pin
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
duration = pulseIn(echoPin, HIGH);
return duration;
}
long msToCm(long microseconds) {
return microseconds * .034 / 2;
}
void playNote(int note, int duration, float rest) {
digitalWrite(led, HIGH);
tone(buzzer, note, duration);
delay((duration * rest)/2);
digitalWrite(led, LOW);
delay((duration * rest)/2);
}
#include "Pitches.h"
/************************/
/** PIN CONFIGURATIONS **/
/************************/
#define led 13
#define buzzer 3
#define echoPin A0
#define pingPin 10
// roughly 2.6 feet
#define detectionDistance 80
void setup() {
pinMode(led, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(pingPin, OUTPUT);
pinMode(echoPin, INPUT);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment