Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Ping))) Sensor
- This sketch reads a PING))) ultrasonic rangefinder and returns the
- distance to the closest object in range. To do this, it sends a pulse
- to the sensor to initiate a reading, then listens for a pulse
- to return. The length of the returning pulse is proportional to
- the distance of the object from the sensor.
- */
- #include <Wire.h>
- #include "Adafruit_LEDBackpack.h"
- #include "Adafruit_GFX.h"
- Adafruit_7segment matrix = Adafruit_7segment();
- int triggerPin = 5;
- int echoPin = 6;
- long duration = 0;
- float cm = 0.0;
- void setup() {
- Serial.begin(9600);
- matrix.begin(0x70);
- pinMode(triggerPin, OUTPUT);
- pinMode(echoPin, INPUT);
- }
- void loop(){
- //Inputs:
- digitalWrite(triggerPin,LOW);
- delayMicroseconds(5);
- digitalWrite(triggerPin,HIGH);
- delayMicroseconds(5);
- digitalWrite(triggerPin,LOW);
- delayMicroseconds(5);
- duration=pulseIn(echoPin, HIGH);
- //if(cm >= 20){
- //Serial.println("youre a little too close");
- //}
- //else{
- //Serial.println("I like nice people and programs. Do you?");
- //}
- // Calculations: convert the time into a distance
- cm = duration /29.0 /2.0;
- //Outputs
- Serial.print(cm);
- Serial.print("cm");
- Serial.println();
- matrix.print(cm);
- matrix.writeDisplay();
- delay(400);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement