Ultrasonic sensor and buzzer

Hello.
Please please help with this code. I've been working on it for many weeks, and cannot make it work. I really need your help.

Purpose of project:
Whenever the US sensor detects an obstacle, a buzzer will sound.

This is what I've done.
Please help me to debug this code.

int ultraSoundSignal = 7;
int val = 0;
int ultrasoundValue = 0;
int timecount = 0; // Echo counter
int ledPin = 13;
int BUZZ = 4
byte i;

void setup()
{
Serial.begin(9600); // Sets the baud rate to 9600
pinMode(ledPin, OUTPUT); // Sets the digital pin as output
}

void loop()
{
timecount = 0;
val = 0;
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output

/* Send low-high-low pulse to activate the trigger pulse of the sensor


*/

digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff

/* Listening for echo pulse


*/

pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
val = digitalRead(ultraSoundSignal); // Append signal value to val
while(val == LOW) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
}
while(val == HIGH)
{ // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
timecount = timecount +1; // Count echo pulse time
}

ultrasoundValue = timecount; // Append echo pulse time to ultrasoundValue
serialWrite('A'); // Example identifier for the sensor
printInteger(ultrasoundValue);
serialWrite(10);
//serialWrite(13);

if(timecount > 0)

{
pinMode(BUZZ,OUTPUT);
for (i=0;i<6;i++)
{
digitalWrite(BUZZ,HIGH);
delay(500);
digitalWrite(BUZZ,LOW);
delay(500);
}
}

What does that code do that you don't want? What does it not do that you do want? Where are your debug statements?

while(val == HIGH)
{ // Loop until pin reads a high value
  val = digitalRead(ultraSoundSignal);
  timecount = timecount +1;            // Count echo pulse time
}

This is not anywhere near the correct way to time a pulse.

serialWrite('A'); // Example identifier for the sensor
printInteger(ultrasoundValue);
serialWrite(10);

Where are these functions?

I think you should toss that book in the trash. It is garbage.

That code doesn't compile. There is at least one closing brace missing, and a missing semicolon.

  serialWrite('A'); // Example identifier for the sensor
  printInteger(ultrasoundValue);

What are these lines supposed to do? Have you missed these functions off?

If you are going to ask for help, at least post code that compiles.

Beaten to it again!

Duplicate post deleted.
OP, do not cross post, it wastes time.

Thank you for your kind replies.

The purpose of this project is to make the buzzer sound every time the US sensor detects an obstacle.
I'm a mature student with little experience in C programming.

I'm using a PING US sensor, and a 3-6 V buzzer.

Your help is MUCH APPRECIATED.

I appologize for not being able to answer all your questions. My knowledge in programming is very limited.

Kind regards,
Elena

There is a Ping tutorial over at the Playground, have you tried it?

Yes, I did try it.
It works fine.

It's just that I don't know how to make the buzzer sound INSTEAD of the distance showing on the computer screen.

Thank you for your reply.

Can you make the buzzer sound without the u/s sensor?

Yes. The buzzer sounds perfectly.

This is the code JUST for the buzzer.

#define BUZZ 4
byte i;
void setup()
{
pinMode(BUZZ,OUTPUT);
for (i=0;i<6;i++) {
digitalWrite(BUZZ,HIGH);
delay(500);
digitalWrite(BUZZ,LOW);
delay(500);
}
}

void loop()
{}

Is your problem that for the six seconds you're sounding the buzzer, you're not reading the u/s sensor?

The "six" sounds are fine. Just what I need.

The project consists of a blind stick with an US sensor that will buzz (6 times) as soon as it senses an obstacle.

I DO have a program (found in google) that works with the PING sensor, but the distance is displayed on the computer screen INSTEAD of making the buzzer sound.

I don't know how to connect these two codes (US with buzzer)

All I've come up with is in my very first post. I tried my best. Worked on it for 3 weeks, but I'm not able to make it work. My knowledge on C programming is VERY BASIC.

I DO have a program (found in google) that works with the PING sensor, but the distance is displayed on the computer screen INSTEAD of making the buzzer sound.

The range displayed has to be calculated and stored, so why don't you simply use that value to decide whether or not to sound the buzzer?
As I suggested, you could travel quite a way in six seconds, so you may want to look at ways of eliminating long delays.
Hint: look at the blink without delay example.

Very good idea about the delay. I will fix that.

I'm now going to class. I will be back in 4 hours or so.

Thank you AGAIN for your help.

Elena

Just to inform all of you that the code has been fixed and works.

Purpose of code: buzzer beeps as soon as the ultrasonic sensor detects an object.

All the best to those working on Arduino projects!!