Go Down

Topic: autonomous 4wheeled robot (Read 1 time) previous topic - next topic

fauz

hi friends!
i am working on a project which is an autonomous 4wheeled robot,its function is to avoid obstacles for that i am using max botix lv ez4 ultrasonic sensor,i am using two dc motors,now for now my problem is that i am unable to code my ultrasonic sensor so it can control my dc motors i am using l293d,as after surfing on net i got some idea that how can i control my motors through ping sensor using echo and trigger pin but maxbotix lv ez4 is different as compared to ping sensor can any one help me that how can i code it?

depressed!

johnwasser

The Ping sensor measures a distance.  The maxbotix lv ez4 measures a distance.  Take the Ping example, remove the Ping code to measure distance, insert the maxbotix lv ez4 code to measure distance.  Done!
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

fauz

here is the code but its for ping sensor and servo motor what changes can i make so i can control dc motors via maxbotix sensor?
plz help
#include <Servo.h>
Servo myservo;
const int numOfReadings = 10;                   // number of readings to take/ items in the array
int readings[numOfReadings];                    // stores the distance readings in an array
int arrayIndex = 0;                             // arrayIndex of the current item in the array
int total = 0;                                  // stores the cumlative total
int averageDistance = 0;                        // stores the average value
int led = 13;
// setup pins and variables for SRF05 sonar device

int echoPin = 8;                               // SRF05 echo pin (digital 2)
int initPin = 7;                               // SRF05 trigger pin (digital 3)
unsigned long pulseTime = 0;                    // stores the pulse in Micro Seconds
unsigned long distance = 0;                     // variable for storing the distance (cm)

int motor1Pin1 = 3;                             // pin 2 on L293D
int motor1Pin2 = 4;                             // pin 7 on L293D                            // pin 1 on L293D
int motor2Pin1 = 5;                             // pin 10 on L293D
int motor2Pin2 = 6;                             // pin  15 on L293D                            // pin 9 on L293D
int speaker = 10;


void setup() {
  // set the motor pins as outputs:
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);

  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(0,INPUT);
  pinMode(1,INPUT);
 
  // set enablePins high so that motor can turn on:
 

  myservo.attach(9);
  myservo.write(90);
  delay(100);

  pinMode(initPin, OUTPUT);                     // set init pin 3 as output
  pinMode(echoPin, INPUT);                      // set echo pin 2 as input

  // create array loop to iterate over every item in the array

  for (int thisReading = 0; thisReading < numOfReadings; thisReading++) {
    readings[thisReading] = 0;
  }
}

void loop() {
  digitalWrite(initPin, HIGH);                  // send 10 microsecond pulse
  delayMicroseconds(10);                                // wait 10 microseconds before turning off
  digitalWrite(initPin, LOW);                   // stop sending the pulse
  pulseTime = pulseIn(echoPin, HIGH);           // Look for a return pulse, it should be high as the pulse goes low-high-low
  distance = pulseTime/58;                      // Distance = pulse time / 58 to convert to cm.
  total= total - readings[arrayIndex];          // subtract the last distance
  readings[arrayIndex] = distance;              // add distance reading to array
  total= total + readings[arrayIndex];          // add the reading to the total
  arrayIndex = arrayIndex + 1;                  // go to the next item in the array                                 

  // At the end of the array (10 items) then start again
  if (arrayIndex >= numOfReadings)  {
    arrayIndex = 0;
  }

  averageDistance = total / numOfReadings;      // calculate the average distance
  delay(10);

  // check the average distance and move accordingly

  if (averageDistance <= 10){
    // go backwards
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, HIGH);
    digitalWrite(motor2Pin2, LOW);
    digitalWrite(10,LOW);
    myservo.write(10);
    delay(100);   

  }

  if (averageDistance <= 25 && averageDistance > 10) {
    // turn
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
    myservo.write(150);
    delay(100);   
  }

   
   if (averageDistance > 25)   {
    // go forward
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
    digitalWrite(10,LOW);
    myservo.write(90);
    delay(100);   

  }

}

johnwasser


here is the code but its for ping sensor and servo motor what changes can i make so i can control dc motors via maxbotix sensor?


Now show example code for reading distance from the maxbotix sensor.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

fauz

here is the code for maxbotix sensor


here is the code but its for ping sensor and servo motor what changes can i make so i can control dc motors via maxbotix sensor?


Now show example code for reading distance from the maxbotix sensor.


const int pwPin = 7;
//variables needed to store values
long pulse, inches, cm;
void setup() {
  //This opens up a serial connection to shoot the results back to the PC console
  Serial.begin(9600);
}
void loop() {
  pinMode(pwPin, INPUT);
    //Used to read in the pulse that is being sent by the MaxSonar device.
  //Pulse Width representation with a scale factor of 147 uS per Inch.

  pulse = pulseIn(pwPin, HIGH);
  //147uS per inch
  inches = pulse/147;
  //change inches to centimetres
  cm = inches * 2.54;
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(500);
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy