// Reads from the arduino at a specified rate public void Communicate() { while (true) { if (SerialComPort.IsOpen) { // If the data mode is DECODER, read the decoder value. if (currentDataMode == ControlEnums.DATAMODE.DECODER) { decoderValue = Communications.ReadDecoder(); } if (currentDataMode != ControlEnums.DATAMODE.DECODER) { decoderValue = new KeyValuePair <int, short>(0, currentPos); } // Send the reset command if position is not between -20000 and +20000 if (currentPos >= decoderResetMax) { Communications.SendResetCommand(); lastPos = 0; decoderValue = new KeyValuePair <int, short>(0, 0); } else if (currentPos <= decoderResetMin) { Communications.SendResetCommand(); lastPos = 0; decoderValue = new KeyValuePair <int, short>(0, 0); } Communications.ReadDAC(); } // Run communication thread at 100Hz independent of the main interface Thread.Sleep(10); } }