Cannot setup a fonction

Hello, I've quite litteraly 0 notions in arduino but I need to do a school projet. basically I'm using multiple HC-SR04 so I wanted to create a function to initialise and use them all

int ultrason(int echoPin, int trigPin)
{  
  // defines variables
  long duration;
  int distance;
  void setup() 
  {
    pinMode(echoPin, INPUT); // Sets the echoPin as an Input    
    pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
    Serial.begin(9600); // Starts the serial communication
  }
  void loop() 
  {
    // Clears the trigPin
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    // Sets the trigPin on HIGH state for 10 micro seconds
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    // Reads the echoPin, returns the sound wave travel time in microseconds
    duration = pulseIn(echoPin, HIGH);
    // Calculating the distance
    distance = duration * 0.034 / 2;
    // Prints the distance on the Serial Monitor
//    Serial.print("Distance: ");
//    Serial.println(distance);
    return (distance);
   }
}

this is the code I wrote, I took an exemple on google and changed it a bit. I keep having the error "a function-definition is not allowed here before '{' token".

You can't define a function inside another function.
You have two functions defined in the function ultrason

How could I change that to make it working ? I've tried removing the function and just run the code once but it doesn't work

Move this (last line of your code)... before this line..

Few other errors.. but this will get you closer.

int ultrason(int echoPin, int trigPin){
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input    
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  long duration;
  int distance;
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2;
  // Prints the distance on the Serial Monitor
  //    Serial.print("Distance: ");
  //    Serial.println(distance);
  return (distance);
}

got it working ! I assume when I was re-initialising the serial it was causing problems

is there any way to add a setup in a function ?

Not exactly sure what you mean.

An Arduino sketch always has a setup() (runs once) and loop() (runs forever) functions... but you can create your own functions... they should be called from either setup or loop.

This conclusion is completely wrong
void myFunction {
// function myFuncB is inside function myFunction
void myFuncB {
....} // end of function myFuncB
} // end of function myFunction

and this is what does not work

it has to be this way
void myFunction {

} // end of function myFunction

// function myFuncB is outside function myFunction
void myFuncB {
} // end of function myFuncB

best regards Stefan

Hi, @doreori
Welcome to the forum.

Please look at the NewPing library for HC-SR04 ultrasonic.
It will make your code easier and tidier and it has a 3 sonic sensor code as an example.
The Library does most if the distance calcs for you.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

sorry for being that late, but this is EXACTLY what I was looking for. Thank you so much, it really helped me a lot

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