I want to measure the speed using Δd/1s d being the HC-SR04 distance output in cm multiplied by 100 to get m/s , I tried testing it but the 7 seg. display wont show anything no matter how much I may move an object in front of the sensor. I haven't yet tested it, only simulated it.
#include "Adafruit_LEDBackpack.h"
int seconds = 0;
int d1 = 0;
int d2 = 0;
int Vms = 0;
int Vkm = 0;
Adafruit_7segment led_display1 = Adafruit_7segment();
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
void setup()
{
led_display1.begin(112);
pinMode(0, INPUT);
}
void loop()
{
if (digitalRead(0) > 0) {
Vkm = (0.01723 * readUltrasonicDistance(9, 8) * 3.6);
led_display1.println(Vkm);
led_display1.writeDisplay();
} else {
d1 = (0.01723 * readUltrasonicDistance(9, 8) * 100);
delay(1000); // Wait for 1000 millisecond(s)
d2 = (0.01723 * readUltrasonicDistance(9, 8) * 100);
Vms = (d2 - d1);
led_display1.println(Vms);
led_display1.writeDisplay();
}
}
I believe I made some mistakes in my code, unfortunately I have nobody to correct me / teach me : ( I would need a better way for the 7 seg display to output Δd...
yes, I'm trying to figure out how to subtract d1 from d2 , d1 being the initial measurement and d2 being the measurement taken after one second. And I have no idea how I could express that in code : (