Last few inputs are unresponsive

Issue #22 resolved
Former user created an issue

Hello, so I have an Arduino Mega on Uduino 2.1.8 that has 7 potentiometers and 6 buttons. I have tested each input individually, and they all work, however when I try to get an input from all of them at once, the last 2 or 3 inputs to be called in my script (whether they're buttons or pots) will be unresponsive. there is no error or warning thrown in Unity to acknowledge this. I have tried fiddling with the serial settings in uduino and it hasn't gotten me any closer to hearing them all at once.

Here is my code for the script for all my inputs and I have attached an image of the inspector so you can see:

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


public class ArduinoReader : MonoBehaviour
{

    public float[] potValue;
    public AnalogPin[] pinsPots;

    public int[] pinsButtons;

    public int[] buttonValue;



    // Use this for initialization
    void Start()
    {

        for (int i = 0; i < pinsPots.Length; i++)
        {
            UduinoManager.Instance.pinMode(pinsPots[i], PinMode.Input);
        }


        for (int i = 0; i < pinsButtons.Length; i++)
        {
            UduinoManager.Instance.pinMode(pinsButtons[i], PinMode.Input_pullup);
        }


    }


    void Update()
    {


        for (int i = 0; i < potValue.Length; i++)
        {
            potValue[i] = UduinoManager.Instance.analogRead(pinsPots[i]);
        }


        for (int i = 0; i < buttonValue.Length; i++)
        {
            buttonValue[i] = UduinoManager.Instance.digitalRead(pinsButtons[i]);
        }






    }
}

Comments (5)

  1. Marc Teys repo owner

    Hi,

    I'll look at that. I think I know were this issue comes from, however, I don't know yet how to fix it. I'll definitely address this issue. There are few possibilities to fix that:

    1. There is a queue system in Uduino, with a queue length of 10 elements. This variable is not exposed (I will expose it in the next version). To increase it, Open UduinoDevice.cs and increase maxQueueLength to 20 .
    2. When you request a lot of readings ( like your code, 12 times/update), the reading rate could be slower than a Unity frame (120FPS in the editor is about 1 frame every 8ms). In this case, the last readings will be skipped
    3. Maybe try to reduce the thread (Advanced > Serial setting > thread frequency , reduce it to 3 or 5)

    I acutally never tried with that maybe buttons reading. It should work just fine. You might run into two issues:

    • When reading a lot a buttons there might be a small delay (5ms)
    • The order of the buttons is not the good one

    Uduino has a specific features called 'Bundles' (documentation). It's a parameter (string) that you can add at the end of your Uduino command. By doing that, rather than sending right avay the commant to the arduino board, it will wait until you call a function to send the bundle.

    Concretely, it increases the performance and reduce the lost packets : Rather than sending 12 times the command 'read pin X',' read pin Y' it will send once the command ' read pin X & Y & Z & ....'

    Examples can be find in Examples > Basic > AnalogRead and Examples > Advanced > Bundle. For your code, it will be :

    //...
            for (int i = 0; i < potValue.Length; i++)
            {
                potValue[i] = UduinoManager.Instance.analogRead(pinsPots[i], "ReadPot");
            }
            UduinoManager.Instance.SendBundle("ReadPot");
    
    
            for (int i = 0; i < buttonValue.Length; i++)
            {
                buttonValue[i] = UduinoManager.Instance.digitalRead(pinsButtons[i], "ReadButtons");
            }
            UduinoManager.Instance.SendBundle("ReadButtons");
    
    //...
    

    Hope it helps !

  2. Miller Klitsner

    All of these together solved the problem! Once I added the analog bundle I didn't even need to change maxQueueLength anymore. However, on the line

    buttonValue[i] = UduinoManager.Instance.digitalRead(pinsButtons[i],"ReadButton");
    

    I'm getting the error

    Assets/ArduinoReader.cs(52,53): error CS1502: The best overloaded method match for `Uduino.UduinoManager.digitalRead(Uduino.UduinoDevice, int)' has some invalid arguments

    and

    Assets/ArduinoReader.cs(52,76): error CS1503: Argument #1' cannot convertint' expression to type `Uduino.UduinoDevice'

    I looked at the unity script reference on the Uduino site and then checked through Visual studio's auto complete, and it looks like there is no [string bundle=null] method in digitalRead's overload methods while analogRead does! Any idea how to remedy that? for now it doesn't hinder what I'm doing, but might be something to update or remedy in some way.

    Thank you for your help!!!

  3. Marc Teys repo owner

    Thank you for your feedback, I'm glad it's working now.

    I probably removed the UduinoManager.Instance.digitalRead(int,string) function in the previous build, I'll work on that tomorrow and add it in the next version.

  4. Log in to comment