Please Help!!

Does this help any:

float getDistance ()
{
  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);

  float 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.

  return microsecondsToMetres(distance);
}

(Don't like the sort of pizza that gets delivered)

Unless this is your last Arduino project ever, you will almost certainly need to master Blink without delay eventually. When that day comes, if you still don't get it, you might want to break out the paper computer:

Print the program
On another piece of paper, rule off a grid & list the variables and items you need to track as columns. This is mercifully short for this program:
ledState, previousMillis, interval, currentMillis are your variables
you should also have a column for millis and one for the led itself - is it on or off

'Run' the program. Start at the top & 'execute' each statement. Run setup. At each statement, if it changes one of your variables, fill in a new row on the grid. if a statement uses one of the variables, look up the value it currently has in the grid.Do the same with loop, again & again. Remember that millis will have increased on each loop. Start by increasing it by one. When you realize that that's going to take forever, use 300 instead. This will be very tedious, but after a few rounds of running loop, you will suddenly see what is happening.

Good luck

blitz22:
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.

So this is your homework. Don't get me wrong here. I am not criticizing you about seeking help for your problem, I have done it, and had forums existed then, I'd probably had used them the way you did.
But the fact that matters is, if you are planning on having any future in electronics and most especially in programming, resolving your problems by yourself is the best way to learn it. No matter how many times you look at a solution, you won't learn how to get to it without actually doing it.

I know it's hard flunking, but that happens for a reason and it ain't to hurt you.

blitz22:
... 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.

It's not 15 minutes.

Do this, write an entire different sketch with just that one section you're stuck on. Try to make it work one little thing at a time. If youre still stuck, then post that.

AWOL,

i used your function and started to test my program with LED first and then later on add a buzzer once i know whats going on

while (m <=.25)
{

digitalWrite(red, HIGH);//if m=>.25 then turn LED on
getDistance(); // check the distance again
if(m>.25) // if distance changes then turn LED off
digitalWrite(red, LOW);
break; // break from that loop
}

while (m <= 1 && m > .5)
{

digitalWrite(red, HIGH);
getDistance();
if(m>.25)
digitalWrite(red, LOW); /
break;
}

while (m <= 1 && m > .5)
{

digitalWrite(red, HIGH);
getDistance();
if(m>.25)
digitalWrite(red, LOW);
break;
}

i am using this piece of code to light the LED but the red LED stays on, the distance is still printed and although it changes, the red LED stays on. can you see whats wrong?

bubulindo,
i am studying a mechanical engineering degree and i have so much more work than this plus exams to revise for and i am not looking for a career in electronics.

GUYS ONCE AGAIN THANK YOU ALL

But you're not using the value that getDistance returns, so I don't really see how that is working at all.

should it be...

digitalWrite(red, HIGH);//if m=>.25 then turn LED on
    getDistance();           // check the distance again
    if(getDistance>.25)        // if distance changes then turn LED off
      digitalWrite(red, LOW);   
    break;          // break from that loop

"getDistance" is a pointer to a function.
It is almost certainly > 0.25.
"getDistance ()" calls the function that "getDistance" points to.

I just looked carefully for the first time at the blink without delay example.
It has seven functional statements ( and it wouldn't be hard to eliminate three of them).
You really need to persist with this one.

ahhhh i see. i've changed it i now use the if statement to read then getDistance function BUT the LED's are still on. sorry if i'm making some stupid mistake. i thought that the break would stop that while loop and move on to the next one but i think it does but leaves the LED's on.why?

here's the code

 while (m <= 1) 
  {

    digitalWrite(green, HIGH);
    getDistance(); 
    if(distanceread>1)
      digitalWrite(red, LOW);	
    break;
  }

No, I still don't see where distanceRead gets assigned - you need to post it all.

its at the bottom along with the first distance function

byte triggerright=2;  // declaring the pins which will be in use
byte echoright = 3;
float distance, m;
int buzzer = 10;
byte red=7;
byte yellow=8;
byte green=4;
float distanceread;
void setup (){
  pinMode(buzzer, OUTPUT);
  pinMode (triggerright, OUTPUT);
  pinMode (echoright, INPUT);
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(green, 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(5);
  digitalWrite(triggerright, LOW);    // then the pulse is stopped being sent for 20 microseconds

  distance = pulseIn (echoright,HIGH); 
  m = microsecondsToMetres(distance);
  distanceread = getDistance ();


  // while (m <= .25){
  //digitalWrite(red, HIGH);
  //getDistance(); 
  {
  while (m <=.25) 
  {

    digitalWrite(red, HIGH);//if m=>.25 then turn LED on
    getDistance();           // check the distance again
    if(distanceread>.25)        // if distance changes then turn LED off
      digitalWrite(red, LOW);	
    break;          // break from that loop
  } 


  while (m <= 0.5) 
  {

    digitalWrite(yellow, HIGH);
    getDistance(); 
    if(distanceread>.5)
      digitalWrite(red, LOW);	
    break;
  } 

  while (m <= 1) 
  {

    digitalWrite(green, HIGH);
    getDistance(); 
    if(distanceread>1)
      digitalWrite(red, LOW);	
    break;
  } 


  }

  Serial.print(m);      // 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 (2000);                // distance will print every 2 seconds




}

float getDistance ()
{
  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(5);
  digitalWrite(triggerright, LOW);    // then the pulse is stopped being sent for 20 microseconds


  float 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.

  return microsecondsToMetres(distance);
}




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;
}
void loop (){

// NOT NEEDED  digitalWrite(triggerright, LOW);    // start of with no pulse being sent
// NOT NEEDED  delayMicroseconds(2);
// NOT NEEDED  digitalWrite(triggerright, HIGH);   // then 2 microseconds later a pulse is sent out for a 15 microsecond delay
// NOT NEEDED  delayMicroseconds(5);
// NOT NEEDED  digitalWrite(triggerright, LOW);    // then the pulse is stopped being sent for 20 microseconds

// NOT NEEDED  distance = pulseIn (echoright,HIGH); 
// NOT NEEDED  m = microsecondsToMetres(distance);
  distanceread = getDistance (); //This you need, just ONCE per "loop"

  // m is not needed. At all. Just use "distanceread"
}

but now there is an error because i try and define that "float getDistance (float microseconds)" and it says too few arguments to function 'float getDistance(float)'

float getDistance (float microseconds)
{
  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(5);
  digitalWrite(triggerright, LOW);    // then the pulse is stopped being sent for 20 microseconds


  float 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.

   return microseconds / 2938 / 2;
}

That's not the definition I gave you.
Why have you changed it?
Look again at reply #21

yeah i messing around trying to get it to work. i got rid of the error eventually and here is the code BUT the LED's still dont work in the corresponding order they are supposed to, this is tough lol and i havent even added a buzzer!!

byte triggerright=2;  // declaring the pins which will be in use
byte echoright = 3;
//float distance, m;
int buzzer = 10;
byte red=7;
byte yellow=8;
byte green=4;
float distanceread;
void setup (){
  pinMode(buzzer, OUTPUT);
  pinMode (triggerright, OUTPUT);
  pinMode (echoright, INPUT);
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(green, 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(5);
//  digitalWrite(triggerright, LOW);    // then the pulse is stopped being sent for 20 microseconds

//  distance = pulseIn (echoright,HIGH); 
//  m = microsecondsToMetres(distance);
  distanceread = getDistance ();


  // while (m <= .25){
  //digitalWrite(red, HIGH);
  //getDistance(); 
  {
  while (distanceread <=.25) 
  {

    digitalWrite(red, HIGH);//if m=>.25 then turn LED on
    getDistance();           // check the distance again
    if(distanceread>.25)        // if distance changes then turn LED off
      digitalWrite(red, LOW);	
    break;          // break from that loop
  } 


  while (distanceread <= 0.5) 
  {

    digitalWrite(yellow, HIGH);
    getDistance(); 
    if(distanceread>.5)
      digitalWrite(yellow, LOW);	
    break;
  } 

  while (distanceread <= 1) 
  {

    digitalWrite(green, HIGH);
    getDistance(); 
    if(distanceread>1)
      digitalWrite(green, LOW);	
    break;
  } 


  }


  Serial.print(distanceread);      // 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 (2000);                // distance will print every 2 seconds




}

float getDistance ()
{
  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(5);
  digitalWrite(triggerright, LOW);    // then the pulse is stopped being sent for 20 microseconds


  float 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.

   return (distance) / 2938 / 2;
}

distanceread = getDistance (); //This you need, just ONCE per "loop"

I even put it in capitals, so it would be noticed.
Why do you think you need more while loops?
Really, I'm not making this up - I have a sketch running here for my ancient (last century) SRF04 flashing the on-board LED at a rate dependent on the range. The sketch is 40 lines long, including whitespace.

The commented stuff - you don't need it.
Just get rid of it - it's confusing.

This:

 delay (2000);                // distance will print every 2 seconds

isn't helping.

i dont understand...

Why do you think you need more while loops?

surely i need to specify what the LED's should do at a given distance dont i?

byte triggerright=2;  // declaring the pins which will be in use
byte echoright = 3;

int buzzer = 10;
byte red=7;
byte yellow=8;
byte green=4;
float distanceread;
void setup (){
  pinMode(buzzer, OUTPUT);
  pinMode (triggerright, OUTPUT);
  pinMode (echoright, INPUT);
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(green, OUTPUT);
  Serial.begin (9600);    // set up the baud rate for serial printing 
}

void loop (){

  {
  while (distanceread <=.25) 
  {

    digitalWrite(red, HIGH);
    getDistance();           
    if(distanceread>.25)        
      digitalWrite(red, LOW);	
    break;         
  } 


  while (distanceread <= 0.5) 
  {

    digitalWrite(yellow, HIGH);
    getDistance(); 
    if(distanceread>.5)
      digitalWrite(yellow, LOW);	
    break;
  } 

  while (distanceread <= 1) 
  {

    digitalWrite(green, HIGH);
    getDistance(); 
    if(distanceread>1)
      digitalWrite(green, LOW);	
    break;
  } 


  }


  Serial.print(distanceread);     
  Serial.print("m");          
  Serial.println();            
  delay (2000);                




}

float getDistance ()
{
  digitalWrite(triggerright, LOW);    
  delayMicroseconds(2);
  digitalWrite(triggerright, HIGH);   
  delayMicroseconds(5);
  digitalWrite(triggerright, LOW);    


  float distance = pulseIn (echoright, HIGH);  

   return (distance) / 2938 / 2;
}

surely i need to specify what the LED's should do at a given distance dont i?

Yes, I suppose you do.

Can I suggest you don't listen to the other guys looking over your shoulder - they aren't helping you.

  while (distanceread <=.25) 
  {

    digitalWrite(red, HIGH);
    getDistance();           
    if(distanceread>.25)        
      digitalWrite(red, LOW);	
    break;         
  }

See - distanceread has a value of zero first time through - you haven't assigned anything to it.
You don't assign anything to it in the loop, but you break out of the loop anyway.

You don't need any loops within "loop ()"

you haven't assigned anything to it.
You don't assign anything to it in the loop, but you break out of the loop anyway.

how would assign the distance value to distanceread? does it not already do if i say distanceread = getDistance ();??

distanceread = getDistance ();
  
  while (distancer <=.25) 
  {

    digitalWrite(red, HIGH);
    getDistance();           
    if(distanceread>.25)        
      digitalWrite(red, LOW);	
    break;         
  }