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. this is a mini project i am being assessed on so i'm going all out and have even added LED's for visual aid. the LED's go from green to amber to red as the distance increases. the problem is that there seems to be really long delay when reading the distance and the buzzer should be constantly on when the if statement applies but it comes on then off then on again.please help me, its probably very simple.
byte triggerright=2; // declaring the pins which will be in use
byte echoright = 3;
int redled=13;
byte yellowled=12;
byte greenled=11;
float distance, m; // used a float function because of output value given
// as a decimal
byte buzzer=10; // buzzer using the PWM pin
void setup (){
// declaring what the pins are used as
pinMode(buzzer, OUTPUT);
pinMode (triggerright, OUTPUT);
pinMode (echoright, INPUT);
pinMode (redled, OUTPUT);
Serial.begin (9600); // set up the baud rate for serial printing
}
void loop (){
digitalWrite(triggerright, LOW); // start of with no pulse being sent
delayMicroseconds(2);
digitalWrite(triggerright, HIGH); // then 2 microseconds later a pulse is sent out for a 15 microsecond delay
delayMicroseconds(15);
digitalWrite(triggerright, LOW); // then the pulse is stopped being sent for 20 microseconds
delayMicroseconds(20);
distance = pulseIn (echoright,HIGH); //Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseIn() waits for the pin to go HIGH,
//starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds
//Gives up and returns 0 if no pulse starts within a specified time out.
m = microsecondsToMetres(distance); // setting up the distance function to be microseconds per metre
distance = (m); // declaring that distance is function m
if(distance < 1) // using if statement to declare that if distance<1 then buzzer will be used
{
analogWrite(10, 128); // writes an analog value to pin 11.the pin will
// generate a steady square wave of the specified duty cycle
delay(700); // specified duty cycle of 0.7 seconds
analogWrite(10, LOW); // pin 11 will be assigned 0v for a specified time
delay(700); // pin 11 will stay at 0v for 0.7 seconds
analogWrite(10, 128);
delay(700);
digitalWrite(10, LOW);
delay(700);
digitalWrite (11,HIGH);
}
else if(distance < 0.5) // using if statement to declare that if distance<0.5 then buzzer will be used
{
analogWrite(10, 128);
delay(400);
digitalWrite(10, LOW);
delay(400);
analogWrite(10, 128);
delay(400);
digitalWrite(10, LOW);
delay(400);
digitalWrite (13,);
digitalWrite (12,HIGH);
}
else if(distance < 0.25) // using if statement to declare that if distance<0.25 then buzzer will be used
{
analogWrite(10, 128);
delay(100);
digitalWrite(10, LOW);
delay(100);
analogWrite(10, 128);
delay(100);
digitalWrite(10, LOW);
delay(100);
digitalWrite (13,HIGH);
}
else if (distance>5) // if distance is over 5 then the serial will print "the distance is over 5"
{
Serial.println("the distance is over 5m...");
delay (1500);
}
else {}
digitalWrite (13,LOW);
digitalWrite (13,LOW);
digitalWrite (13,LOW);
Serial.print(distance); // serial prints the distance funtion which is declared prior to this line
Serial.print("m"); // distance is printed in metres
Serial.println(); // inserts gap line in the print
delay (500); // distance will print every 2 seconds
}
float microsecondsToMetres(float microseconds)
{
// The speed of sound is 340 m/s or 2938 microseconds per metre.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 2938 / 2;
}