Webotricks
Published

How to make a cute WALL-E smart robot with Arduino

In this project, we will learn how to make a cute smart robot with Arduino.

BeginnerProtip1 hour633
How to make a cute WALL-E smart robot with Arduino

Things used in this project

Hardware components

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

Story

Read more

Code

Code

Arduino
//include the servo motor library
#include <Servo.h>
//Include the motor driver library
#include <AFMotor.h>

Servo servo;//Servo motor object

int sPoint = 100; // servo start point

//Define the sensor pins
#define Trig A1
#define Echo A0

//Set the speed of the motors
#define Speed 160

//Create objects for the motors
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);

void setup() {
  Serial.begin(9600);
  servo.attach(9);
  servo.write(sPoint);
  //Set the Trig pins as output pins
  pinMode(Trig, OUTPUT);
  //Set the Echo pins as input pins
  pinMode(Echo, INPUT);
  //Set the speed of the motors
  motor1.setSpeed(Speed);
  motor2.setSpeed(Speed);
  delay(1000);
  headMove();
  delay(2000);
  leftSide();
  rightSide();
  left();
  delay(1000);
  Stop();
}

void loop() {
  bool check = true;

  int sValue = ultrasonic();
  if (sValue <= 6) {
    check = false;
    forward();
    delay(300);
    Stop();
    headMove();
  } else if (sValue >= 8 && sValue <= 12) {
    check = false;
    backward();
  } else {
    Stop();
  }
}

//Get the sensor values
int ultrasonic() {
  //pulse output
  digitalWrite(Trig, LOW);
  delayMicroseconds(4);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);

  long t = pulseIn(Echo, HIGH);//Get the pulse
  int cm = t / 29 / 2; //Convert time to the distance
  return cm; // Return the values from the sensor
}
/*******************Motor functions**********************/

void headMove() {
  for (int a = 0; a <= 5; a++) {
    servo.write(110);
    delay(100);
    servo.write(90);
    delay(100);
  }
}

void leftSide() {
  for (int a = 100; a <= 140; a++) {
    servo.write(a);
    delay(25);
  }
  for (int b = 139; b >= 100; b--) {
    servo.write(b);
    delay(25);
  }
}

void rightSide() {
  for (int a = 100; a >= 40; a--) {
    servo.write(a);
    delay(25);
  }
  for (int b = 39; b <= 100; b++) {
    servo.write(b);
    delay(25);
  }
}

void forward() {
  motor1.run(FORWARD);
  motor2.run(FORWARD);
}
void backward() {
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
}
void left() {
  motor1.run(BACKWARD);
  motor2.run(FORWARD);
}
void right() {
  motor1.run(FORWARD);
  motor2.run(BACKWARD);
}
void Stop() {
  motor1.run(RELEASE);
  motor2.run(RELEASE);
}

Credits

Webotricks
28 projects • 9 followers
Contact

Comments

Please log in or sign up to comment.