Skip to content

Instantly share code, notes, and snippets.

@hpainter
Last active August 29, 2015 14:13
Show Gist options
  • Save hpainter/6feaed8f52f2bfcd5907 to your computer and use it in GitHub Desktop.
Save hpainter/6feaed8f52f2bfcd5907 to your computer and use it in GitHub Desktop.
Steve-O
/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
Red POS to Arduino pin 11
Green POS to Arduino pin 10
560 ohm resistor to both LED NEG and GRD power rail
More info at: http://goo.gl/kJ8Gl
Original code improvements to the Ping sketch sourced from Trollmaker.com
Some code and wiring inspired by http://en.wikiversity.org/wiki/User:Dstaub/robotcar
*/
#define PIN_TRIGGER 13
#define PIN_ECHO 12
#define LED_RED 11
#define LED_YELLOW 10
#define BUZZER 8
void setup() {
// Initialize Serial comms
Serial.begin(9600);
// Pin initializations
pinMode(PIN_TRIGGER, OUTPUT);
pinMode(PIN_ECHO, INPUT);
pinMode(LED_RED, OUTPUT);
pinMode(LED_YELLOW, OUTPUT);
pinMode(BUZZER, OUTPUT);
}
// Main program loop
void loop() {
float min_distance = 0; // Shortest supported distance in cm
float max_distance = 100; // Longest supported distance in cm
float min_hertz = 100; // Lowest tone to play
float max_hertz = 500; // Highest tone to play
int tone_ms = 75; // Length of time to play tone
// Turn off both lights to start with, just to be safe:
digitalWrite(LED_RED,LOW);
digitalWrite(LED_YELLOW,LOW);
// Wait half a second before doing anything
// (Doing this first prevents having to do it multiple places)
delay(500);
// Send out a 1ms pulse through PIN_TRIGGER
digitalWrite(PIN_TRIGGER, LOW);
delayMicroseconds(1);
digitalWrite(PIN_TRIGGER, HIGH);
delayMicroseconds(1);
digitalWrite(PIN_TRIGGER, LOW);
// Measure time elapsed until return signal
float duration = pulseIn(PIN_ECHO, HIGH);
// Calculate distance in cm based on time lapse
float distance = (duration/2) / 29.1;
// Sanity check on calculated distance:
// NB: I would probably move this to a dedicated function for clarity
if (distance >= max_distance || distance <= min_distance){
Serial.println("Out of range");
// Output the generic "I'm alive but nothing is happening" light pulse on LED_RED
digitalWrite(LED_RED,HIGH);
delay(10);
digitalWrite(LED_RED,LOW);
// Bail out
return;
}
// If here, we have a valid distance measurement, so continue
// Output measurement over serial port:
Serial.print(distance);
Serial.println(" cm");
// Calculate the measurement as a percentage of supported distance range
float distance_pct = distance/(max_distance - min_distance);
// Calculate a tone hertz proportional to the distance
// (min_distance = max_hertz, max_distance = min_hertz)
float tone_hertz = max_hertz - (distance_pct * (max_hertz - min_hertz));
// Swing the LED_YELLOW on briefly
digitalWrite(LED_YELLOW,HIGH);
delay(5);
digitalWrite(LED_YELLOW,LOW);
// Play tone:
tone(BUZZER, tone_hertz, tone_ms);
return; // All done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment