PROJECT Nº 4: Avoid obstacles

Learn how to create a program so that, when the Code&Drive ultrasonic sensor detects an object or plain surface 25 cm or less away, turns on the LEDs, moves backwards and turns to avoid it. Once it has avoided the object, it will keep moving forward in a straight line.

DIFFICULTY LEVEL: Intermediate.

DURATION OF THE EXERCISE:  40 min.

MATERIALS:

  • 1 Green LED
  • 1 Red LED
  • 1 Ultrasonic sensor
  • 2 DC motors with their wheels
  • 1 USB – Micro USB cable
  • Computer

The Code&Drive will have to be built according to the instructions manual.

What is an ultrasonic sensor?

An ultrasonic sensor is a device to measure the distance. It functions by sending a high frequency sound pulse, not audible to the human ear. This pulse bounces on near objects and it’s reflected towards the sensor, that has a microphone suitable for this frequency.

By measuring the time between pulses and knowing the speed of the sound, we can estimate the distance of the object, on whose surface has bounced the sound pulse.

code-and-drive-project-4

CONNECTIONS:

  1. Connect the LEDs to the digital pins “9” and “10”.
  2. Connect the ultrasonic sensor to the digital pins “12” and “13”.
  3. Connect the DC motors to the grey connector of the Build&Code 4in1 board.

PROGRAMMING CODE

You can do this project using the Arduino, Bitbloq and other visual programming software by blocks compatible. Below you will find the necessary code.

Arduino Code

  1. Download and install the Arduino IDE program. It is available for Windows, Mac OS and Linux.
  2. Open the Arduino program and copy the following program in it:
    int PinSpeedMA = 5, PinSpeedMB = 6; // DIGITAL PIN FOR THE SPEED OF THE MOTORS
    int PinTurnMA = 4, PinTurnMB = 7; // DIGITAL PIN FOR DIRECTION OF THE MOTORS
    int TrigPin = 13;  // ULTRASONIC SENSOR PINS
    int EchoPin = 12;  
    float SSound = 0.0343;  // SOUND SPEED IN cm/us
    long Lengh, Distance ;  // VARIABLES TO CALCULATE THE DISTANCE IN cm
    int PinLED1 = 9, PinLED2 = 10; // LED1 AND LED2 DIGITAL PINS
    
    void setup() {
      // put your setup code here, to run once:
      //CONFIGURATION OF THE DIGITAL PORTS
      pinMode(PinSpeedMA, OUTPUT);
      pinMode(PinSpeedMB, OUTPUT);
      pinMode(PinTurnMA, OUTPUT);
      pinMode(PinTurnMB, OUTPUT);
      pinMode(TrigPin, OUTPUT);  
      pinMode(EchoPin, INPUT);
      pinMode(PinLED1, OUTPUT);
      pinMode(PinLED2, OUTPUT); 
      // SPEED OF THE MOTORS  100 TO 255
      analogWrite(PinSpeedMA, 175);
      analogWrite(PinSpeedMB, 175);
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
      DistanceCM(); // CALL THE FUNCTION TO CALCULATE THE DISTANCE
      if (( Distance < 25) && ( Distance > 1)) // IF THE DISTANCE IS BETWEEN 1 AND 25CM
        {
          digitalWrite(PinLED1,HIGH);//LED1 Y LED2 = ON
          digitalWrite(PinLED2,HIGH);
          digitalWrite(PinTurnMA, HIGH);// CODE&DRIVE BACKWARD
          digitalWrite(PinTurnMB, LOW);
          delay(1000);
          digitalWrite(PinTurnMA, HIGH);// CODE&DRIVE ROTATION
          digitalWrite(PinTurnMB, HIGH);   
          delay(1000);  
        }
        else
        {
          digitalWrite(PinLED1,LOW);// LED1 Y LED2 = OFF
          digitalWrite(PinLED2,LOW);
          digitalWrite(PinTurnMA, LOW); // CODE&DRIVE FORWARD
          digitalWrite(PinTurnMB, HIGH);      
        }
    }
    
    void DistanceCM()// DISTANCE CALCULATION FUNCTION
    {
      // CALCULATE THE DISTANCE IN CM
      digitalWrite(TrigPin, LOW);        // VERIFY THAT THE TRIGGER IS DEACTIVATED
      delayMicroseconds(4);              // VERIFY THAT THE TRIGGER IS LOW
      digitalWrite(TrigPin, HIGH);       // ACTIVATE THE OUTPUT PULSE
      delayMicroseconds(14);             // WAIT 10µs. PULSE REMAINS ACTIVE DURING
      digitalWrite(TrigPin, LOW);        // STOP PULSE AND WAIT FOR ECHO
      Lengh = pulseIn(EchoPin, HIGH) ; // pulseIn MEASURES THE TIME THAT TAKES TO THE DECLARED PIN (echoPin) TO CHANGE FROM LOW TO HIGH STATUS (FROM 0 TO 1)
      Distance = SSound* Lengh / 2;  // CALCULATE DISTANCE
    

Code for the visual programming software by blocks compatible

  1. Download and install the program.
  2. Open the software and copy the following code. Use the following image as a guide:
  3. Configure and upload the code, following the indications on the Code&Drive First Steps guide.
  4. Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.

Bitbloq code 

  1. Download Bitbloq and install the Web2board app.
  2. Open the software and copy the following code.
    • Hardware
    • Software
  3. Configure and upload the code, following the indications on the Code&Drive First Steps guide.
  4. Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.

RESULT OF THE EXERCISE

The Code&Drive will go straight forward. When the ultrasonic sensor detects an object less than 25 cm away, the Code&Drive will turn its LEDs on, go back for 1 second and turn around for another second. Then, it will check that there are no obstacles, turn the LEDs off and continue to go straight forward.

1 0

0