PROJECT Nº 6: Emergency vehicle

Learn how to create a program so that the Code&Drive simulates an emergency vehicle. For example: a police car, an ambulance or a firemen truck.

DIFFICULTY LEVEL: Intermediate.

DURATION OF THE EXERCISE:  60 min.

MATERIALS:

  • 6 AA batteries
  • 1 USB – Micro USB cable
  • Computer

CONNECTIONS:

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

PROGRAMMING CODE

You can do this project using the Arduino and a 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 MdirectionA = 4, velocityA = 5, MdirectionB = 7, velocityB = 6; // MOTOR A AND MOTOR B SPEED AND DIRECTION
    
    int trigPin = 13;  // ULTRASONIC SENSOR PINS 
    
    int echoPin = 12;  
    
    float SSound = 0.0343;  // SOUND SPEED IN cm/us
    
    long length, distance ;  // VARIABLES TO CALCULATE DISTANCE IN cm
    int LEDG = 9; // GREEN LED PIN
    int LEDB = 10;  // BLUE LED PIN
    
    void setup() {
    
      //BUILD&CODE 4IN1 PORTS CONFIGURATION
    
      pinMode ( MdirectionA, OUTPUT);
      pinMode ( velocityA, OUTPUT);
      pinMode ( MdirectionB, OUTPUT);
      pinMode ( velocityB, OUTPUT);
      pinMode(trigPin, OUTPUT);  
      pinMode(echoPin, INPUT); 
      pinMode(LEDG , OUTPUT);     
      pinMode(LEDB , OUTPUT); 
    
    }
    
    void loop() {
    
    
        // CALCULATE DISTANCE
    
      digitalWrite(trigPin, LOW);        // VERIFY THAT THE TRIGGER IS DEACTIVATED
    
    
      delayMicroseconds(4);              // VERIFY THAT THE TRIGGER IS IN LOW
    
      digitalWrite(trigPin, HIGH);       // ACTIVATE THE PULSE
    
     
      delayMicroseconds(14);             // WAIT 10µs. THE PULSE REMAINS ACTIVE THIS TIME.
    
    
    
      digitalWrite(trigPin, LOW);        // STOP THE PULSE AND WAIT FOR ECHO
    
     length = pulseIn(echoPin, HIGH) ; //pulseIn MEASURE THE TIME IT TAKES THE DECLARED PIN (echoPin) TO CHANGE FROM LOW TO HIGH STATUS (FROM 0 TO 1)
    
      distance = SSound* length / 2; // CALCULATE DISTANCE
    
      if (( distance < 25) && ( distance > 1)) // IF THE MEASURED DISTANCE IS BETWEEN 15 AND 1 CM
    
    
     
      {
        // BACK
        //MOTOR B
        analogWrite (velocityB, 150);   // MOTOR B SPEED, FROM 0 (STOP) TO 255 (MAXIMUM SPEED)
    
        digitalWrite ( MdirectionB,HIGH); // MOTOR B ROTATION, LOW (FORWARD), HIGH (BACKWARD)
    
        //MOTOR A
        analogWrite (velocityA, 150);   // MOTOR A SPEED, FROM 0 (STOP)TO 255 (MAXIMUM SPEED)
        digitalWrite ( MdirectionA,LOW); // MOTOR A DIRECTION ROTATION, HIGH (FORWARD) , LOW (BACKWARD)
        delay (1000);      
    
        // TURN RIGHT
        
    //MOTOR B
        analogWrite (velocityB, 150);   // MOTOR B SPEED, FROM 0 (STOP) TO 255 (MAXIMUM SPEED)
        digitalWrite ( MdirectionB,LOW); // MOTOR B ROTATION, LOW (FORWARD), HIGH (BACKWARD)
    
        //MOTOR A
        analogWrite (velocityA, 0);   // MOTOR A SPEED, FROM 0 (STOP) TO 255 (MAXIMUM SPEED)
        digitalWrite ( MdirectionA,HIGH); // MOTOR A DIRECTION ROTATION, HIGH (FORWARD) , LOW (BACKWARD)
        delay (1000);       
      }
      else if ( distance > 25) // IF DISTANCE IS HIGHER THAN 25 cm
      {
        //FORWARD
        //MOTOR B
        analogWrite (velocityB, 150);   // MOTOR B SPEED, FROM 0 (STOP) TO 255 (MAXIMUM SPEED)
        digitalWrite ( MdirectionB,LOW); // MOTOR B ROTATION, LOW (FORWARD), HIGH (BACKWARD)
    
        //MOTOR A
        analogWrite (velocityA, 150);   // MOTOR A SPEED, FROM 0 (STOP) TO 255 (MAXIMUM SPEED)
        digitalWrite ( MdirectionA,HIGH); // MOTOR A DIRECTION ROTATION, HIGH (FORWARD) , LOW (BACKWARD)
    
      }
      // ACTIVATE THE LIGHTS AND THE EMERGENCY SOUND
    
     Emergency();
    
    }
    void Emergency ()
    {
        digitalWrite(LEDG, HIGH);// GREEN LIGHT ON
        digitalWrite(LEDB, LOW);// BLUE LIGHT OFF
        tone(8, 494, 500);// BUZZER
        delay(400); // 400ms PAUSE
        digitalWrite(LEDG, LOW);// GREEN LIGHT OFF
        digitalWrite(LEDB, HIGH);// BLUE LIGHT ON
        tone(8, 440, 500);// BUZZER
        delay(400);// 400ms PAUSE
    }
    

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 move forward with its LEDs and buzzer on, simulating the alarm of an emergency vehicle. When the ultrasound sensor detects an object less than 25 cm away, it will move backward, turn and move forward in different direction.

You can combine the code of this project with project 4 (Avoid obstacles) and 5 (Line follower).

You can personalize the Code&Drive with the van chasis template. Use this template to make your own designs, and turn the Code&Drive into an ambulance, a police car or a firemen truck.

1 0

0