Created
January 20, 2019 22:34
-
-
Save blister/70953a3127ebf85aa72aed0d2044185a to your computer and use it in GitHub Desktop.
Guard Dog Tutorial for Robot Scholar
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
void blink(int blinks) { | |
for ( int i = 0; i <= blinks; i++ ) { | |
digitalWrite(led, HIGH); | |
delay(250); | |
digitalWrite(led, LOW); | |
delay(250); | |
} | |
} |
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
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); | |
} | |
} |
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
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; | |
} |
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
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); | |
} |
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
#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