- Home /
Setting up event listener for change to global variable
I'm really struggling with this.
I'm connecting to an Arduino and reading the position of 3 motors and sending it commands to have those motors go to certain positions at a set speed. I have a Kinect camera setup that detects my gesture and runs (successfully) a function called Step_Left(). I have three global variables setup that I parse the data coming from the Arduino into three ints, encoder1, encoder2, encoder3.
The first line tells the Arduino to make a motor go to position 1024. I want to wait until that motor gets to about position 250 and then send the last two serial commands at the bottom.
This is my attempt but obviously the while loop will run immediately after the first serial command, see that the condition isn't true and exit. I need to setup some kind of event or listener to wait until the variable encoder3 is close to 250 (245-255 is fine) and then run those commands.
Any suggestions?
void Step_Left() { unitySerialPort.SendSerialDataAsLine("4,3200,1024;");
// Add listener that listens for encoder3 being at position 250.
while (encoder3 > 245 && encoder3 < 255) {
print ("shoulder and elbow");
unitySerialPort.SendSerialDataAsLine ("3,3200,768;");
unitySerialPort.SendSerialDataAsLine ("2,3200,256;");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613201455im_/https://answers.unity.com/themes/thub/images/avi.jpg)