#include <TimerOne.h>
#define echoPin 0 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 1 // attach pin D3 Arduino to pin Trig of HC-SR04
long duration; // Variable to store time taken to the pulse to reach receiver
int distance; // Variable to store distance calculated using formula
// 7-segment display pins
int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;
int p = 9;
int d4 = 10;
int d3 = 11;
int d2 = 12;
int d1 = 13;
long n = 0; // n represents the value displayed on the LED display (e.g., 0000)
int del = 2; // Set delay to control display flicker
unsigned long previousMillis = 0; // Store last update time for display
const long interval = 5; // Interval to update the display (in ms)
volatile long newDistance = 0; // This will hold the latest measured distance
volatile bool distanceUpdated = false; // Flag to indicate that distance is updated
void setup()
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
// Set all the pins of the LED display as output
pinMode(d1, OUTPUT);
pinMode(d2, OUTPUT);
pinMode(d3, OUTPUT);
pinMode(d4, OUTPUT);
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
pinMode(p, OUTPUT);
Timer1.initialize(100000); // Set a timer of length 100000 microseconds (0.1 sec)
Timer1.attachInterrupt(add); // Attach the interrupt service routine
}
void loop()
{
unsigned long currentMillis = millis();
// Update display at a non-blocking interval
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
clearLEDs(); // Clear the display
pickDigit(0); // Light up 7-segment display d1
pickNumber((n / 1000)); // Get the value of thousand
delay(del); // Small delay to avoid flicker
clearLEDs(); // Clear the display
pickDigit(1); // Light up 7-segment display d2
pickNumber((n % 1000) / 100); // Get the value of hundred
delay(del); // Small delay
clearLEDs(); // Clear the display
pickDigit(2); // Light up 7-segment display d3
pickNumber(n % 100 / 10); // Get the value of ten
delay(del); // Small delay
clearLEDs(); // Clear the display
pickDigit(3); // Light up 7-segment display d4
pickNumber(n % 10); // Get the value of single digit
delay(del); // Small delay
}
// Only update 'n' with the latest distance if it was updated in the interrupt
if (distanceUpdated) {
n = newDistance;
distanceUpdated = false; // Reset the flag
}
}
// Light up a specific 7-segment display digit
void pickDigit(int x)
{
digitalWrite(d1, LOW);
digitalWrite(d2, LOW);
digitalWrite(d3, LOW);
digitalWrite(d4, LOW);
switch (x)
{
case 0: digitalWrite(d1, HIGH); break;
case 1: digitalWrite(d2, HIGH); break;
case 2: digitalWrite(d3, HIGH); break;
case 3: digitalWrite(d4, HIGH); break;
}
}
// Display numbers 0-9 on the 7-segment display
void pickNumber(int x)
{
switch (x)
{
case 0: zero(); break;
case 1: one(); break;
case 2: two(); break;
case 3: three(); break;
case 4: four(); break;
case 5: five(); break;
case 6: six(); break;
case 7: seven(); break;
case 8: eight(); break;
case 9: nine(); break;
}
}
// Clear the 7-segment display
void clearLEDs()
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
digitalWrite(p, HIGH);
}
// Define each number from 0 to 9 for 7-segment display
void zero() { digitalWrite(a, LOW); digitalWrite(b, LOW);
digitalWrite(c, LOW); digitalWrite(d, LOW); digitalWrite(e, LOW);
digitalWrite(f, LOW); digitalWrite(g, HIGH); }
void one() { digitalWrite(a, HIGH); digitalWrite(b, LOW);
digitalWrite(c, LOW); digitalWrite(d, HIGH); digitalWrite(e, HIGH);
digitalWrite(f, HIGH); digitalWrite(g, HIGH); }
void two() { digitalWrite(a, LOW); digitalWrite(b, LOW);
digitalWrite(c, HIGH); digitalWrite(d, LOW); digitalWrite(e, LOW);
digitalWrite(f, HIGH); digitalWrite(g, LOW); }
void three() { digitalWrite(a, LOW); digitalWrite(b, LOW);
digitalWrite(c, LOW); digitalWrite(d, LOW); digitalWrite(e, HIGH);
digitalWrite(f, HIGH); digitalWrite(g, LOW); }
void four() { digitalWrite(a, HIGH); digitalWrite(b, LOW);
digitalWrite(c, LOW); digitalWrite(d, HIGH); digitalWrite(e, HIGH);
digitalWrite(f, LOW); digitalWrite(g, LOW); }
void five() { digitalWrite(a, LOW); digitalWrite(b, HIGH);
digitalWrite(c, LOW); digitalWrite(d, LOW); digitalWrite(e, HIGH);
digitalWrite(f, LOW); digitalWrite(g, LOW); }
void six() { digitalWrite(a, LOW); digitalWrite(b, HIGH);
digitalWrite(c, LOW); digitalWrite(d, LOW); digitalWrite(e, LOW);
digitalWrite(f, LOW); digitalWrite(g, LOW); }
void seven() { digitalWrite(a, LOW); digitalWrite(b, LOW);
digitalWrite(c, LOW); digitalWrite(d, HIGH); digitalWrite(e, HIGH);
digitalWrite(f, HIGH); digitalWrite(g, HIGH); }
void eight() { digitalWrite(a, LOW); digitalWrite(b, LOW);
digitalWrite(c, LOW); digitalWrite(d, LOW); digitalWrite(e, LOW);
digitalWrite(f, LOW); digitalWrite(g, LOW); }
void nine() { digitalWrite(a, LOW); digitalWrite(b, LOW);
digitalWrite(c, LOW); digitalWrite(d, LOW); digitalWrite(e, HIGH);
digitalWrite(f, LOW); digitalWrite(g, LOW); }
// Interrupt function to read the distance
void add()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2); // Ensure the trigPin has been low for at least 2 µs
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Trigger pulse width of 10 µs
digitalWrite(trigPin, LOW);
// Measure the duration of the pulse received
duration = pulseIn(echoPin, HIGH);
newDistance = duration * 0.0344 / 2; // Calculate the distance in cm
distanceUpdated = true; // Set the flag to indicate that new data is available
}
Comments
Post a Comment