Arduino Help

i am writing a code which will hopefully replicate a parking sensor using ultrasonic sensor, it is really similar to the PING tutorial. i am trying to add a buzzer which should start to beep more frequent as the distance to an object reduces. the buzzer needs to beep at 4cm and then more frequent at 3cm and even more frequent at 2cm and pretty much flatline at 1cm. please help me, its probably very simple.

here is my code:

byte triggerright=2;
byte echoright = 3;
float distance, cm;

byte triggerleft=4;
byte echoleft = 5;
int distance2;
byte buzzer=11;
void setup (){
pinMode(buzzer, OUTPUT);
pinMode (triggerright, OUTPUT);
pinMode (echoright, INPUT);
pinMode (triggerright, OUTPUT);
pinMode (echoright, INPUT);

pinMode (triggerleft, OUTPUT);
pinMode (echoleft, INPUT);
pinMode (triggerleft, OUTPUT);
pinMode (echoleft, INPUT);

Serial.begin (9600);

}

void loop (){

digitalWrite(triggerright, LOW);
delayMicroseconds(2);
digitalWrite(triggerright, HIGH);
delayMicroseconds(15);
digitalWrite(triggerright, LOW);
delayMicroseconds(20);

digitalWrite(triggerleft, LOW);
delayMicroseconds(2);
digitalWrite(triggerleft, HIGH);
delayMicroseconds(15);
digitalWrite(triggerleft, LOW);
delayMicroseconds(20);

distance = pulseIn (echoright,HIGH);

cm = microsecondsToCentimeters(distance);
/* Serial.println("the distance is...");
Serial.print((2,3));Serial.print("cm");
Serial.println("");
delay (1500);
*/

while (cm<4)

{
/* analogWrite (11,128);
delay (150);
digitalWrite (11,LOW);
delay (150);
analogWrite (11,128);
delay (150);
digitalWrite (11,LOW);
delay (150);

*/

analogWrite(11, 128);
delay(100);
digitalWrite(11, LOW);
delay(100);
analogWrite(11, 128);
delay(100);
digitalWrite(11, LOW);
delay(100);
}

Serial.print(cm);
Serial.print("cm");
Serial.println();
delay (2000);

}
float microsecondsToCentimeters(float microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

pinMode (triggerright, OUTPUT);
pinMode (echoright, INPUT);
pinMode (triggerright, OUTPUT);
pinMode (echoright, INPUT);

Setting the pin mode once isn't good enough?

digitalWrite(triggerright, LOW);
delayMicroseconds(2);
digitalWrite(triggerright, HIGH);
delayMicroseconds(15);
digitalWrite(triggerright, LOW);
delayMicroseconds(20);

No comments explaining what this is doing...

distance = pulseIn (echoright,HIGH);

You wait to read a return pulse until after you get done diddling around with the left one. Why?

How do you expect this loop to ever end?

while (cm<4)
{
  analogWrite(11, 128);   
  delay(100);                 
  digitalWrite(11, LOW); 
  delay(100);
  analogWrite(11, 128);   
  delay(100);                 
  digitalWrite(11, LOW); 
  delay(100);
}

its probably very simple.

Probably. But, it would be even simpler if you told us what the problem is.

Most important tstep for you to make is creating your own functions. then you can do a top down approach which helps to keep a helicopterview.

(this code is just an overall structure, it will not compile due to typos etc)

// VARIABLES
int distance, prevDistance;
int buzzerFreq = 100;

void setup()
{
  Serial.begin(115200); // fastest speed takes less time
  Serial.print("Start...");
  prevDistance = readDistance();
}

void loop()
{
  // READ DISTANCE
  distance = readDistance(PINGPIN);  // function to be defined

  // COMPARE IT TO PREVIOUS TO CALC NEW BUZZER VALUE
  buzzerFreq = freq(prevDistance, distance);
  prevDistance = distance;

  // DO THE BUZZER
  doBuzzerFreq();

}

The above gives more overview and shows what you want to accomplish. And yes, you still need to write the appropiate functions. But these are smaller, can be tested separately , and if proven to work they ca be reused.

Succes,

Paul S,

this is the part im having a problem with

while (cm<4)
"{
analogWrite(11, 128);
delay(100);
digitalWrite(11, LOW);
delay(100);
analogWrite(11, 128);
delay(100);
digitalWrite(11, LOW);
delay(100);
}"

the buzzer just stays on and the rest of the program doest work. i need to insert a break but i don't know how.

using a sensor like this one:http://www.elexp.com/test/ping.jpg
i am trying to create a parking sensor which is similar to a car and will warn the driver (via buzzer) how close an object is. the main problem is implementing the buzzer to increase its frequency (get louder and have less delay) as an object is nearer

robtillaart,

i am pretty poor at writing code, as you've seen, and i don't know how to create functions

robtillaart,

i am pretty poor at writing code, as you've seen, and i don't know how to create functions

Please read - http://www.arduino.cc/en/Reference/FunctionDeclaration

This is an example of you using a function and how it is written. ==> cm = microsecondsToCentimeters(distance);
In fact setup() and loop() are two - predefined and mandatory - functions.

It is good to spend time on the tutorial and reference pages, reading the examples and running them to get an understanding of how to write code for the Arduino
Time spend there is no way time lost :wink:

while (cm<4)
"{
  analogWrite(11, 128);   
  delay(100);                 
  digitalWrite(11, LOW);
  delay(100);
  analogWrite(11, 128);   
  delay(100);                 
  digitalWrite(11, LOW);
  delay(100);
}"

the loop will run as long as cm is smaller as 4. As the value does not change inside the loop it will never exit it.

Maybe you should first focus on a bit simpler program to get the readDistance() function right. Taking the problem step by step makes it manageble.

// VARIABLES
int distance;

void setup()
{
  Serial.begin(115200); 
  Serial.print("Start...");
}

void loop()
{
  // READ DISTANCE
  distance = readDistance(PINGPIN);  // function to be defined

  // SEND DISTANCE TO PC
  Serial.println(distance, DEC);
  delay(500);
}


int readDistance(int pinNumber)
{
  int d = 0;
  unsigned long microSecs;

  .. add code here to determine microseconds
  
  d = microsecondsToCentimeters(microSecs)
  return d;
}


float microsecondsToCentimeters(float microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

this is the simplest form i can get the code to be:

double ping(int outPin, int inPin)// declaring the ultrasonic sensor

{
int buzzer =11;
long duration;
pinMode(outPin, OUTPUT);
pinMode(inPin, INPUT);
digitalWrite(outPin, LOW);
delayMicroseconds(2);
digitalWrite(outPin, HIGH);
delayMicroseconds(15);
digitalWrite(outPin, LOW);
delayMicroseconds(20);
duration = pulseIn(inPin, HIGH);
return duration / 29.0 / 2.0;
}

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

void loop()
{

Serial.print(ping(2, 3)); Serial.println("cm");
delay(500);
}

the code works perfectly in reading distance but when i try to add the buzzer it doesn't work. i have literally racked my brains and tried everything i know but evidently it is not enough. all i need is to add a buzzer which will beep more frequent from 4cm to 1cm, thats all. if anyone knows how to this then plz help

Ok. Now, on each pass through loop, you calculate a distance. What you need to do now is make a noise ONCE. The duration of that noise, and the delay after making the noise, will be a function of the distance.

double dist = ping(2,3);
if(dist < 2)
{
  // make long noise
}
else if(dist < 3)
{
  // make medium length noise
}
else if(dist < 4)
{
  // make short noise
}
else
{
   // be quiet
}

When the noise making ends, loop ends, and gets called again. When it does, it will compute the distance again, and make some more noise, if necessary.

By the way, the accuracy of your sensors make not enable you to distinguish 1 centimeter differences in distance.

yeah you're right about the sensor not being able to read 1cm. thank you very much for the help guys!!i will now try and sort it out and ill be back if doesn't work!