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?
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.
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?