/*
#define PIN_TRIG 20
#define PIN_ECHO 21
#include <SevSeg.h>
SevSeg sevseg;
uint16_t duration = 0;
uint32_t interval = 0;
float distance = 0;
void setup()
{
uint8_t numDigits = 4;
uint8_t digitPins[] = {9, 2, 4, 7};
uint8_t segmentPins[] = {0, 4, 10, 12, 13, 1, 8, 10};
uint8_t displayType = COMMON_ANODE; // (Common Anode or Common Cathode)
bool resistorsOnSegments = false;
bool updateWithDelays = false;
bool leadingZeros = false;
bool disableDecPoint = false;
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
sevseg.begin(displayType, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(90);
}
void loop()
{
if ((millis() - interval) > 100) {
interval = millis();
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
int duration = pulseIn(PIN_ECHO,HIGH);
distance = (distance / 2) / 29;
sevseg.setNumber(distance);
}
sevseg.refreshDisplay();
}
/* SevSeg Counter Example
Copyright 2020 Dean Reading
This example demonstrates a very simple use of the SevSeg library with a 4
digit display. It displays a counter that counts up, showing deci-seconds.
*/
/*
#include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment controller object
void setup() {
byte numDigits = 4;
byte digitPins[] = {9, 2, 3, 7};
byte segmentPins[] = {0, 4, 10, 12, 13, 1, 8, 10};
bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
byte hardwareConfig = COMMON_ANODE; // See README.md for options
bool updateWithDelays = false; // Default 'false' is Recommended
bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
bool disableDecPoint = false; // Use 'true' if your decimal point doesn't exist or isn't connected
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(90);
}
void loop() {
static unsigned long timer = millis();
static int deciSeconds = 0;
if (millis() - timer >= 100) {
timer += 100;
deciSeconds++; // 100 milliSeconds is equal to 1 deciSecond
if (deciSeconds == 10000) { // Reset to 0 after counting for 1000 seconds.
deciSeconds=0;
}
sevseg.setNumber(deciSeconds, 1);
}
sevseg.refreshDisplay(); // Must run repeatedly
}
/// END ///
*/
/*
#define PIN_TRIG 20
#define PIN_ECHO 21
#include <SevSeg.h>
SevSeg sevseg;
uint16_t duration = 0;
uint32_t interval = 0;
float distance = 0;
void setup()
{
uint8_t numDigits = 4;
uint8_t digitPins[] = {9, 2, 3, 7};
uint8_t segmentPins[] = {0, 4, 10, 12, 13, 1, 8, 11};
uint8_t displayType = COMMON_ANODE; // (Common Anode or Common Cathode)
bool resistorsOnSegments = false;
bool updateWithDelays = false;
bool leadingZeros = false;
bool disableDecPoint = false;
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
sevseg.begin(displayType, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(90);
Serial.begin(115200);
}
void loop()
{
if ((millis() - interval) > 0) {
interval = millis();
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
delayMicroseconds(5);
int duration = pulseIn(PIN_ECHO,HIGH);
Serial.print("distance in CM:");
Serial.println(duration / 58);
distance = (duration / 58);
delay(1000);
sevseg.setNumber(distance);
}
sevseg.refreshDisplay();
}
*/
// HC-SR04 Ultrasonic Distance Sensor with 7 Segment Display
#define TRIG_PIN 20
#define ECHO_PIN 21
#include <SevSeg.h>
SevSeg sevseg;
uint16_t duration = 0;
uint32_t interval = 0;
float distance = 0;
void setup()
{
uint8_t numDigits = 4;
uint8_t digitPins[] = {8, 2, 3, 7};
uint8_t segmentPins[] = {0, 4, 10, 12, 13, 1, 8, 11};
uint8_t displayType = COMMON_ANODE; // (Common Anode or Common Cathode)
bool resistorsOnSegments = false;
bool updateWithDelays = false;
bool leadingZeros = false;
bool disableDecPoint = false;
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
sevseg.begin(displayType, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(90);
Serial.begin(115200);
}
void loop()
{
if ((millis() - interval) >= 100) {
interval = millis();
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(5);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read time of the trig and echo pins
duration = pulseIn(ECHO_PIN, HIGH);
Serial.print("distance in cm:");
Serial.println(duration / 58);
delay(1000);
// Calculates the distance
distance = (duration / 2) / 29;
sevseg.setNumber(distance);
}
sevseg.refreshDisplay();
}