Making Smart robot

I’ve noticed that you tend

I’ve noticed that you tend to post pictures when describing parts. Pictures are very useful, but links to the specifications, a datasheet, or where you bought the part from are generally more helpful.

**Changes **

Well i had changes the structure of the RC car and practically using the minature one for beigining .

I want to take the reading of my ultrasonic.

i want to take the average reading of 10-15 counts at once , how do i do it in my below code.

at moment reading for my Rultra i am not using, also i didnt setup my another motor D1,

 

#include <Ultrasonic.h>

 

  #define TRIGGER_PIN  24

  #define ECHO_PIN     25

  #define TRIGGER  22

  #define ECHO     23

 

 

  Ultrasonic leftultrasonic(TRIGGER_PIN, ECHO_PIN);

  Ultrasonic rightultrasonic(TRIGGER, ECHO);

 

  int D2 = 42;

  int P2 = 44;

  int D1 = 38;

  int P1 = 40;

 

 

  void setup()

    {

    pinMode(D2,OUTPUT);

    pinMode(P2,OUTPUT);

    pinMode(D1,OUTPUT);

    pinMode(P1,OUTPUT);

    Serial.begin(9600);

    }

 

  void loop()

    {

 

 

    // left ultrasonic

    float Lultra;

    long Lmicrosec = leftultrasonic.timing();

    Lultra = leftultrasonic.convert(Lmicrosec, Ultrasonic::CM);

    Serial.print(" LCM: “);

    Serial.print(Lultra);

    // right ultrasonic

    float Rultra;

    long Rmicrosec = rightultrasonic.timing();

    Rultra = rightultrasonic.convert(Rmicrosec, Ultrasonic::CM);

    Serial.print(” RCM: ");

    Serial.println(Rultra);

    delay(100); 

 

   if (Lultra <=40 or Lultra >=20)

         {

           int val = ((Lultra*3.875)+100);

           digitalWrite(D2 , HIGH);analogWrite(P2, val);

         }

    }

average…

Take a given number of readings and add them to a total as you go. When you are done, divide by the total number of readings.

A Rolling average Function

I was just following some comment on Shout box and thought this might be useful:

void RollDistance()                                 // The Rolling Averge Calculator
{
  Total = Total - IRAverage[ThisRead];             // Minus the oldest read
  IRAverage[ThisRead] = analogRead(IRPin);  // Replaces the oldest Read
  Total = Total + IRAverage[ThisRead];           // Adds the New Read to the total
  ThisRead ++;                                           // Increment the Reading number
 
  if (ThisRead >= 5)                                     // Loops the Reading Number
  {
    ThisRead = 0;
  }
  float volts = (Total / 5)*0.0048828125;  // Converts it back into a voltage, volts (5) / Analog steps (1024)
  Distance = 65 * pow(volts, -1.1);       // Volts to the power of -1.1, times 65 gives a rough distance in cm
}

I use it with an Sharp IR, but it should be similar, this way it just add’s the lastest reading, saves timing looping 5 times before deciding what to do.

Also, If you use this, you need to Prime it: Call this function 5 times(or how ever many readings your averaging) from the setup loop  to make the average have data (cheers birdmun for reminding me to add this)

**overlap Conditioning **

If i am using the feed from ultrasonic “Lultra” to detect the distance of an object and if i write the code as below. Would it over lap the conditioning ?

if (Lultra <=40){fast speed}

if (Lultra <=20){stop}

would the <=40 line would overlap the speed in <=20 when the sensor read the distance 10

1) how do i setup the if command that the condition remain in between range: here the range is 40 to 20 & another is 20 to 0  fast and stop speed respectively.

As you’ve written it, both

As you’ve written it, both the {fast speed} and {stop} sections will be executed when Lultra is <= 20 (Meaning that it, depending on implementation, will likely not move, but it’s best to only execute one).

The simplest change would be to first check if Lultra is <= 20, and if not, check if it is <= 40. Thus:

if (Lultra <= 20
  {stop}
else
{
  if (Lultra <= 40)
    {fast speed}
}

Although it’s not in the question you asked, I notice this means that with an Lultra value > 40, neither the stop nor fast speed sections will be executed. Is this intentional?

Thanks for Help!

Hi Janvar, Thanks for the code, i have another related question.

if i am reading from two ultrasonic which is next to each other in certain angle which act like an imaginary cone shape path and hence any object come in between the path would be avoided.

this is an example: 

i have two Lultrasonic named Lultra & Rultra, D2:P2 & D1:P1 are the command for the motor conected to wheels.

if (Lultra >= 41 and Rultra >= 41)

         { 

           digitalWrite(D2 , HIGH); analogWrite(P2, 255);

           digitalWrite(D1 , HIGH); analogWrite(P1, 255);

         }

My question is would this command only follwed if the two condition matched ( like the car with sensor move to a flat wall) at given moment, would it also be follwed if one is >=40 and other is not.

I think you need to do

I think you need to do this:

if (Lultra > 40 && Rultra > 40)

         { 

           digitalWrite(D2 , HIGH); analogWrite(P2, 255);

           digitalWrite(D1 , HIGH); analogWrite(P1, 255);

         } 
 Then if Both values are greater than 40 (or you could do >=41 but by habit i did > 40) it will run the digtialWrite code. If one is above 40 but the other isnt then it wont run it. ( ‘&&’ means and, whereas ‘&’ means bitwise adding, also i don’t think ‘and’ is an operator in arduino but i could be wrong…)

Refernce Page for you:

http://arduino.cc/en/Reference/Boolean  

Yes, both operands must

Yes, both operands must evaluate to true for the conditional section to be executed. As The Benjaneer says, you’ll want ‘&&’ instead of ‘and’.

If you wanted it to execute if either of the operands are true (i.e. if Lultra or Rultra or both at once are >= 41), the symbol for logical OR is ‘||’.