store data given by ultrasonic sensor in an array

my project is to store the data generated by ultrasonic sensor in an array (atleast 3 different values) and then find the median of those values. I have written a code for storing the values in array but unable to find the median.
Also please check the code for array

const int trigPin = 3;
const int echoPin = 2;

long duration,distance,Sensor;

void setup() {
Serial.begin(9600);

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

}

void loop() {
distance = calculateDistance();
int i=0;
int dist = {distance};

  • for(i=0;i<3;i++)*
  • {*
    _ Serial.println(dist*);_
    _
    return 1;_
    _
    }_
    _
    }_
    int calculateDistance()
    _
    {_
    _
    digitalWrite(trigPin, LOW);_
    _
    delayMicroseconds(2);_
    _
    digitalWrite(trigPin, HIGH);_
    _
    delayMicroseconds(10);_
    _
    digitalWrite(trigPin, LOW);_
    _
    duration = pulseIn(echoPin, HIGH);_
    _ distance = (duration/2)0.034;_
    _
    }
    _
    ultra_sonic.ino (622 Bytes)

Because of the way that you posted it the forum software has turned some of your code into italics

Please follow the advice on posting code given in posting code

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

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