Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Basic Ultrasonic
- * by Tyson Moll
- *
- * References:
- * Ultrasonic Tutorial by Rui Santos, https://randomnerdtutorials.com
- *
- */
- #include <Wire.h>
- #include <ArduinoJson.h>
- // Ultrasonic
- int trigPin = 5; // Ultrasonic Trigger
- int echoPin = 6; // Ultrasonic Echo
- long duration, cm; // Units for Ultrasonic
- unsigned long lastSend, sonicCount;
- int sendRate = 50;
- int sonicRate = sendRate;
- // Thermochromic Pins
- int ledPin = 7;
- int chromicPin = 4;
- int dist = 10; // Distance to light at
- void setup() {
- Serial.begin(9600);
- pinMode(trigPin, OUTPUT); // Ultrasonic
- pinMode(echoPin, INPUT);
- pinMode(ledPin, OUTPUT); // Thermochromics
- pinMode(chromicPin, OUTPUT);
- }
- void loop() {
- // Ultrasonic
- if (millis() - sonicCount >= sonicRate) {
- digitalWrite(trigPin, LOW); // LOW clearing ensures clean HIGH pulse
- delayMicroseconds(5); // 0.000005 seconds
- digitalWrite(trigPin, HIGH); // Triggers ultrasonic signal
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- // Read the signal from the sensor: a HIGH pulse whose
- // duration is the time (in microseconds) from the sending
- // of the ping to the reception of its echo off of an object.
- pinMode(echoPin, INPUT);
- duration = pulseIn(echoPin, HIGH);
- // Convert the time into a distance
- cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
- Serial.print(cm); // Report unit distance
- Serial.print("cm");
- Serial.println();
- }
- if (cm > dist) {
- digitalWrite(ledPin, HIGH);
- digitalWrite(chromicPin, HIGH);
- } else {
- digitalWrite(ledPin, LOW);
- digitalWrite(chromicPin, LOW);
- }
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement