Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int trigPin = 7;
- const int echoPin = 6;
- const int buzzPin = 5;
- const int ledPin = 4;
- long duration;
- long microsecondsToCentimeters(long microseconds) {
- return microseconds / 29 / 2;
- }
- void setup() {
- Serial.begin(9600);
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- pinMode(ledPin, OUTPUT);
- }
- void loop() {
- long duration, cm;
- pinMode(trigPin, OUTPUT);
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- pinMode(echoPin, INPUT);
- duration = pulseIn(echoPin, HIGH);
- cm = microsecondsToCentimeters(duration);
- Serial.print(cm);
- Serial.print("cm");
- Serial.println();
- delay(100);
- digitalWrite(ledPin, HIGH); //here i need to do something with if and else but i dont know how
- digitalWrite(buzzPin, HIGH);
- }
Add Comment
Please, Sign In to add comment