background image

      if(pos>119)  sweepFlag = false;                       // assign the var
iable again 

  }else {                                        

      if(pos>=60 && pos<=120){     

        pos=pos-1; 

        myservo.write(pos); 

      } 

      if(pos<61)  sweepFlag = true; 

   } 

 

STEP 5: Debugging Robot

 

#include <Servo.h>  

#include <Metro.h> 

Metro measureDistance = Metro(50); 

Metro sweepServo = Metro(20); 

 

int speedPin_M1 = 5;     //M1 Speed Control 

int speedPin_M2 = 6;     //M2 Speed Control 

int directionPin_M1 = 4;     //M1 Direction Control 

int directionPin_M2 = 7;     //M1 Direction Control 

unsigned long actualDistance = 0; 

 

Servo myservo;  // create servo object to control a servo  

int pos = 60;  

int sweepFlag = 1; 

 

int URPWM = 3; // PWM Output 0

25000US

Every 50US represent 1cm 

int URTRIG= 10; // PWM trigger pin 

uint8_t EnPwmCmd[4]={0x44,0x02,0xbb,0x01};    // distance measure command 

  

void setup(){                                 // Serial initialization 

  myservo.attach(9);  

Summary of Contents for ROB0118

Page 1: ...the platform of the Turtle Robot based on ultrasonic sensor as distance measuring device and combined with servo STEP 1 Assemble Robot Refer to Instruction Manual http www dfrobot com cn image data RO...

Page 2: ...2 Finished the robot chassis Then following the connection diagram to wire the hardware Motor Connection Physical map...

Page 3: ...oid setup void loop carAdvance 100 100 delay 1000 carBack 100 100 delay 1000 carTurnLeft 250 250 delay 1000 carTurnRight 250 250 delay 1000 void carStop Motor Stop digitalWrite speedPin_M2 0 digitalWr...

Page 4: ...talWrite directionPin_M2 LOW void carTurnRight int leftSpeed int rightSpeed Turn Right analogWrite speedPin_M2 leftSpeed digitalWrite directionPin_M1 LOW analogWrite speedPin_M1 rightSpeed digitalWrit...

Page 5: ...rasonic Sensor Position Please see the Installation Manual http www dfrobot com cn images upload File 20141030183325g7lofm pdf 3 Fixed Servo Position STEP4 Debug Ultrasonic Sensor and Servo 1 Hardware...

Page 6: ...Ultrasonic Sensor and Servo Controll...

Page 7: ...ate servo object to control a servo int pos 60 int sweepFlag 1 int URPWM 3 PWM Output 0 25000US Every 50US represent 1cm int URTRIG 10 PWM trigger pin uint8_t EnPwmCmd 4 0x44 0x02 0xbb 0x01 distance m...

Page 8: ...TRIG triggering a se nsor reading digitalWrite URTRIG LOW digitalWrite URTRIG HIGH reading Pin PWM will output pulses unsigned long distance pulseIn URPWM LOW if distance 50000 the reading is invalid...

Page 9: ...M1 5 M1 Speed Control int speedPin_M2 6 M2 Speed Control int directionPin_M1 4 M1 Direction Control int directionPin_M2 7 M1 Direction Control unsigned long actualDistance 0 Servo myservo create servo...

Page 10: ...ntln actualDistance delay 100 if sweepServo check 1 servoSweep if actualDistance 30 myservo write 90 if pos 90 carBack 100 100 Serial println carBack delay 100 carTurnRight 150 150 Serial println carT...

Page 11: ...ial write EnPwmCmd i int MeasureDistance a low pull on pin COMP TRIG triggering a sensor r eading digitalWrite URTRIG LOW digitalWrite URTRIG HIGH reading Pin PWM will output pulses unsigned long dist...

Page 12: ...n_M2 leftSpeed digitalWrite directionPin_M1 LOW analogWrite speedPin_M1 rightSpeed digitalWrite directionPin_M2 LOW void carTurnRight int leftSpeed int rightSpeed Turn Right analogWrite speedPin_M2 le...

Page 13: ...o to po sition in variable pos if pos 119 sweepFlag false assign the var iable again else if pos 60 pos 120 pos pos 1 myservo write pos if pos 61 sweepFlag true Your own car was born https www dfrobot...

Reviews: