ITTT - Distance Car Controller - by Dave Van Staden

by AurAmile in Circuits > Arduino

96 Views, 0 Favorites, 0 Comments

ITTT - Distance Car Controller - by Dave Van Staden

IMG_0145.jpg

Hello there!

Welcome to my HKU If This Then That project!

For this assignment I made a distance censor that allows you to control the speed of a car in a videogame, you can slow down by coming close and go faster by moving more away with your hand!

Here is an Video: https://www.youtube.com/watch?v=rgORKbOnsD4

Supplies

Materials:

·        1 x Arduino Uno R3

·        1 x Ultrasonig Sensor

·        4 x Led - LED5RLN

·        12 x Jumper Cable

·        4 x 220 Ohm Resistors

·        1 x Breadboard

Tools:

·        1 x Soldering Iron

·        1 x Soldering tin line

schets.png

When I started with this project it was very hard to come up with an idea. I first wanted to make a laser gun but that sounded far too advanced and after a lot of thinking, I came up with making a controller for a racing game. This controller would work with the distance you are from it that makes you go slower or faster.

I sucked at drawing so it didn’t look good, but the idea was there. AS I went on about thinking just the sensor wouldn’t be enough, so I decided to add lights. These lights would determine how fast you where going, the read meaning you are going very fast, the green determining you are going slow.

Coding Everything

int ledPingGreen1 = 2;
int ledPingGreen2 = 4;
int ledPingOrange = 7;
int ledPingRed = 8;

const int trigPing = 12;
const int echoPing = 13;
const int threshhold = 20;
const int threshholdGreen2 = 40;
const int threshholdOrange = 60;
const int threshholdRed = 80;
float duration;
float distance;


void setup() {
 // put your setup code here, to run once:
 Serial.begin(9600);

 pinMode(trigPing,OUTPUT);
 pinMode(echoPing,INPUT);

 pinMode(ledPingGreen1,OUTPUT);
 pinMode(ledPingGreen2,OUTPUT);
 pinMode(ledPingOrange,OUTPUT);
 pinMode(ledPingRed,OUTPUT);
}

void loop() {
 // put your main code here, to run repeatedly:
 digitalWrite(trigPing,HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPing,LOW);
 duration = pulseIn(echoPing,HIGH);
 distance = 0.017 * duration;

 if(distance < threshhold){
   digitalWrite(ledPingGreen1,HIGH);
 } else{
   digitalWrite(ledPingGreen1,LOW);
 }
 if(distance < threshholdGreen2){
   digitalWrite(ledPingGreen2,HIGH);
 } else{
   digitalWrite(ledPingGreen2,LOW);
 }
 if(distance < threshholdOrange){
   digitalWrite(ledPingOrange,HIGH);
 } else{
   digitalWrite(ledPingOrange,LOW);
 }
 if(distance < threshholdRed){
   digitalWrite(ledPingRed,HIGH);
 } else{
   digitalWrite(ledPingRed,LOW);
 }

 serialPrint();
}

void serialPrint()
{
 Serial.println(distance);
 delay(50);

}

The code above is used inside Arduino while the Code below is used to implement it in Unity.

You do need to implement Ardity: https://ardity.dwilches.com/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class myListener : MonoBehaviour
{
    public float distance;
    
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void OnMessageArrived(string msg)
    {
        string[] msgSplit = msg.Split(',');
        float dis = (float.Parse(msgSplit[0]));
        if (dis >= 1500)
        {
            distance = 1500;
        }else
        {
            distance = dis / 2;
        }
        //Debug.Log(dis);
    }

    void OnConnectionEvent(bool success)
    {
        Debug.Log(success?"DeviceConnected" : "DeviceDisconnected");
    }
}

Circuit Drawing

Circuit.png

AS you can see on the image everything should be connected to the Arduino, otherwise IT WONT WORK.

Soldering

IMG_0136.jpg
IMG_0138.jpg

This was my first time EVER soldering, so I didn’t expect it to look amazing, but it does the job, the hardest part was making sure that the tin on the sonar sensor doesn’t touch that of another pin, since they are made so close to each other. The soldering did take quite some time, especially for the LED’s since you need a minus line for the minus pin from the LED’s.

Making It Look Good!

IMG_0141.jpg
IMG_0144.jpg
IMG_0145.jpg

When the Soldering was done I moved on to make a pretty box for it, it should look like a speedometer kinda. I used paint and markers for it.

Making the Inside Practical

IMG_0143.jpg

This was one of the first things I tested before working on making the box pretty. Making SURE that the box fits everything nice and tidy! Making sure the cables are decently managed which can be hard in such a small box is also important.

What I’ve Learned

I really REALLY wasn’t looking forward to this project, I dislike working with my hands, and this entire project was about that. I really didn’t have any idea about what to do and kept procrastinating even into my second year which didn’t help at all… So stepped up and brainstormed together with a friend who helped me get this idea.

After I got the idea everything went very fast and before I knew it I already had it working inside Unity! The biggest challenge was the soldering, which was just very new to me. After that it was just a question of how artistic can I make the boxing. Im happy with my final product, it looks good and does exactly what I wanted it to do! I learned not only to work with an Arduino, but also how to approach a project like this, where I have no clue what to do, and don’t know where to start.

I hope to use this experience for future projects.