I’m using the following code to control a servo motor from within the Unreal Game Engine (using the UE4Duino Plugin). I send a dynamic variable (from the game engine) to the arduino serial and this value dictates the servo rotation. This all works fine…
The problem is when I want to simultaneously read from an arduino sensor.
As soon as I connect a sensor the incoming readings merge with the Unreal variables in the Serial Window and end up trying to control the servos. I presume this is due to using Serial.parseInt();
Is there a way to have two separate serial windows from the one arduino? Or… would the best way be to put unique identity tags on both the Servo Variable Data and the Sensor Data and then somehow split this stream of data based on the identity tags?
I don't see any sensor code.
parseInt() probably won't be good for you, it pauses processing until a value is fully received.
Is there a way to have two separate serial windows from the one arduino?
One way would be to use an Arduino with more than one hardware serial port, such as a Mega. Another, less satisfactory option, is to use hardware serial for one connection and SoftSerial for the other.
The data being sent to the game engine from arduino is creating an array based on the readings from two sensors. These sensor variables are being separated by a unique character )eg. an apostrophe, a comma or a backslash. The game engine knows to separate the data by looking for this unique character on the incoming arduino feed. The separation characters will be added into the arduino - see below:
At the same time the game engine will be sending data to the arduino serial window in an attept to trigger the servo. Is it an option that I can call a digitalWrite from within arduino when it determines that the serial data has come from the game engine and not from the connected sensors. For example if I were to send a unique character to arduino from the game engine exactly before sending an integer from the game engine code.....can I get arduino to look for this unique character and only write to the digital servo once it finds this incoming character ? (I hope this makes sense...if it does can anyone send me an example or let me know a key term I should search for)
Serial.println(flexer1 / 100);
Serial.println(flexer2 / 50);
let me know a key term I should search for
if (Serial.read() == 'X') //where 'X' is the special character
//read value from Serial and send to servo