Magicbit
Published

Toy Car using Magicbot & Magicbit(esp-32)

In this project, we will make a toy car. Whenever someone or something gets too close to it, it will move away.

IntermediateProtip302
Toy Car using Magicbot & Magicbit(esp-32)

Things used in this project

Hardware components

magicbit
×1
magicbot
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Rechargeable Battery, 3.7 V
Rechargeable Battery, 3.7 V
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Code

Arduino
#include <ESP32Servo.h>
#include <NewPing.h>
#define T_F    26  // Trigger pin
#define E_F    26  // Echo pin

#define T_B    21 
#define E_B    21

#define T_L    5
#define E_L    5

#define T_R    32
#define E_R    32


#define MAX_DISTANCE 200


NewPing sonar_F(T_F, E_F, MAX_DISTANCE);
NewPing sonar_B(T_B, E_B, MAX_DISTANCE);
NewPing sonar_L(T_L, E_L, MAX_DISTANCE);
NewPing sonar_R(T_R, E_R, MAX_DISTANCE);

int limit = 20; // Minimum distance that the car starts to move.

int M1A = 17; //motor drive input pins
int M1B = 16;
int M2A = 27;
int M2B = 18;
int F;
int B;
int L;
int R;

void setup() {
  // put your setup code here, to run once:
  //Motor inputs
  pinMode(M1A, OUTPUT); 
  pinMode(M1B, OUTPUT);
  pinMode(M2A, OUTPUT);
  pinMode(M2B, OUTPUT);

  Serial.begin(115200);
  
}

void loop() {
  // Get data from sonars
  F = sonar_F.ping_cm();
  delay(10);
  B = sonar_B.ping_cm();
  delay(10);
  L = sonar_L.ping_cm();
  delay(10);
  R = sonar_R.ping_cm();
  delay(10);
  
/*
  Serial.print(F);
  Serial.print(" ");
  Serial.print(B);
  Serial.print(" ");
  Serial.print(L);
  Serial.print(" ");
  Serial.print(R);
  Serial.println(" ");
  */
    
  // Method
  if (F<limit) {
    reverse(Speed(F));
  }
  
   if (B<limit) {
    forward(Speed(B));
  }
   if (L<limit) {
    turn_L(Speed(L));
  }
   if (R<limit) {
    turn_R(Speed(R));
  }

    if ((F>limit)||(B>limit)||(L>limit)||(R>limit)) {
    analogWrite(M1A, 0);
    analogWrite(M1B, 0);
    analogWrite(M2A, 0);
    analogWrite(M2B, 0);
    delay(200);
  }
}


void forward(int x) {  //x is the speed
    analogWrite(M1A, x);
    analogWrite(M1B, 0);
    analogWrite(M2A, x);
    analogWrite(M2B, 0);
    delay(200);
}

void reverse(int x) {  //x is the speed
    analogWrite(M1A, 0);
    analogWrite(M1B, x);
    analogWrite(M2A, 0);
    analogWrite(M2B, x);
    delay(200);
}

void turn_L(int x) {  //x is the speed
    analogWrite(M1A, 0);
    analogWrite(M1B, x);
    analogWrite(M2A, x);
    analogWrite(M2B, 0);
    delay(200);
}

void turn_R(int x) {  //x is the speed
    analogWrite(M1A, x);
    analogWrite(M1B, 0);
    analogWrite(M2A, 0);
    analogWrite(M2B, x);
    delay(200);
}

int Speed(int distance) {
  return map(distance,40,10,0,255);
}

Credits

Magicbit
57 projects • 35 followers
Magicbit is an integrated development platform based on ESP32 for learning, prototyping, coding, electronics, robotics, IoT and more.
Contact

Comments

Please log in or sign up to comment.