How to Make (almost) Anything



Week 11: Interface and Application Programming

For the application programming assignment, I decided to connect my step response pressure sensor board to the Unity3D environment. I would like to make a shoe insert that will respond to pressure on the toe or the heel which would translate to controlling a character in the Unity environment. As I already had working Arduino code that was sending data out through the serial port, the first step in this process was to get Unity to read that data. This required using the SerialPort class, which is a part of the .NET framework. I could then create a serialPort object and call the ReadLine method. I found this tutorial to be quite useful. The data that was being sent from arduino was a value between 5000 and 15000 depending on a number of different factors such as the thickness of elastomeric material, and size of conductive pads. I also had trouble getting a consistent value even when trying to maintain common environmental variables. My solution was to add a block of code which would populate an array with a number of values upon program start, and then take an average of these values and set that average to be the new zero value. I could then use the difference between the baseline value and current value as a variable that would control elements in my Unity environment. I played around with a few basic actions such as moving and rotating game objects, so my next step is to apply this variable to the game character controller.

using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System;
using UnityEngine.UI;


public class COM : MonoBehaviour
{
    public bool debug = false;
    public string input;

    public static int Baud = 115200;
    public static string portName = "COM6";

    public int value;

    const int numReadings = 500;
    public int total = 0;                  // the running total
    public int average = 0;                // the average

    // Array to store readings to establish baseline
    private int[] readings = new int[numReadings];
    public int readIndex = 0;
    public int min = 0;
    public int diff;

    // UI text element for viewing sensor reading
    public Text Sensor_UIText;


    SerialPort myData = new SerialPort(portName, Baud);

    void Start()
    {
        openConnection();
        logDebug("initialized");
    }

    void Update()
    {
        readInput();

        // Establish baseline reading
        if (readIndex < numReadings)
        {
            if(value < average)
            {
                value = average;
            }
            readings[readIndex] = value;

            // add the reading to the total:
            total = total + readings[readIndex];

            readIndex = readIndex + 1;
        }
        else if (value < average)
        {
            value = average;
        }

        average = total / readIndex;
        min = average;
        diff = value - min;
        Debug.Log("Value: " + value.ToString());

        Sensor_UIText.text = "Value: " + diff.ToString();

    }

    void readInput()
    {
        try
        {
            input = myData.ReadLine();
            value = int.Parse(input);

        }
        catch (TimeoutException) { }
    }


    void openConnection()
    {
        if (myData != null)
        {
            if (myData.IsOpen)
            {
                myData.WriteLine("O1");
                myData.Close();
                logDebug("Allready open, closing");
            }
            else
            {
                myData.Open();
                myData.ReadTimeout = 5;
                logDebug("Connection open");
                //StartCoroutine(handshake(2));
            }
        }
        else
        {
            if (myData.IsOpen)
            {
                logDebug("Allready opn");
            }
            else
            {
                logDebug("Setup is NULL - check settings");
            }
        }
    }

    void OnApplicationQuit()
    {
        myData.WriteLine("O1");
        myData.Close();
        logDebug("Connection closed");
    }


    void logDebug(string debugMsg)
    {
        if (debug == true)
        {
            print(debugMsg);
        }
    }

}