Please 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. 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;
}

You are slowing the code down using the delay's when turning the buzzer on and off. also when you write to your pins use the following.

Instead of:
analogWrite(10, 128);
USE:
analogWrite(buzzer, 128);

Instead of:
digitalWrite(11, High);
Use:
digitalWrite(greenled, HIGH);

thats why you declared them in the first place

my sensors slow down also, they are okay if the distance is far but a distance below 1 metre creates a delay somehow. is there a way to get past that? also, is there a way to make the code re read the distance value while in the if statement?

From a quick read, i see at least two major issues to fix.

First, for this way of structuring the code, it would be better to test the shorter distances first. So instead of:

if(x < 1)
{
}
else if (x < .5)
{
}
else if(x < .25)
{
}

Do it this way:

if(x < .25)
{
}
else if (x < .5)
{
}
else if(x < 1)
{
}

Next, why are all those delays in there? If you add up all those delay() calls you have quite a lot of delay, like a few seconds.

You need to write the code without ever calling delay().

Okay I see why you have the delay statements. This is extremely similar in concept to the "blink without delay" example:

Have a look at that, it should answer your questions about how to do this without delay() calls.

i used the blinkwithoutdelay example to make the following code, when i need to buzz some stuff, it might help but you do have to take all delays out of your code.

// Variables will change:
const int buzzer = 10;
int buzzerState = 0;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

void buzz(int interval)
{
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (buzzerState == 0)
      buzzerState = 128;
    else
      buzzerState = 0;

    // set the LED with the ledState of the variable:
    analogWrite(buzzer, buzzerState);
}
}

void setup(){
pinMode(buzzer, OUTPUT);
}

void loop(){
buzz(100); // when you want to buzz use this
}

I would try using while instead of if

while (distance =< .25){
buzz(100);
distanceFunction(); //MAKE A FUNCTION TO UPDATE DISTANCE HERE 
}

while (distance <= .5 && distance > .25){
buzz(400);
distanceFunction(); //MAKE A FUNCTION TO UPDATE DISTANCE HERE 
}

while (distance <= 1 && distance > .5){
buzz(700);
digitalWrite(greenled, HIGH);
distanceFunction(); //MAKE A FUNCTION TO UPDATE DISTANCE HERE 
digitalWrite(greenled, LOW);
}

You can put it in what ever order you want, the last one i added the digitalWrite(greenled) to bit bang your led the whole time, it will look as if its on the whole time. Without bit banging it would stay on when the while statement is no longer true. Just an idea.

mate i'm gonna sound stupid but i don't even know how to declare functions, I've looked at the example on arduino but they don't really make sense to me to be honest. can i get an example using my code maybe? or better yet could someone just really help me out by using my code to sort out the delay problem??

i probably sound like im taking the piss but i really appreciate this guys honestly!!

tnovak,

i really appreciate the help and you probably cannot be bothered to detail the code you're trying to explain to me. i understand what you're but like i said before (not trying to be rude!) i dont know how to declare functions, attach interrupts or use other simple codes you're saying. could you just use my code and help?? please

I don't mind helping at all there are things about the arduino that i just don't get and need help with also, so i don't mind trying to help. Functions seem difficult at first but they are easier than you think, same with interrupts just takes time. I have started to write most the code to finish what started here, just didn't want to give you the whole thing because theres no learning there. (And im sure there is a better way yet todo it). Not to be rude here either, but it would be easier for me to start over than to use the code you posted. Im sure there is someone else that can help, I will check later and if you haven't gotten any answer I will finish more code and try to explain how it works. Ive been in front of this screen all day so im gonna give it a break until tomorrow, good luck.

i appreciate your honesty and its quite evident that i haven't just posted a question and expected someone to answer it straightaway without me showing any desire to do it myself. i can accept that some people are better than me at this code stuff and i have tried to do it myself but it is beyond me clearly. i have to present a working model by Tuesday and i will keep going but if you could just help me by showing me the answers im looking for then that would be brilliant!! my main problem is the delay stuff on the buzzer. i really need to get this right or ill lose marks which i cant afford to lose. thank you.

tnovak,

any chance you could help now?

Cross-posting just makes people cross.
Don't do it.

why is so hard for someone to show me the right bit of code so i can stop wasting time??the whole point of posting problems is so someone can show you how to do it properly

why is so hard for someone to show me the right bit of code so i can stop wasting time??

You want us to do your homework for you?
That would be cheating.
Besides, it wouldn't be any fun for you.

You've been given plenty of pointers here and in your other post - it's up to you to pull them all together.

no its not about doing my homework for me, i have done most of it I JUST NEED TO GET RID OF THE DELAY BUT I DONT KNOW HOW TO and referring me to blinkwithoutdelay doesnt really help. Ive explained that i do not understand it. if you're still reluctant to help then fair enough and thanks to everyone who tried.

OK, think about blink without delay.
Two different ways of blinking a LED.

  1. The delay way.
    Just keep looking at the second hand of your clock until n seconds has elapsed.
    Switch on the light.
    Just keep looking at the second hand of your clock until n seconds has elapsed.
    Switch off the light.

  2. The right way.
    Write down the time. Go off and put the kettle on.
    Look at the time - has n seconds elapsed?
    No, get out a cup.
    Look at the time - has n seconds elapsed?
    No, get the teabags out of the cupboard.
    Look at the time - has n seconds elapsed?
    No, get the milk out of the fridge.
    Look at the time - has n seconds elapsed?
    Yes, switch on the light.
    Write down the time.

Does that help?
(you can substitute coffee for tea if you prefer)

lol yeah it does and that was the way i thought it might be. thinking about my problem logically then that is what i need to do but writing the code itself is the problem. thanks for your help mate i appreciate it, ill just hand it in as it is tbh i've spent enough time looking at examples and trying to get to grips with it but if ya cant do it...ya cant do it!

I don't know how to explain blink without delay any simpler.

You are very close, but if you're leaving it as it is, I'd suggest tidying up the code before presentation.

Many people here are happy to help, and I would have helped more but I really have a pile of work to do and, if I don't do it, I don't get marked down, my family's bills don't get paid. We can't write your code because it takes time to code, debug, post and then explain. Personally, I'm happy to answer questions when I can, but as someone pointed out, we're not going to do your homework for you.

But this would be easier if someone were sitting next to you explaining. There is no one on campus you can solicit assistance from? You have all weekend. Find someone who knows, buy a pizza and get them to show you how it works. That would be one heck of a lot quicker than getting us to teach you in a web forum.

skyjumper,

it's a bank holiday weekend which means university is closed until Tuesday, the day i need to hand this in. like i said before, i appreciate everyone trying to help and teach at the same time but it's only one section of my code thats wrong and i just needed the correct code which would probably take one of you guys about 15 min but once again thanks anyway.

AWOL,

You are very close, but if you're leaving it as it is, I'd suggest tidying up the code before presentation.

i'll order a pizza to your place if you help me :slight_smile: lol