Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int vcc = 10;
- int trigger = 11;
- int echo = 12;
- int gnd = 13;
- void setup() {
- Serial.begin(9600);
- pinMode(echo, INPUT);
- pinMode(trigger, OUTPUT);
- digitalWrite(trigger, LOW);
- pinMode(vcc, OUTPUT);
- digitalWrite(vcc, HIGH);
- pinMode(gnd, OUTPUT);
- digitalWrite(gnd, LOW);
- }
- void loop() {
- long duration, inches, cm;
- digitalWrite(trigger, LOW); // Make sure trigger is LOW by default
- delayMicroseconds(10);
- digitalWrite(trigger, HIGH); // 12ms pulse on trigger
- delayMicroseconds(12);
- digitalWrite(trigger, LOW); // Back to LOW
- duration = pulseIn(echo, HIGH); // Measuring the length of the ECHO pulse
- cm=duration/58;
- inches=duration/148;
- // Print Data Serial (9600)
- if (cm > 100 || cm<3){ // Range: 3-100 cm
- Serial.println("N/A");
- }
- else{
- Serial.print(cm);
- Serial.print(" ");
- Serial.println(inches);
- }
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement