Sending data from Arduino via serial to Unity

I would like to receive data from arduino into unity.
I'm printing to the serial values from three buttons and three pots with different id for each item.

How difficult is to receive that data in unity and use the values to controll other things in unity?

Is this is the way to send data to unity via serial using Serial.print() object?

Thanks

Hello hk_jh

What is the task of the programme in real life?
What is "unity"?

We need some additional information.

Post your sketch, well formated, with well-tempered comments and in so called code tags "< code >" and a detailed circuit diagram to see how we can help.

Have a nice day and enjoy coding in C++.

Unity software

code:

const int numButtons = 3;            // Number of buttons
const int numPots = 3;            // Number of pots

const int buttonPins[numButtons] = {2, 3, 4};   // Button pin numbers
const int ledPins[numButtons] = {5, 6, 7};       // LED pin numbers
const int potentiometerPins[numPots] = {A0, A1, A2};   // Potentiometer analog input pins
const int heartbeatLED = 13;

bool ledStates[numButtons] = {false};   // States of LEDs
bool previousStates[numButtons] = {false};   // Previous states of buttons
unsigned long debounceDelay = 25;     // Debounce delay time in milliseconds

unsigned long lastDebounceTime[numButtons] = {0};  // Last time the button was toggled


int lastValue[numPots];
const byte filter = 3;  //adjust as needed

//timing variables
unsigned long previousMillis = 0;
unsigned long heartbeatMillis;

void setup() {
  for (int i = 0; i < numButtons; i++) {
    pinMode(buttonPins[i], INPUT_PULLUP);    // Set button pins as inputs with internal pull-up resistors
    pinMode(ledPins[i], OUTPUT);              // Set LED pins as outputs
    pinMode(heartbeatLED, OUTPUT);
  }

  Serial.begin(115200);                  // Initialize serial communication
}

void loop() {
  checkButtons();
  readPotentiometers();
  checkHeartbeatTIMER();

}

void checkButtons() {
  for (int i = 0; i < numButtons; i++) {
    bool currentButtonState = (digitalRead(buttonPins[i]) == LOW);   // Read the current state of the button

    // Check if the button state has changed and enough time has passed for debounce
    if (currentButtonState != previousStates[i] && (millis() - lastDebounceTime[i]) > debounceDelay) {
      lastDebounceTime[i] = millis();   // Update the debounce time

      ledStates[i] = !ledStates[i];   // Toggle the state of the LED
      digitalWrite(ledPins[i], ledStates[i]);   // Update the LED state

      Serial.print("Button");
      Serial.print(i + 1);
      Serial.print(": ");
      Serial.println(ledStates[i]);

      previousStates[i] = currentButtonState;   // Update the previous button state
    }
  }
}

void readPotentiometers() {
  for (int i = 0; i < numButtons; i++) {
    int potValue = 1023 -  analogRead(potentiometerPins[i]);  // Read the potentiometer value
    //has analog value changed more than the filter amount ?
    if (abs(lastValue[i] - potValue) > filter) {
      if (lastValue[i] != potValue) {
        //update to the new value
        lastValue[i] = potValue;
        Serial.print("Potentiometer");
        Serial.print(i + 1);
        Serial.print(": ");
        Serial.println(potValue);
      }
    }
  }
}

void checkHeartbeatTIMER() {

  //is it time to toggle the heartbeatLED ?
  if (millis() - heartbeatMillis >= 500ul) {
    //restart this TIMER
    heartbeatMillis = millis();

    //toggle the heartbeatLED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }
}  //END of   checkHeartbeatTIMER()

As I never worked with unity, I try to understand if someone that working with Inity will be able to use the data I'm printing to the serial inside unity easily?

Thanks

What do you mean by using data in Unity? As far as I can see - Unity is a universal programming environment, it does not work by itself.

hk_jh,
Have you asked your question on the unity forum?
People there might be more au fait with what you can/need to do.

making a game are we..

quick search found this..
Unity Writes Arduino via Serial Port

maybe it helps..

good luck.. ~q

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.