I was trying to learn how to do some new tricks on my skateboard when I ran into a trashcan. I thought about how this problem had occurred and what I could do to remedy this situation. Taking inspiration from Tesla autopilot I designed a skateboard that when stop whenever it sensed an obstacle in front of this. I used an ultrasonic distance sensor to get the readings from an arduino uno board.
I first tried to use a servo that would just stop the wheels when they were already in motion. I attached a piece of cardboard to the servo that would hopefully stop the wheels from turning.
This didn't work very well, so instead I attached a dc motor to each of the wheels. When the was an obstacle in front of the skateboard, the ultrasonic sensor would get the reading and the tell the wheels to stop moving.
I then using unity, created an app that would get the distance readings from the arduino and display how far away objects were, along with when to stop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
using System.Threading;
using System;
using System.Security.Cryptography;
using TMPro;
using UnityEngine.UI;
using System.Linq.Expressions;
public class distanceReading : MonoBehaviour
{
SerialPort sp = new SerialPort("COM6", 9600);
public string message;
public TextMeshProUGUI distanceText;
public TextMeshProUGUI text;
// Start is called before the first frame update
void Start()
{
OpenConnection();
}
// Update is called once per frame
void Update()
{
message = sp.ReadLine();
print(message);
distanceText.text = "Distance: " + message;
if (float.Parse(message) < 20)
{
text.text = "Stop";
text.color = Color.red;
}
else
{
text.text = "Go";
text.color = Color.green;
}
}
public void OpenConnection()
{
try
{
if (sp != null)
{
if (sp.IsOpen)
{
sp.Close();
print("closing port");
}
else
{
sp.Open();
sp.ReadTimeout = 26;
print("Port open");
}
}
else
{
if (sp.IsOpen)
{
print("Pory is open");
}
else
{
print("Port == null");
}
}
}catch (TimeoutException)
{
throw;
}
}
}
#include <AFMotor.h>
#include <Servo.h>
const int trigPin = A5;
const int echoPin = A4;
float duration, distance;
Servo myservo;
int pos = 0;
AF_DCMotor motor(2);
AF_DCMotor motor2(1);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
motor.setSpeed(500);
motor2.setSpeed(500);
motor2.setSpeed(500);
motor3.setSpeed(500);
motor4.setSpeed(500);
myservo.attach(10);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Turn on motor
motor.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(BACKWARD);
duration = pulseIn(echoPin, HIGH);
distance = (duration*.0343)/2;
//Serial.print("Distance: ");
Serial.println(distance);
delay(100);
if (distance < 20){
motor.run(BRAKE);
motor2.run(BRAKE);
motor3.run(BRAKE);
motor4.run(BRAKE);
Serial.println("STOP");// waits 15ms for the servo to reach the position
}
}