Arduino Smart Robot Car-Part 2

 

Connections

Smart Car Motor Test

/*  Arduino DC Motor L298N Module H-bridge DC Motor Test
    By Chen The Design Maker  chenthedesignmaker.com
*/

//Define all the connections maps to the L298N
#define enA 10
#define in1 9
#de<span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce_SELRES_start"></span>fine in2 8

#define in3 7
#define in4 6
#define enB 5

class Motor{

    int enablePin;
    int directionPin1;
    int directionPin2;
    public:
    
    //Method to define the motor pins
    Motor(int ENPin,int dPin1,int dPin2){
      enablePin = ENPin;
      directionPin1 = dPin1;
      directionPin2 = dPin2;
    };     
    
    //Method to drive the motor 0~255 driving forward. -1~-255 driving backward
    Drive(int speed){
    if(speed>=0){
        digitalWrite(directionPin1, LOW);
        digitalWrite(directionPin2, HIGH);
      }
    else{
        digitalWrite(directionPin1, HIGH);
        digitalWrite(directionPin2, LOW);
        speed = - speed;
      } 
    analogWrite(enablePin, speed);  
    }
  };
  Motor leftMotor = Motor(enA, in1, in2);
  Motor rightMotor = Motor(enB, in3, in4);
  
void setup() {
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  // Set initial direction and speed
  digitalWrite(enA, LOW);
  digitalWrite(enB, LOW);
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}


void loop() {

leftMotor.Drive(200);
rightMotor.Drive(200);

delay(500);

leftMotor.Drive(0);
rightMotor.Drive(0);

delay(250);

leftMotor.Drive(150);
rightMotor.Drive(-150);

delay(500);

leftMotor.Drive(0);
rightMotor.Drive(0);

delay(250);
 

}

Servo Test:

/* Sweep
 by BARRAGAN <http://barraganstudio.com>
 This example code is in the public domain.

 modified 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

/**
 * HC-SR04 Demo
 * Demonstration of the HC-SR04 Ultrasonic Sensor
 * Date: August 3, 2016
 * 
 * Description:
 *  Connect the ultrasonic sensor to the Arduino as per the
 *  hardware connections below. Run the sketch and open a serial
 *  monitor. The distance read from the sensor will be displayed
 *  in centimeters and inches.
 * 
 * Hardware Connections:
 *  Arduino | HC-SR04 
 *  -------------------
 *    5V    |   VCC     
 *    A0     |   Trig     
 *    A1     |   Echo     
 *    GND   |   GND
 *  
 * License:
 *  Public Domain
 */

// Pins
const int TRIG_PIN = A0;
const int ECHO_PIN = A1;

// Anything over 400 cm (23200 us pulse) is "out of range"
const unsigned int MAX_DIST = 23200;

void setup() {

  // The Trigger pin will tell the sensor to range find
  pinMode(TRIG_PIN, OUTPUT);
  digitalWrite(TRIG_PIN, LOW);

  // We'll use the serial monitor to view the sensor output
  Serial.begin(9600);
}

void loop() {

  unsigned long t1;
  unsigned long t2;
  unsigned long pulse_width;
  float cm;
  float inches;

  // Hold the trigger pin high for at least 10 us
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // Wait for pulse on echo pin
  while ( digitalRead(ECHO_PIN) == 0 );

  // Measure how long the echo pin was held high (pulse width)
  // Note: the micros() counter will overflow after ~70 min
  t1 = micros();
  while ( digitalRead(ECHO_PIN) == 1);
  t2 = micros();
  pulse_width = t2 - t1;

  // Calculate distance in centimeters and inches. The constants
  // are found in the datasheet, and calculated from the assumed speed 
  //of sound in air at sea level (~340 m/s).
  cm = pulse_width / 58.0;
  inches = pulse_width / 148.0;

  // Print out results
  if ( pulse_width > MAX_DIST ) {
    Serial.println("Out of range");
  } else {
    Serial.print(cm);
    Serial.print(" cm \t");
    Serial.print(inches);
    Serial.println(" in");
  }
  
  // Wait at least 60ms before next measurement
  delay(60);
}

Parts list:

Smart Robot Car kit:

https://www.banggood.com/DIY-L298N-2WD-Ultrasonic-Smart-Tracking-Moteur-Robot-Car-Kit-For-Arduino-p-1155139.html?p=JH2514288496201309O3

M3 *10mm Standoffs:

https://www.banggood.com/50pcs-M3-10mm-Brass-Female-Threaded-Hex-Standoffs-Spacer-Nut-DIY-PCB-Parts-p-1007519.html?p=JH2514288496201309O3
Amazon: http://amzn.to/2Auwbzq

Extra servo for pan and tilt camera mount:

https://www.banggood.com/TowerPro-SG90-Mini-Gear-Micro-Servo-9g-For-RC-Airplane-Helicopter-p-1009914.html?p=JH2514288496201309O3
Amazon: http://amzn.to/2jO7BBE

Speed encoder kit(Will cover in later video):

https://www.banggood.com/HC-020K-Double-Speed-Measuring-Module-With-Speed-Encoder-Kit-p-970327.html?p=JH2514288496201309O3
Amazon: http://amzn.to/2kq7H6J

3 in 1 Jumper cable set:

https://www.banggood.com/3-IN-1-120pcs-10cm-Male-To-Female-Female-To-Female-Male-To-Male-Jumper-Cable-Dupont-Wire-For-Arduino-p-1054670.html?p=JH2514288496201309O3
Amazon: http://amzn.to/2D5QYuw

Let me know if you have any kit that need help with!

My Website: http://chenludesign.com
My Instagram:https://www.instagram.com/friedlc/
My Dribbble: https://dribbble.com/chenludesign
My Instructable: https://www.instructables.com/member/Friedlc/

DISCLAIMER: This video and description contains affiliate links, which means that if you click on one of the product links, I’ll receive a small commission. This help support the channel and allows us to continue to make videos like this. Thank you for the support!

DISCLAIMER: This post contains affiliate links, which means that if you click on one of the product links, I’ll receive a small commission. This help support the channel and allows us to continue to make videos like this. Thank you for the support!

Leave a Reply

Your email address will not be published. Required fields are marked *