Distance sensor with an Arduino program is a problem that keeps me up at night.

Hello gentlemen,

I am a french student and I would like to program a HC-SR04 sensor to make it calculate a distance.
My program is devided in several parts

-Calculate the distance

-(While) 50<distance<120
Create a table of 5 values and make an average

-(If) fifth value<average<first value
make the red LED wink
(else) make the red light normally

Here is the program: (moyenne = average ; valeur = value)

#define trigPin 13 
#define echoPin 12 
#define led 11 
#define led2 10 
void setup() 
{
 Serial.begin (9600); 
 pinMode(trigPin, OUTPUT);  
 pinMode(echoPin, INPUT); 
 pinMode(led, OUTPUT);  
 pinMode(led2, OUTPUT);  
}
void loop() {
 long duration, distance;  
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2);  
 digitalWrite(trigPin, HIGH);  

 delayMicroseconds(10);  
 digitalWrite(trigPin, LOW); 
 duration = pulseIn(echoPin, HIGH);  
 distance = (duration/2) / 29.4; 
 if (distance < X) 
{  
 digitalWrite(led,HIGH);  
 digitalWrite(led2,LOW); 
} 

else { // Sinon:
   digitalWrite(led,LOW); 
   digitalWrite(led2,HIGH); 

while (50<distance<120)
{
float moyenneNote(float tableau[], int nombreValeur)
{
   int i = 0;
   double total = 0; 
   float moyenne = 0; 
   for(i = 0; i < 10; i++)
   {
       total = total + tableau[i];
   }
   moyenne = total / 10;
   return moyenne;
   int valeur0 = tableau[0];
   int valeur10 = tableau[10];
}
}
if (tableau[10]< moyenne < tableau[0])
{
{
 digitalWrite(led, HIGH);   
 delay(500);             
 digitalWrite(led, LOW); 
 delay(500);             
 digitalWrite(led2,LOW); 
}

else
{
 digitalWrite(led,HIGH); 
 digitalWrite(led2,LOW);
}

There are many error messages when I try to make it work and I am not qualified enough to find my mistakes. I request the help of this community to help me.

Thanks in advance,

Vladnirki

Please read the “How to Use This Forum” thread for hint on how to post code to make it more readable using code tags.

This line is wrong. While it is fine to write this in human terms, a computer just can’t understand:

while (50<distance<120)

It should be two separate conditions.

while (50<distance && distance<120)

The next line defines a function. But a function cannot be defined within a function. I’d suggest some reading on C++ functions. You need to define the function outside of the loop and call it from within.

Hi,

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html
then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Also what model Arduino are you using?

Thanks.. Tom... :slight_smile:

TomGeorge:
Hi,

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html
then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Also what model Arduino are you using?

Thanks… Tom… :slight_smile:

I have edited my message as requested.

I use an Arduino Uno.

Delta_G:
Please read the “How to Use This Forum” thread for hint on how to post code to make it more readable using code tags.

This line is wrong. While it is fine to write this in human terms, a computer just can’t understand:

while (50<distance<120)

It should be two separate conditions.

while (50<distance && distance<120)

The next line defines a function. But a function cannot be defined within a function. I’d suggest some reading on C++ functions. You need to define the function outside of the loop and call it from within.

Hello Delta_G,

I have edited this part of my program this way:

while (50<distance && distance<120)
{
float moyenneNote(float tableau[], int nombreValeur)
{
    int i = 0;
    double total = 0;
    float moyenne = 0; 
    for(i = 0; i < 10; i++)
    {
        total = total + tableau[i];
    }
    moyenne = total / 10;
    return moyenne;
    int valeur0 = tableau[0];
    int valeur10 = tableau[10];
}
}
if (tableau[10]< moyenne && moyenne < tableau[0])
{
 {
  digitalWrite(led, HIGH);   
  delay(500);              
  digitalWrite(led, LOW);  
  delay(500);              
  digitalWrite(led2,LOW); 
}

else
{
  digitalWrite(led,HIGH); 
  digitalWrite(led2,LOW);
}

However I do not understand this.

Delta_G:
The next line defines a function. But a function cannot be defined within a function. I’d suggest some reading on C++ functions. You need to define the function outside of the loop and call it from within.

What’s hard to understand? This defines a function:

float moyenneNote(float tableau[], int nombreValeur)
{
    int i = 0;
    double total = 0;
    float moyenne = 0; 
    for(i = 0; i < 10; i++)
    {
        total = total + tableau[i];
    }
    moyenne = total / 10;
    return moyenne;
    int valeur0 = tableau[0];
    int valeur10 = tableau[10];
}

But that function is being defined inside another function which is illegal. GO DO SOME READING ON C++ FUNCTIONS and you might start to understand.

Delta_G:
What’s hard to understand? This defines a function:

float moyenneNote(float tableau[], int nombreValeur)

{
    int i = 0;
    double total = 0;
    float moyenne = 0;
    for(i = 0; i < 10; i++)
    {
        total = total + tableau[i];
    }
    moyenne = total / 10;
    return moyenne;
    int valeur0 = tableau[0];
    int valeur10 = tableau[10];
}




But that function is being defined inside another function which is illegal. GO DO SOME READING ON C++ FUNCTIONS and you might start to understand.

I wish I could learn C++ by heart and be the best at it, but I can’t. If I put the function outside the other function, the “While” function loses all its sense because it becomes empty.

Can you modify this part directly on my code to make it clear for me? please.

    moyenne = total / 10;
    return moyenne;
    int valeur0 = tableau[0];
    int valeur10 = tableau[10];

A return marks the end of a particular code path; nothing after it is executed.

But you would add a line in there to CALL your function.

GO READ A TUTORIAL ON C++ FUNCTIONS! Are you just not willing to do any reading on your own? I get that you don't think you can learn the whole language by heart, but you won't learn anything if you are unwilling to read. If that is really the case and you are just unwilling to read, then put the Arduino away, it's not for you.

Here's a link, maybe this will help you to understand the difference between defining a function and calling one. I can't rewrite this for you because it isn't clear to me what you want it to do.

http://www.tutorialspoint.com/cplusplus/cpp_functions.htm

Delta_G:
But you would add a line in there to CALL your function.

GO READ A TUTORIAL ON C++ FUNCTIONS! Are you just not willing to do any reading on your own? I get that you don't think you can learn the whole language by heart, but you won't learn anything if you are unwilling to read. If that is really the case and you are just unwilling to read, then put the Arduino away, it's not for you.

Here's a link, maybe this will help you to understand the difference between defining a function and calling one. I can't rewrite this for you because it isn't clear to me what you want it to do.

http://www.tutorialspoint.com/cplusplus/cpp_functions.htm

Hello,

Indeed that was a dumb mistake, but I am a nuub in computing. I followed your remark on functions and I modified all the mistakes. The program should work now.

Thank you.