Problems with the HC-SR04

Hi, i just started with Arduino and i have a Little Problem with programming sth. for School.

My Task is to use the HC-SR04 ultrasonic sensor and the piezo-Speaker. I have to make sure that if a ball comes Closer than 8cm to the sensor, it gives 2 different tones with a Little delay between them.
The Problem I have is with the twoe different tones and the delay.

can anyone pleeeeeeeeease help me . I´m sitting on it for 2 Hours already and i´m running out of time. I hope someone can help me. I´m adding the Code I have already.

int trigger=7;
int echo=6;
long dauer=0;
long entfernung=0;
int piezo=5;
void setup()

{

Serial.begin (9600);

pinMode(trigger, OUTPUT);

pinMode(echo, INPUT);

pinMode(piezo, OUTPUT);

}

void loop()

{

digitalWrite(trigger, LOW);

delay(5);

digitalWrite(trigger, HIGH);

delay(10);

digitalWrite(trigger, LOW);

dauer = pulseIn(echo, HIGH);

entfernung = (dauer/2) * 0.03432;

if (distance >= 500 || distance <= 0)

{

Serial.println;

}

else

{

Serial.print;

Serial.println(" cm");

}

if (entfernung <= 80)
{
digitalWrite(piezo,HIGH);
}

else
{
digitalWrite(piezo,LOW);
}

delay(1000);
}

The Problem I have is with the twoe different tones and the delay.

What exactly is the problem ?

What do think that these 2 lines of code do ?

Serial.println;
Serial.print;

Does the code actually compile ?

Hi UKHeliBob,
I appreciate a lot that you try to help me with this.

The Problem is that i Need to get the piezo Speaker to Play just two tones with a delay between it (1sec) , IF the ball Comes for 10 seconds Closer than 8cm to the sensor.

I hope I expressed myself in a more understandable way now (;

ChiefMarco:
My Task is to use the HC-SR04 ultrasonic sensor and the piezo-Speaker. I have to make sure that if a ball comes Closer than 8cm to the sensor, it gives 2 different tones with a Little delay between them.
The Problem I have is with the twoe different tones and the delay.

can anyone pleeeeeeeeease help me . I´m sitting on it for 2 Hours already and i´m running out of time. I hope someone can help me. I´m adding the Code I have already.

You don't have a variable named 'distance' but you try to use it. Remove the lines that use 'distance'.
If you want two different notes you will probably have to use a piezo speaker and the tone() function.

Hi John,
I actually use a piezo Speaker i´m just incapable of finding out the way that it Plays just 2 different tones with a delay between it.

How should I use the tone function because I´ve tried it already and it Shows Always some Errors.
By the way,
thanks for helping John

I´ve tried it already and it Shows Always some Errors.

Please post what you tried and the full error messages

so this is what i typed in:

int trigger=7;
int echo=6;
long dauer=0;
long entfernung=0;
int piezo=5;
void setup()

{

Serial.begin (9600);

pinMode(trigger, OUTPUT);

pinMode(echo, INPUT);

pinMode(piezo, OUTPUT);
}

void loop()

{

digitalWrite(trigger, LOW);

delay(5);

digitalWrite(trigger, HIGH);

delay(10);

digitalWrite(trigger, LOW);

distance = pulseIn(echo, HIGH);

distance = (dauer/2) * 0.03432;

if (distance >= 500 || entfernung <= 0)

{

Serial.println(“80”);

}

else

{

Serial.print(distance);

Serial.println(" 80cm");

}

if (distance <= 80)
{
digitalWrite();
{
tone(8, 100);
delay(1000);
noTone(8);
tone(8, 200)
}

else
{
digitalWrite(piezo,LOW);
}

delay(1000);
}

and that´s the Errors:

In function ‘void loop()’:
33:1: error: ‘distance’ was not declared in this scope
58:14: error: too few arguments to function ‘void digitalWrite(uint8_t, uint8_t)’
1:0:
126:6: note: declared here

64:1: error: expected ‘;’ before ‘}’ token
66:1: error: expected ‘}’ before ‘else’

distance = pulseIn(echo, HIGH);

So where is distance declared ? Answer : nowhere

digitalWrite();

Which pin do you want to write to and what value (HIGH or LOW) do you want to write ?

tone(8, 200)

No semicolon on the end of the line

if (distance <= 80)
{
digitalWrite(); 
  {
 tone(8, 100);
delay(1000);
noTone(8); 
tone(8, 200)
}

Which commands should be executed if the test returns true and which will always be executed ? HINT : put the { and } round the conditional code

On a more general note, you did not Auto Format the code in the IDE or use code tags when posting it so it is hard to follow and even has a smiley in it. Please read this before posting a programming question and follow the advice it contains

ChiefMarco:
How should I use the tone function because I´ve tried it already and it Shows Always some Errors.

Perhaps reading the reference material in your native language would help:
https://www.arduino.cc/reference/de/language/functions/advanced-io/tone/