Devices & Components
Arduino Nano
Jumper wires (generic)
Perma-Proto Breadboard Half Size
Ultrasonic Sensor - HC-SR04 (Generic)
Software & Tools
Arduino IDE
Project description
Code
untitled
c_cpp
1#include <NewPing.h> 2 3// Pins 4const int LED = 8; 5const int VCC = 10; 6const int TRIG = 11; 7const int ECHO = 12; 8 9// Sonar Distance x 10const int MAX_DISTANCE_CM = 200; 11const int LED_DISTANCE_CM = 7; 12 13NewPing sonar (TRIG, ECHO, MAX_DISTANCE_CM); 14 15void setup() { 16 // Sonar VCC connected to leg 10, so put there HIGH. 17 pinMode (VCC, OUTPUT); 18 digitalWrite (VCC, HIGH); 19 20 Serial.begin (9600); 21 delay (500); 22} 23 24void loop() { 25 /* Comments */ 26 int dist = sonar.ping_cm(); 27 28 Serial.println ( dist ); 29 30 if (dist <= LED_DISTANCE_CM){ 31 digitalWrite (LED, HIGH); 32 } 33 else{ 34 digitalWrite (LED, LOW); 35 } 36} 37
untitled
c_cpp
1#include <NewPing.h> 2 3// Pins 4const int LED = 8; 5const 6 int VCC = 10; 7const int TRIG = 11; 8const int ECHO = 12; 9 10// Sonar Distance 11 x 12const int MAX_DISTANCE_CM = 200; 13const int LED_DISTANCE_CM = 7; 14 15NewPing 16 sonar (TRIG, ECHO, MAX_DISTANCE_CM); 17 18void setup() { 19 // Sonar VCC connected 20 to leg 10, so put there HIGH. 21 pinMode (VCC, OUTPUT); 22 digitalWrite (VCC, 23 HIGH); 24 25 Serial.begin (9600); 26 delay (500); 27} 28 29void loop() 30 { 31 /* Comments */ 32 int dist = sonar.ping_cm(); 33 34 Serial.println 35 ( dist ); 36 37 if (dist <= LED_DISTANCE_CM){ 38 digitalWrite (LED, HIGH); 39 40 } 41 else{ 42 digitalWrite (LED, LOW); 43 } 44} 45
Downloadable files
untitled
untitled

untitled
untitled

Comments
Only logged in users can leave comments