Webotricks
Published

How to make a DIY Multi-functional robot car using Arduino

In this tutorial, we will learn how to make a multi-functional robot car using Arduino. Also, this robot car includes three functions.

BeginnerProtip2 hours75
How to make a DIY Multi-functional robot car using Arduino

Things used in this project

Hardware components

Arduino Uno
×1
Servo Motor (SG90)
×1
Ultrasonic Sensor
×1
Gear motor
×1
65mm Robot wheel
×1
Lithium Battery (3.7V)
×1
Lithium Battery Holder
×1
Jumper Wires
×1

Story

Read more

Code

Code

Arduino
/*Multi function robot with Arduino.
 * https://webotricks.com
 */
 
#include <AFMotor.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
 
Adafruit_PWMServoDriver srituhobby = Adafruit_PWMServoDriver();
AF_DCMotor motor1(2);
AF_DCMotor motor2(3);
 
#define Echo A0
#define Trig A1
#define S1 A2
#define S2 A3
#define Speed 150
 
#define servo1 0
#define servo2 1
#define servo3 2
#define servo4 3
 
void setup() {
  pinMode(S1, INPUT);
  pinMode(S2, INPUT);
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);
 
  webotricks.begin();
  webotricks.setPWMFreq(60);
  webotricks.setPWM(servo1, 0, 340);
  webotricks.setPWM(servo2, 0, 150);
  webotricks.setPWM(servo3, 0, 300);
  webotricks.setPWM(servo4, 0, 290);
 
  motor1.setSpeed(Speed);
  motor2.setSpeed(Speed);
 
 
}
void loop() {
  int distance = obstacle();
  Serial.println(distance);
  if (distance <= 9) {
    motor1.run(RELEASE);
    motor2.run(RELEASE);
    motor1.run(BACKWARD);
    motor2.run(BACKWARD);
    delay(100);
    motor1.run(RELEASE);
    motor2.run(RELEASE);
    delay(100);
    robotArm();
    delay(100);
    motor1.run(FORWARD);
    motor2.run(FORWARD);
    delay(100);
 
  } else {
    linefollower();
  }
}
void linefollower() {
  bool value1 = digitalRead(S1);
  bool value2 = digitalRead(S2);
 
  if (value1 == 0 && value2 == 0) {
    motor1.run(FORWARD);
    motor2.run(FORWARD);
  } else if (value1 == 1 && value2 == 1) {
    motor1.run(RELEASE);
    motor2.run(RELEASE);
  } else if (value1 == 1 && value2 == 0) {
    motor1.run(BACKWARD);
    motor2.run(FORWARD);
  } else if (value1 == 0 && value2 == 1) {
    motor1.run(FORWARD);
    motor2.run(BACKWARD);
  }
}
 
int obstacle() {
  digitalWrite(Trig, LOW);
  delayMicroseconds(4);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);
  long t = pulseIn(Echo, HIGH);
  int cm = t / 29 / 2;
  return cm;
}
 
void robotArm() {
 
  for (int S4value = 290; S4value <= 490; S4value++) {
    webotricks.setPWM(servo4, 0, S4value);
    delay(10);
  }
 
  for (int S3value = 300; S3value <= 450; S3value++) {
    webotricks.setPWM(servo3, 0, S3value);
    delay(10);
  }
 
  for (int S2value = 150; S2value <= 190; S2value++) {
    webotricks.setPWM(servo2, 0, S2value);
    delay(10);
  }
 
  for (int S4value = 490; S4value > 290; S4value--) {
    webotricks.setPWM(servo4, 0, S4value);
    delay(10);
  }
 
  for (int S3value = 450; S3value > 300; S3value--) {
    webotricks.setPWM(servo3, 0, S3value);
    delay(10);
  }
 
  for (int S2value = 190; S2value <= 320; S2value++) {
    webotricks.setPWM(servo2, 0, S2value);
    delay(10);
  }
 
  for (int S1value = 340; S1value >= 150; S1value--) {
    webotricks.setPWM(servo1, 0, S1value);
    delay(10);
  }
 
  for (int S3value = 300; S3value <= 410; S3value++) {
   webotricks.setPWM(servo3, 0, S3value);
    delay(10);
  }
 
  for (int S4value = 290; S4value <= 490; S4value++) {
   webotricks.setPWM(servo4, 0, S4value);
    delay(10);
  }
 
  for (int S4value = 490; S4value > 290; S4value--) {
    webotricks.setPWM(servo4, 0, S4value);
    delay(10);
  }
 
  for (int S3value = 410; S3value > 300; S3value--) {
    webotricks.setPWM(servo3, 0, S3value);
    delay(10);
  }
  for (int S2value = 320; S2value > 150; S2value--) {
    webotricks.setPWM(servo2, 0, S2value);
    delay(10);
  }
 
  for (int S1value = 150; S1value < 340; S1value++) {
    webotricks.setPWM(servo1, 0, S1value);
    delay(10);
  }
}

Credits

Webotricks
28 projects • 9 followers
Contact

Comments

Please log in or sign up to comment.