/*
HC-SR04 Ultrasonic Sensor Example.
Turn the LED on when an object is within 100cm range.
Copyright (C) 2021, Uri Shaked
*/
#include <TM1637Display.h>
#define ECHO_PIN 2
#define TRIG_PIN 3
const int CLK = 13; //Set the CLK pin connection to the display
const int DIO = 12; //Set the DIO pin connection to the display
TM1637Display display(CLK, DIO); //set up the 4-Digit Display.
void setup() {
Serial.begin(115200);
display.setBrightness(0x0a);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
float readDistanceCM() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2;
}
void loop() {
float distance = readDistanceCM();
display.showNumberDec(readDistanceCM());
delay(100);
// delay(100);
}
// void setup()
// {
// //set the diplay to maximum brightness
// // Set brightness (0-7):
// }
// void loop()
// {
// for(numCounter = 0; numCounter < 1000; numCounter++) //Iterate numCounter
// {
// //Display the numCounter value;
// delay(100);
// }
// }