Introduction: A Cute Kids Toy That Speaks With Arduino and Unity :)

About: Hi eveyone, my name is Raed, I'm a computer science student from Jordan, I'm passionate about learning and developing mobile games in Unity3D. I love to explore new ways and methods such as sensors as inputs f…

please watch the video for demonstration.

this project is purely out of boredom, I was experimenting with a flex sensor when the idea came to me, originally it was meant to use a flex sensor but after a second thought the same results can be achieve easily and more efficiently using an LDR.

the main idea here is that we will constantly be monitoring the LDR values and check if it exceeds a certain limit which indicates the toy has closed it's mouth in which case a random sound is played through unity I think this can provide a couple of laughs and giggles from your kids or little sibilings , have fun :)

Step 1: Lets Collect Our Components

1- a hand puppet

2- LDR

3- 10k ohm resistor

4- a little something to separate the legs of the LDR inside the puppet to prevent shot circut, I used Lego

5- a bunch of Audio sounds of your choice that you want your puppet to say. a great place to look is freesound.org

*I don't understand Japanese, but I chose it as it does sound as if it would come out of a toy :)

Step 2: Lets Build Our Toy

1- insert the two legs of the LDR inside the puppet's mouth, make sure they are reacheable from the inside as we need to connect them to wires

2- extend your wires that will be connecting your arduino and LDR 4 male-female wires

3- place your seperator on the LDR legs from inside

4- attach your wires to the legs and tape them so they won't fall

Step 3: Lets Build Our Circut

please see the picture included

Step 4: Arduino Code

in arduino we will constantly be monitering the LDR value at a loop delay of(100) and check if it exceeds a certain limit which indicates the toy has closed it's mouth then send value "1" to Unity

void setup() {
// put your setup code here, to run once: Serial.begin(9600); }
void loop() {
  // put your main code here, to run repeatedly:
  int data = analogRead(A0);
  if(data>950){
      Serial.write(1);
      
    }
  
  delay(100);
}

Step 5: Unity Code

please make sure that your unity projects allows serial Communication by
edit>project settings>player> scroll down to optimization and change api compatibility to .NET 2.0

1-import your sound files into unity (drag and drop)

2-create an AudioSource in your scene

3-Create an empty GameObject you can call it manager, and attach script to

4- add soundFiles to our publicly defined Array named clips(drag n drop)

the main Idea is to have an array of SoundClip to hold our sound files, and constantly checking readings from arduino if at any given time the value is = "1" then randomly pick a sound file using Random.range and play it

using UnityEngine;
using System.Collections; using System.IO.Ports; public class Audio : MonoBehaviour { public AudioClip[] clips;
    public AudioSource player;
    private SerialPort port = new SerialPort(@"\\.\" + "COM11", 9600);
    // Use this for initialization
    void Start () {
        port.Open();
        port.ReadTimeout = 25;
    }
	
	// Update is called once per frame
	void Update () {
        if (port.IsOpen)
        {
            try
            {
                int value = port.ReadByte();
                Debug.Log(value);
                if (value == 1) {
                    int random = Random.Range(0, clips.Length);
                    if (!player.isPlaying)
                    {
                        player.clip = clips[random];
                        player.Play();
                    }
                }
            }
            catch (System.Exception)
            {
            }
        }
    }
}