Can someone help me with this code for a school project?

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...

led_display1.begin(112);

I am not familiar with the library that you are using. What is the significance of the 112 parameter used in this function call ?

Which Arduino board are you using? When testing, are you getting the correct HC-SR04 values before testing the display?
What is pin 0 being used for?

I didn't yet test it on my board, I used Tinkercad

That's how the 7 Seg display is set up in Tinkercad I think

You should have told everyone that you are simulating stuff.

1 Like

sorry

When digitalRead(0) is 1, you are only take one distance measurement,
but when it is 0, you take two and subtract one from the other.

Shouldn't you take two readings in each case?

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 : (

result = d2 - d1;

It will always output 0 if I do it like this

void loop()
{
  cm = 0.01723 * readUltrasonicDistance(9, 8);

  d1 = cm;
  delay(1000); // Wait for 1000 millisecond(s)
  d2 = cm;
  Vms = (d2 - d1);
  Serial.println(Vms);

sorry, I'm pretty new to this...

No surprise there because both d1 and d2 are both equal to cm. Before doing the subtraction you need to do

cm = 0.01723 * readUltrasonicDistance(9, 8);
d2 = cm;

or you could just do

 d1 = 0.01723 * readUltrasonicDistance(9, 8);
 delay(1000); // Wait for 1000 millisecond(s)
 d2 = 0.01723 * readUltrasonicDistance(9, 8);
1 Like

Looking at the Adafruit LED Backpack Tutorial, they say that you also need to download and install the Adafruit GFX library.

They also use led_display1.print() rather than led_display1.println().

thanks!

Thank you so much!

Do you understand why it didn't work the way that you wrote it ?

Just out of curiousity, how to you make the sensor "move" in a simulator?

yeap, thanks !

you make the object in front move

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.