I need a way of doing OUTPUT from unity in an android app im doing or a customer
At the moment im struggling with getting the serial port working properly so that i can talk to an Arduino back and forth but its not very stable.
Im using the SPUP plugin and its working so-so but theres something that makes the serial connection stall i think.
I would really like to team up with someone who are also working with physical computing that would like to look at my project and se if they kan spot the problem for me ?
right now im testing:
neopixel light controlled by arduino from a unity android app trough USB OTG cable
and at the same time i controll a puck in a circle off “bells” with a hid joystick
Here is a screenshot of my setup in unity where you kan se the puck and the bells and my electronic setup
Here is my Arduino code
#include <Adafruit_NeoPixel.h>
const uint8_t PIN = 6;
const uint8_t NUMPIXELS = 50; // ALLE
const uint8_t FEEDBACK_LED = 13;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_RGB + NEO_KHZ800);
void setup() {
Serial.begin(9600);
strip.begin();
strip.show();
delay(10000);
}
void loop() {
while (Serial.available() > 0) {
int cmd = Serial.parseInt();
int n = Serial.parseInt();
int red = Serial.parseInt();
int green = Serial.parseInt();
int blue = Serial.parseInt();
//
if (Serial.read() == '\n') {
switch (cmd) {
case 0:
strip.setPixelColor(n, red, green, blue);
break;
case 1:
strip.setPixelColor(n, red, green, blue);
strip.show();
break;
case 2:
// strip.begin();
// SORT SORT
for (int n = 0; n < NUMPIXELS; n++)
{
strip.setPixelColor(n, 0, 0, 0);
}
strip.show();
break;
case 3:
strip.show();
break;
case 4 :
for (int i = 0; i < NUMPIXELS; i++)
{
strip.setPixelColor(i, red, green, blue);
}
strip.show();
break;
}
}
}
}
And here is my unity script
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using SerialPortUtility;
using System.Linq;
//using UnityEngine.InputSystem;
//using UnityEngine.InputSystem.Controls;
using AudioHelm;
public class InputController : MonoBehaviour
{
public Transform puck; // using jp.kshoji.midisystem;
public float radius;
public float offset;
public GameObject dot;
public float del;
private int[] harmoni = { 0, 3, 4, 2 }; //4
private int[] circle = { 0, 1, 2, 3, 4,5,6,7 }; //3
private int[] square = { 1, 3, 5, 7 }; // 2
private int[] triangle = { 2, 5, 7 }; // 1
private int[] learn = { 0, 1, 6, 3, 7 }; // 0
public bool useGyro;
public SerialPortUtility.SerialPortUtilityPro port;
public float distance;
public float tresh;
public HelmController synth;
public float noteLength = 0.1f;
public int[] notes;
// resolution / 1080 x 2280
// Start is called before the first frame update
private void Awake()
{
// port.BaudRate = 115200;
// port.OpenMethod = SerialPortUtility.SerialPortUtilityPro.OpenSystem.USB;
// port.Open();
/*/
jp.kshoji.unity.midi.MidiManager.Instance.RegisterEventHandleObject(gameObject);
jp.kshoji.unity.midi.MidiManager.Instance.InitializeMidi(() =>
{
jp.kshoji.unity.midi.MidiManager.Instance.StartScanBluetoothMidiDevices(0);
});
*/
}
void Start()
{
// <uses-feature android:name="android.hardware.usb.host" />
if (SystemInfo.supportsGyroscope)
{
//Gyro is available
Input.gyro.enabled = true;
Input.gyro.updateInterval = 0.01f; // 75Hz 0.0 - 0,00835 = 90Hz Input.gyro.UpdateInterval = 0.001;
// Input.gyro.updateInterval = 0.0167f; // 60Hz
}
port = this.GetComponent<SerialPortUtility.SerialPortUtilityPro>();
CreateDotsAroundPoint(8, Vector3.zero, radius); // num around radius
Screen.sleepTimeout = SleepTimeout.NeverSleep; //
}
// Update is called once per frame
void Update()
{
float x;
float y;
//indput x y er en værdi fra -0.5 til 0.5
if (Application.platform == RuntimePlatform.Android)
{
if (useGyro)
{
x = Input.gyro.attitude.x;
y = Input.gyro.attitude.y;
}
else
{
x = Input.GetAxis("Vertical");
y = Input.GetAxis("Horizontal");
}
}
else
{
x = Input.GetAxis("Vertical");
y = Input.GetAxis("Horizontal");
}
float radians = Mathf.Atan2(x, y);
float angle = radians * Mathf.Rad2Deg;
Vector2 cirklePos = new Vector2();
cirklePos.x = x * Mathf.Sqrt(1 - y * y / 2);
cirklePos.y = y * Mathf.Sqrt(1 - x * x / 2);
puck.position = cirklePos * offset;
}
public void SerialCom(int msg)
{
/* var deviceIds = jp.kshoji.unity.midi.MidiManager.Instance.DeviceIdSet.ToArray();
if (deviceIds.Length != 0)
{
//feedback.text = deviceIds[0];
// jp.kshoji.unity.midi.MidiManager.Instance.SendMidiNoteOn(deviceIds[0], 0, (int)0, (int)msg, (int)127);
}
else
{
// feedback.text = "no midi device!";
}*/
port.WriteCR("4 0 255 255 255");
synth.NoteOn(notes[msg], 1.0f, noteLength);
}
public void CreateDotsAroundPoint(int num, Vector3 point, float r)
{
for (int i = 0; i < num; i++)
{
/* Distance around the circle */
var radians = 2 * Mathf.PI / num * (8 - i);
/* Get the vector direction */
var vertical = Mathf.Sin(radians);
var horizontal = Mathf.Cos(radians);
var spawnDir = new Vector3(horizontal, vertical, 0);
/* Get the spawn position */
var spawnPos = point + spawnDir * r; // Radius is just the distance away from the point
/* Now spawn */
GameObject dotdot = Instantiate(dot, spawnPos, Quaternion.identity) as GameObject;
dotdot.SetActive(true);
DOT dScript = dotdot.GetComponent<DOT>();
dScript.id = i;
dScript.mother = gameObject.GetComponent<InputController>();
//
// not used in this afsnit
float dotradians = Mathf.Atan2(spawnPos.x, spawnPos.y);
float dotangle = dotradians * Mathf.Rad2Deg;
SpriteRenderer sp = dotdot.GetComponent<SpriteRenderer>();
// sp.material.color = Colors[i];
}
dot.SetActive(false);
}
}