Go Down

Topic: Compare diffrent measurements. (Read 689 times) previous topic - next topic

Stealth0113

Dec 16, 2011, 10:56 am Last Edit: Dec 16, 2011, 10:59 am by Stealth0113 Reason: 1
I've got an HC-SR04 Ultrasonic range detector mounted on a servo. It rotate, scan and store 3 values (45, 90 and 135 degrees)

Now i want that it can compare the 3 values to choose the longest distance. I used an if..else loop to let is choose but is there any way that works better. Because now it compares 45 with 90 and 135 with 90 degrees. But if the first string (45>90) is true, it won't check (135>90).

Here's my sketch:
Code: [Select]

void loop()
{  
 myservo.write(45);
 delay(1000);
 int microsec45 = ultrasonic.timing();
 Serial.print("MS45: ");
 Serial.print(microsec45);
 
 myservo.write(90);
 delay(1000);
 int microsec90 = ultrasonic.timing();
 Serial.print("MS90: ");
 Serial.print(microsec90);

 myservo.write(135);
 delay(1000);
 int microsec135 = ultrasonic.timing();
 Serial.print("MS135: ");
 Serial.print(microsec135);

 for(pos = 135; pos>=45; pos-=1)     // goes from 180 degrees to 0 degrees
 {                                
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   delay(15);                       // waits 15ms for the servo to reach the position
 }
 
 if (microsec45 > microsec90)
   {
     digitalWrite(ledpin43, HIGH);   // set the LED on
     delay(1000);              // wait for a second
     digitalWrite(ledpin43, LOW);    // set the LED off
   }
  else if (microsec135 > microsec90)
   {
    digitalWrite(ledpin13, HIGH);   // set the LED on
    delay(1000);              // wait for a second
    digitalWrite(ledpin13, LOW);    // set the LED off
   }
  else
   {
    digitalWrite(ledpin13, HIGH);   // set the LED on
    digitalWrite(ledpin43, HIGH);   // set the LED on
    delay(1000);              // wait for a second
    digitalWrite(ledpin13, LOW);    // set the LED off
    digitalWrite(ledpin43, LOW);    // set the LED off
   }


I used the 2 led pins on my Uno32 in stead of dc motors, to make it move visible what choices it makes.
Watch my stealth-bot @ let's make robots

swiftless

Try changing
Code: [Select]
if (microsec45 > microsec90)
to
Code: [Select]
if (microsec45 > microsec90) && (microsec45 > microsec135)

Might be a better way to do it though, but can't come up one myself sorry.

magnethead794

I'm assuming you want to detect if vehicle needs to go left or right for obstacle avoidance?

easiest way I can come up with:

if 45<90 AND 90>135 (90 is greatest)

if 45>90 AND 90>135 (45 is greatest)

if 45<90 AND 90<135 (135 is greatest)

if 45>90 AND 90<135 (90 is least)
-> if 45<135 (135 is greatest/45 is least)


The reason for the last one:

Say there's an obstruction straight ahead. Do I go left or right? IE, is left blocked sooner, or is right blocked sooner?
KF5RVR

Stealth0113

Thanks MagnetHead794 for the fast response. I'm gonna try it later today and post my results!
Watch my stealth-bot @ let's make robots

ryschwith

It might work better to determine the highest as you're reading them. Then you can just compare the current reading to the highest reading thus far. Something like this:

Code: [Select]

void loop()
{
    byte deg[] = { 45, 90, 135 };
    int readings[ 3 ] = { 0 };
    int highReading = 0;
    int furthest;
    boolean light45 = false;
    boolean light135 = false;

    for( int i = 0; i < 3; i++ )
    {
        myservo.write( degs[ i ] );
        delay( 1000 );
        readings[ i ] = ultrasonic.timing();
        if( readings[ i ] > highReading )
        {
            highReading = readings[ i ];
            furthest = i;
        }
    }

    if( furthest < 2 ) // either 45 or 90
    {
        digitalWrite( pinFor45, HIGH );
    }
    if( furthest > 0 ) // either 90 or 135
    {
        digitalWrite( pinFor135, HIGH );
    }
    delay( 1000 );
    digitalWrite( pinFor45, LOW ); // don't need to test to turn off, since there's no harm in turning off one that's already off
    digitalWrite( pinFor135, LOW );
}


(Note: untested code)

Stealth0113

#5
Dec 17, 2011, 10:22 am Last Edit: Dec 17, 2011, 10:45 am by Stealth0113 Reason: 1
@ryschwith: i don't fully understand your code. The "for" loop is clear to me, but the "if" string looks like gibberish to me...

Can you explain to me why furthest < 2 stands for 45 or 90 degrees??

And why did you use the light45 = false and light135 = false? I don't see it back anywhere in the code
Watch my stealth-bot @ let's make robots

magnethead794


@ryschwith: i don't fully understand your code. The "for" loop is clear to me, but the "if" string looks like gibberish to me...

Can you explain to me why furthest < 2 stands for 45 or 90 degrees??

And why did you use the light45 = false and light135 = false? I don't see it back anywhere in the code


For loop iterates through an array of the 3 sensors

i is the sensor number- 0, 1, 2

It stores the highest reading, it's sensor number and value

if furthest < 2 means if furthest is 0 or 1

if furthest > 0 means if furthest is 1 or 2

0 is 45
1 is 90
2 is 135

if 45 or 90 are furthest, light the respective LED.

if 90 or 135 are furthest, light LED

one led is 45 degs, second led is 135 degrees, if neither lit, it's 90.
KF5RVR

ryschwith

Actually, both light for 90 (which I believe is what the OP had in the original code). Otherwise, though, spot on.


And why did you use the light45 = false and light135 = false? I don't see it back anywhere in the code


Whoops! Ignore those. They were going to be used before I came up with a better idea, and I missed deleting them.

Go Up