Skip to content

Instantly share code, notes, and snippets.

@giljr
Created September 1, 2019 13:57
Show Gist options
  • Save giljr/ec9e1c961b330a2e33355c6078dda013 to your computer and use it in GitHub Desktop.
Save giljr/ec9e1c961b330a2e33355c6078dda013 to your computer and use it in GitHub Desktop.
/* Project: LEGO Episode # 29
* Bridging All Sensors Together — Pitbot
* Collecting All Codes for the Final Act of Giving Behaviors to Robot
*
* INO file: _29_HC-SR04_SimpleTest_01.ino
*
* date: 9/01/19
*
* code by: https://forum.arduino.cc
* hardware by: Arduino IDE 1.8.9
*
* Description: There is no easy way more simple than that sketch to test right away HC-SR04!
* Please oepn Serial Monitor at 9600 bps:)
*
* Visit: https://medium.com/jungletronics
*
* Tutorial: https://medium.com/kidstronics/bridging-all-sensors-together-pitbot-ca8803bf9cb
* License: CC-SA 3.0, feel free to use this code however you'd like.
* Please improve upon it! Let me know how you've made it better.
*/
/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
*/
int echoPin = 13;
int trigPin = 12;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
int duration, cm;
digitalWrite(trigPin, LOW);
delayMicroseconds(20);
digitalWrite(trigPin, HIGH);
delayMicroseconds(20);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
cm = duration / 29 / 2;
/* Sound velocity=29cm/microsec,
then divide by 2 because of the return time
*/
Serial.print(cm);
Serial.println(" cm");
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment