Oh oh ohhhhhh !!! Italian Christmas has arrived :) 

In the pic you can see my new toys !!

In the Nord-West corner a Ping sensor (HC-sr04), then a Gyro+acc+baro+compass (the red one), then an MPU6050-gyro+acc (more accurate) and an RTC module :D

borsaci06

10 years 5 months ago

Alessandro you are just like me... i  also love to receive small packets from China and always be happy unpacking them and inspect one by one with love and happiness... I am anxious about what you will do with these... I experimented a lot with HC-SR04 and MPU 6050 and i can be of help if needed for their interaction with Arduino... For ex... HC-SR04 has 4 pins there, seems that you have to hook up + and GND and 2 other for sending and receiving echo. but actually there is a simple library to use it with only 3 pins instead of 4. connect middle 2 pins as one and use it just like Ping sensor. send receive on single pin. below is a simple code i wrote for an autonomous ArduRover for object avoidance navigation... the HC-SR04 is on a servo and if something detected nearer than 20cm it starts sweep scan and tries to find a left or right opening in front to drive..

// ArduTank Project L293D implementation with HC-SR04 ultrasonic sensor

// 2 DC motors on Tamiya tank chasis
// if you need PWM, just use the PWM outputs on the Arduino
// and instead of digitalWrite, you should use the analogWrite command
// Dincer Hepguler 2013
 
#include <NewPing.h>
#include <Servo.h>
#define PING_PIN  8      // Arduino pin tied to combined echo and trigger pin on the ultrasonic sensor.
#define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). 
                         // Max sensor distance is rated at 400-500cm.
NewPing sonar(PING_PIN, PING_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
Servo myservo;           // create servo object to control sonar servo
// —————————————————————————  Motors
int motor_left[] = {6, 7};
int motor_right[] = {12,13};
int pos = 90;            //define initial servo position at center
int leftDistance, rightDistance;
int dist;
// ————————————————————————— Setup
void setup() {
Serial.begin(115200);
myservo.attach(11);        // Arduino pin to which sonar servo attached
 
// ---------------------- Setup motors
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
}
 
}
 
// ————————————————————————— Loop
void loop() {
  
  delay(50);                    // Wait 50ms between pings (about 20 pings/sec).
                                // 29ms should be the shortest delay between pings.
  dist = sonar.ping_cm();       //get ping dist in cm 
  Serial.print("Dist: ");
  Serial.print(dist);          // serial print ping dist in cm 
                               // (0 = outside set distance range, no ping echo)
  Serial.println("cm");
  
  if ((dist > 20) or (dist == 0)){
      drive_fwd();
      delay(100);
  }
  else {
    avoid();
  }  
}
 
// --------------------------------------- Obstacle avoidance routine
void avoid(){
  motor_stop();
  drive_back(); 
  delay(1000); 
  motor_stop(); 
  myservo.write(140); 
    delay(200);
    leftDistance = sonar.ping_cm();  //scan to the left and get left clearance
    delay(100);
    myservo.write(40);
    delay(200);
    rightDistance = sonar.ping_cm(); //scan to the right and get right clearance
    delay(100);
    myservo.write(90);               //return to center
    delay(200);
    
  if (leftDistance > rightDistance) //if left is less obstructed 
  {
    turn_left();
    delay(1000);
  Serial.print("LeftDist: ");
  Serial.print(leftDistance); 
  Serial.println("cm"); 
  
  }
  else { 
    turn_right();                   //if right is less obstructed
    delay(1000);
  Serial.print("RightDist: ");
  Serial.print(rightDistance); 
  Serial.println("cm");
  }
 
}  
 
 
// ————————————————————————— Drive
 
void motor_stop(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);
 
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
delay(50);
}
 
void drive_fwd(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
 
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
 
void drive_back(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
 
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}
 
void turn_left(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
 
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
 
void turn_right(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
 
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}
 
and the associated scan routine as a seperate scan.ino tab:
 
// ---------------------------------------sonar servo sweep routine 
void scan()
  for(pos = 90; pos < 160; pos += 10)  // goes from center to 140 degrees 
  {                                  // in steps of 10 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(50);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 160; pos > 90; pos -= 10)     // goes from 140 degrees to center
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(50);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 90; pos > 20; pos -= 10)  // goes from center to 40 degrees 
  {                                  // in steps of 10 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(50);                       // waits 15ms for the servo to reach the position 
  }
   for(pos = 20; pos < 90; pos += 10 )     // goes from 40 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(50);                       // waits 15ms for the servo to reach the position 
  }  
}

Hope this helps... i am sorry for the format of the post... couldnt paste the program block as seperate..