# Airspeed in Arduino help

Hi there. I’m doing a project where I need to use 2 ultrasonic distance sensors to calculate Airspeed and trigger a buzzer whenever it falls to a lower airspeed or no air at all. I’ve been wrapping my head around the formula for the actual airspeed, but I can’t seem to translate it into arduino and make it work with 2 sensors. Any inputs as to how to do this would be helpful. Thanks.
I am attaching 2 pictures: one of them is of the sensors in tinkercad, the other one is the picture of the study I found. I am lost in the code part for this.

I found a simplified formula that can be used too, and it was this: AirSpeed=(0.3/2)((pingSensor1/1000000-pingSensor2/1000000)/(pingSensor1/1000000pingSensor2/1000000));

This is what I have so far, which is not right:

void Motor_Sensor()
{

//FROM HERE -->This moves the motor between 10 and 100 cm.
long duration;
int cm;
int revSpeed;
//cm = Ping4Pin(triggerPin, echoPin, duration);
float pingSensorA;
float pingSensorB;
float Airspeed;

serial.print(“Sensor A-B:”);
delay(1000);
serial.print(“Sensor B-A:”)

//Change L for size of tube
serial.print(“Air Flow=”);
serial.print(Airspeed);
serial.print(“m/s”);
delay(1000);

if (Airspeed > 0 && Airspeed < 100)
{ // if the object is within range (change this to suit)
revSpeed = map(Airspeed, 10, 100, 50, 255); // converts data range from sensor
// from 5 → 100 to 50 → 255
analogWrite(motorPin, revSpeed); // drive the motor at the converted speed
delay(500);
digitalWrite(10, HIGH);
digitalWrite(8, LOW);
noTone(0);
}

else
{
analogWrite(motorPin, 0); // else set the motor to zero
//digitalWrite(motorPin, LOW);
revSpeed = 0;
digitalWrite(10, LOW);
digitalWrite(8, HIGH);
tone(13, 659, 1000);
lcd.setCursor(1,0);
lcd.print(“No Airflow!”);
}

delay(10); // DELAY 10 MILISEC

Hi

codewriter123:

``````void Motor_Sensor()
``````

{

//FROM HERE -->This moves the motor between 10 and 100 cm.
long duration;
int cm;
int revSpeed;
//cm = Ping4Pin(triggerPin, echoPin, duration);
float pingSensorA;
float pingSensorB;
float Airspeed;

serial.print(“Sensor A-B:”);
delay(1000);
serial.print(“Sensor B-A:”)

//Change L for size of tube
serial.print(“Air Flow=”);
serial.print(Airspeed);
serial.print(“m/s”);
delay(1000);

if (Airspeed > 0 && Airspeed < 100)
{ // if the object is within range (change this to suit)
revSpeed = map(Airspeed, 10, 100, 50, 255); // converts data range from sensor
// from 5 → 100 to 50 → 255
analogWrite(motorPin, revSpeed); // drive the motor at the converted speed
delay(500);
digitalWrite(10, HIGH);
digitalWrite(8, LOW);
noTone(0);
}

else
{
analogWrite(motorPin, 0); // else set the motor to zero
//digitalWrite(motorPin, LOW);
revSpeed = 0;
digitalWrite(10, LOW);
digitalWrite(8, HIGH);
tone(13, 659, 1000);
lcd.setCursor(1,0);
lcd.print(“No Airflow!”);
}

delay(10); // DELAY 10 MILISEC

You need to simplify your code by separating Motor control, sensor reading, speed calculation and then tie up in a controller…

For realtime control a blocking call - delay() cannot be used…

Since motors need to accelerate and do not stop instantly some ramping up and down might be necessary…

The code is incomplete. No mention of libraries/includes, function bodies and variable names appear without being declared (e.g. readSensorA) - one has no idea what type they are and how are actualised.

Then you did not mentioned the physical details for your (desired) device:

• speed range min/max limits
• speed precision
• L - horisontal distance between sensors.

Beware that either HC-SR04 and Arduino have:

• limited timing precision.
• possible inconsistent timing measurements due to interrupts

I’m unconvinced by your math. Have you run the numbers to see if the Arduino will have enough precision to actually detect the differences in airflow?

I actually changed the whole code, but I still have problems with the formula. The formula that I need is FORMULA: (L/2cos(x))((Tup-Tdown)/(Tup*Tdown))

I did cos(0), with L being the distance of .03 (meters).

So what I did was float AirSpeed=(0.3/2)((cm/10000-cm1/10000)/(cm/10000cm1/10000));

With cm as my T up and cm1 as my Tdown. But it still gives me a negative air flow on tinkercad. I need help with the formula, please!

Thanks

Those cm/10000 calculations are done in integer. Declare cm as float, or change the number to 10000.0 for floating point math.

You can probably leave out all those divisions by 10000 and do this for the final result only. Makes the formula simpler. Continue to work in cm and only in the end convert to the final result to hm (why you even want to do that is a mystery to me considering the range of an ultrasound sensor is about 0.04-0.05 hm, but well, it's your project).

Well, each sensor is reading the distance from the ultrasonic sensor and converting it to cm.
This is why I have cm and cm1.

to calculate either cm or cm1, this is the formula cm = (0.01723 * readSensorA(6,5))