nekazuko
Published

Selsa

Selsa is a cute girl, robotic car, whose intelligence is growing.

IntermediateWork in progress157
Selsa

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
L293D motor driver shield
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Perma-Proto Breadboard Half Size
Perma-Proto Breadboard Half Size
×1
Jumper wires (generic)
Jumper wires (generic)
×15
9V battery (generic)
9V battery (generic)
×1
Snap-on Connector, For 1 9-V Battery
Snap-on Connector, For 1 9-V Battery
×1
Slide Switch
Slide Switch
×1
DC Motor, 12 V
DC Motor, 12 V
×2
Wheels
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Circuit Diagram (Selsa Ver 2.0)

This is the circuit diagram showing the connection of ultrasonic sensor and motor driver shield.

Code

Source Code

Arduino
This is the source code of Selsa (Ver 2.0)
#include <AFMotor.h>

int trigPin = A2;
int echoPin = A0;
int delaytime = 200;
float duration;
float distance;

float soundVelocity = 343;

AF_DCMotor motor1(1, MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_64KHZ);



void setup() {
  AF_DCMotor begin();
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void forward()
{
  motor1.setSpeed(255);
  motor2.setSpeed(255);
  motor1.run(FORWARD);
  motor2.run(FORWARD);
}

void backward()
{
  motor1.setSpeed(255);
  motor1.run(BACKWARD);
  motor2.setSpeed(255);
  motor2.run(BACKWARD);
}

void move_stop()
{
  motor1.run(RELEASE);
  motor2.run(RELEASE);
}

void turn()
{
  motor1.setSpeed(255);
  motor1.run(BACKWARD);
  motor2.setSpeed(255);
  motor2.run(FORWARD);

  Serial.begin(9600);
}

void loop() {

  

  digitalWrite(trigPin, LOW);
  delayMicroseconds(10);

  digitalWrite(trigPin, HIGH);
  delay(delaytime);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  duration = duration / 1e+6; // to change the second from microsecond

  duration = duration/2; //to get the duration for one time

  distance = soundVelocity * duration;

  float final_distance = distance * 10; // to get the centimeter

  Serial.println(final_distance);


  
  if (final_distance < 5)
  {
    move_stop();
    delay(10);
    backward();
    delay(500);
    turn();
    delay(1000);
    final_distance = 0;
  }

  else
  {
    forward();
    delay(90);
  }
}

Credits

nekazuko
0 projects • 0 followers

Comments