Vyžaduje externí napájení 5V – jinak čidlo nejelo a displej slabě svítí.
co 1 vteřina vyčítá vzdálenost a zobrazuje na displeji.
#include "SevenSegmentTM1637.h" // defines pins numbers const int trigPin = 2; //D4 const int echoPin = 0; //D3 const byte PIN_CLK = D5; // define CLK pin (any digital pin) const byte PIN_DIO = D6; // define DIO pin (any digital pin) SevenSegmentTM1637 display(PIN_CLK, PIN_DIO); // defines variables long duration; int distance; void setup() { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.begin(9600); // Starts the serial communication Serial.print("Meric-vzdalenosti-HC-SR04 "); display.begin(); // initializes the display display.setBacklight(100); // set the brightness to 100 % display.print("INIT"); // display INIT on the display delay(200); // wait 1000 ms } void loop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance= duration*0.034/2; // Prints the distance on the Serial Monitor Serial.print("Distance: "); Serial.println(distance); display.clear(); // clear the display display.print(distance); // print SUCC for success delay(1000); }