arduino compiling failure

Hi, I have this relation between create freq with my speaker. But the relation with the ultrasonic sensor doesnt work.
If someone see the problem would be gratefull, thanks a lot.

#include <NewPing.h>
const byte UltrasonicPin = 5;
const unsigned int MaxDistance = 200;

NewPing sonar(UltrasonicPin, UltrasonicPin, MaxDistance);

//#include <SoftwareSerial.h>
//SoftwareSerial DFPlayerSerial(10, 11); // RX, TX


float distance() //distance-reprdouce one frequency

#define MIN_FREQ 40
#define MAX_FREQ 500


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

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

  int distance = sonar.ping_cm();
  if (distance > 50) {
    noTone(4);
  } else {
    int freq = map(distance, 0, 50, MIN_FREQ, MAX_FREQ);// al cuadrat log expo-
    freq = constrain(freq, MIN_FREQ, MAX_FREQ);
    tone(4, freq);
  }
}

What do your debug prints tell you?

Oh. Wait.

Error for board arduino one

#include <NewPing.h>

#define MIN_FREQ 40
#define MAX_FREQ 500

const byte UltrasonicPin = 5;
const unsigned int MaxDistance = 200;

const int trigPin = 9;
const int echoPin = 10;

NewPing sonar(UltrasonicPin, UltrasonicPin, MaxDistance);

//#include <SoftwareSerial.h>
//SoftwareSerial DFPlayerSerial(10, 11); // RX, TX


float distance(); //distance-reprdouce one frequency

void setup() {
  Serial.begin(9600);
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);

}

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

  int distance = sonar.ping_cm();
  if (distance > 50) {
    noTone(4);
  } else {
    int freq = map(distance, 0, 50, MIN_FREQ, MAX_FREQ);// al cuadrat log expo-
    freq = constrain(freq, MIN_FREQ, MAX_FREQ);
    tone(4, freq);
  }
}

Error for board arduino one

What error?

You commented-out the software serial, but still use it.
Is that your problem?

Why have you put Serial.begin in loop()?

First of all thanks.
Ok serial begin out.
The error is exit status 1
Error compiling arduino one

No. There is more than that.
Please post all the error message(s)

Tone.cpp.o(symbol from plugin): In function "timer0_pin_port

(.text+0x0) : multiple definition of "_vector_7"

Libraries/NewPing/NewPing.cpp.o (symbol from plugin):(text+0x0):

Collect2.exe:error 1d returned 1 exit status

ok i change things

this are the rrors

sketch_mar05c:16:20: error: expected initializer before ‘setup’

sketch_mar05c:20:1: error: expected initializer before ‘void’

void setup() {

^

sketch_mar05c:27:1: error: expected declaration before ‘}’ token

}

^

exit status 1
expected initializer before ‘setup’

#include <NewPing.h>

#define MIN_FREQ 40
#define MAX_FREQ 500 

const byte UltrasonicPin = 5;
const unsigned int MaxDistance = 200;

const int trigPin = 9;
const int echoPin = 10;

NewPing sonar(UltrasonicPin, UltrasonicPin, MaxDistance);

//#include <SoftwareSerial.h>
//SoftwareSerial DFPlayerSerial(10, 11); // RX, TX

float distance //distance-reprdouce one frequency


void setup() {
  //Serial.begin(9600);
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);


}
}

void loop()
{
  //Serial.begin(9600);

  int distance = sonar.ping_cm();
  if (distance > 50) {
    noTone(4);
  } else {
    int freq = map(distance, 0, 50, MIN_FREQ, MAX_FREQ);// al cuadrat log expo-
    freq = constrain(freq, MIN_FREQ, MAX_FREQ);
    tone(4, freq);
  }
}

So, drop the NewPing library - drive the sensor directly using the example code.
Put the Serial.begin in setup()
And put a semicolon after “distance”.
Lose the excess } at the end of setup.

yes the libary is included
I changed the ;
but still give me the errors

Tone.cpp.o (symbol from plugin): In function `timer0_pin_port':

(.text+0x0): multiple definition of `__vector_7'

libraries\NewPing\NewPing.cpp.o (symbol from plugin):(.text+0x0): first defined here

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Uno.
#include <NewPing.h>

#define MIN_FREQ 40
#define MAX_FREQ 500 

const byte UltrasonicPin = 5;
const unsigned int MaxDistance = 200;

const int trigPin = 9;
const int echoPin = 10;

NewPing sonar(UltrasonicPin, UltrasonicPin, MaxDistance);

//#include <SoftwareSerial.h>
//SoftwareSerial DFPlayerSerial(10, 11); // RX, TX

float distance; //distance-reprdouce one frequency


void setup() {
  Serial.begin(9600);
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);

}


void loop()
{
  //Serial.begin(9600);

  int distance = sonar.ping_cm();
  if (distance > 50) {
    noTone(4);
  } else {
    int freq = map(distance, 0, 50, MIN_FREQ, MAX_FREQ);// al cuadrat log expo-
    freq = constrain(freq, MIN_FREQ, MAX_FREQ);
    tone(4, freq);
  }
}

I wrote "drop the NewPing library - drive the sensor directly using the example code’

I dont undesrtand that…

Remove any reference to the NewPing library.

Use digitalWrite/pinMode/ pulseIn to drive the sensor instead.

const byte UltrasonicPin = 5;
...
const int trigPin = 9;
const int echoPin = 10;

Which type of ultrasound do you have?

HSC3

pepo177:
HSC3

Do you have a link to it?

about digital write, I cant change it

#define MIN_FREQ 40
#define MAX_FREQ 500 

const byte UltrasonicPin = 5;
const unsigned int MaxDistance = 200;

const int trigPin = 9;
const int echoPin = 10;

digitalWrite sonar(UltrasonicPin, UltrasonicPin, MaxDistance);

What has pin 5 got to do with it?
You need a trigger pin and an echo pin.

Where does HSC3 come into this?

Whit is this nonsense?digitalWrite sonar(UltrasonicPin, UltrasonicPin,

but how I can code that?
thanks for your time

that give me errors, digitWrite does not name a type.