Posts

Arduino installation

#define TRIG_PIN 7 #define ECHO_PIN 8 void setup() {   Serial.begin(9600);   pinMode(TRIG_PIN, OUTPUT);   pinMode(ECHO_PIN, INPUT); } void loop() {   // Clear the trigger   digitalWrite(TRIG_PIN, LOW);   delayMicroseconds(2);   // Send a 10 microsecond pulse to trigger   digitalWrite(TRIG_PIN, HIGH);   delayMicroseconds(10);   digitalWrite(TRIG_PIN, LOW);   // Read the echo pin: pulseIn returns duration in microseconds   long duration = pulseIn(ECHO_PIN, HIGH);   // Calculate the distance (duration / 2 because it goes to the object and comes back)   float distance_cm = duration * 0.0343 / 2;   Serial.print("Distance: ");   Serial.print(distance_cm);   Serial.println(" cm");   delay(500);  // Delay between readings }