Personally . I don't think adding Delays to your code is the right way to fix it. If you need to apply a delay in order to get the correct UWP application (Windows 10 IOT core), then there might be something that can be done better to avoid delays, since the delays are not only inaccurate but also unreliable. that the work is being done, especially when it comes to the IoT project. Everything may go wrong at any time, and the operation may take longer. In this case, your delay is interrupted, and your IoT installation begins to ruin it.
It says: I wrote a class that can help you control your stepper motor without delay, that would be fast, so if there are any problems let me know, but I checked it completely and I didn't seem to find any problems on the functioning or on the execution side. My code is as follows:
public class Uln2003Driver : IDisposable { private readonly GpioPin[] _gpioPins = new GpioPin[4]; private readonly GpioPinValue[][] _waveDriveSequence = { new[] {GpioPinValue.High, GpioPinValue.Low, GpioPinValue.Low, GpioPinValue.Low}, new[] {GpioPinValue.Low, GpioPinValue.High, GpioPinValue.Low, GpioPinValue.Low}, new[] {GpioPinValue.Low, GpioPinValue.Low, GpioPinValue.High, GpioPinValue.Low}, new[] {GpioPinValue.Low, GpioPinValue.Low, GpioPinValue.Low, GpioPinValue.High} }; private readonly GpioPinValue[][] _fullStepSequence = { new[] {GpioPinValue.High, GpioPinValue.Low, GpioPinValue.Low, GpioPinValue.High}, new[] {GpioPinValue.High, GpioPinValue.High, GpioPinValue.Low, GpioPinValue.Low}, new[] {GpioPinValue.Low, GpioPinValue.High, GpioPinValue.High, GpioPinValue.Low}, new[] {GpioPinValue.Low, GpioPinValue.Low, GpioPinValue.High, GpioPinValue.High } }; private readonly GpioPinValue[][] _haveStepSequence = { new[] {GpioPinValue.High, GpioPinValue.High, GpioPinValue.Low, GpioPinValue.Low, GpioPinValue.Low, GpioPinValue.Low, GpioPinValue.Low, GpioPinValue.High}, new[] {GpioPinValue.Low, GpioPinValue.High, GpioPinValue.High, GpioPinValue.High, GpioPinValue.Low, GpioPinValue.Low, GpioPinValue.Low, GpioPinValue.Low}, new[] {GpioPinValue.Low, GpioPinValue.Low, GpioPinValue.Low, GpioPinValue.High, GpioPinValue.High, GpioPinValue.High, GpioPinValue.Low, GpioPinValue.Low}, new[] {GpioPinValue.Low, GpioPinValue.Low, GpioPinValue.Low, GpioPinValue.Low, GpioPinValue.Low, GpioPinValue.High, GpioPinValue.High, GpioPinValue.High } }; public Uln2003Driver(int blueWireToGpio, int pinkWireToGpio, int yellowWireToGpio, int orangeWireToGpio) { var gpio = GpioController.GetDefault(); _gpioPins[0] = gpio.OpenPin(blueWireToGpio); _gpioPins[1] = gpio.OpenPin(pinkWireToGpio); _gpioPins[2] = gpio.OpenPin(yellowWireToGpio); _gpioPins[3] = gpio.OpenPin(orangeWireToGpio); foreach (var gpioPin in _gpioPins) { gpioPin.Write(GpioPinValue.Low); gpioPin.SetDriveMode(GpioPinDriveMode.Output); } } public async Task TurnAsync(int degree, TurnDirection direction, DrivingMethod drivingMethod = DrivingMethod.FullStep) { var steps = 0; GpioPinValue[][] methodSequence; switch (drivingMethod) { case DrivingMethod.WaveDrive: methodSequence = _waveDriveSequence; steps = (int) Math.Ceiling(degree/0.1767478397486253); break; case DrivingMethod.FullStep: methodSequence = _fullStepSequence; steps = (int) Math.Ceiling(degree/0.1767478397486253); break; case DrivingMethod.HalfStep: methodSequence = _haveStepSequence; steps = (int) Math.Ceiling(degree/0.0883739198743126); break; default: throw new ArgumentOutOfRangeException(nameof(drivingMethod), drivingMethod, null); } var counter = 0; while (counter < steps) { for (var j = 0; j < methodSequence[0].Length; j++) { for (var i = 0; i < 4; i++) { _gpioPins[i].Write(methodSequence[direction == TurnDirection.Left ? i : 3 - i][j]); } await Task.Delay(5); counter ++; if (counter == steps) break; } } Stop(); } public void Stop() { foreach (var gpioPin in _gpioPins) { gpioPin.Write(GpioPinValue.Low); } } public void Dispose() { foreach (var gpioPin in _gpioPins) { gpioPin.Write(GpioPinValue.Low); gpioPin.Dispose(); } } } public enum DrivingMethod { WaveDrive, FullStep, HalfStep } public enum TurnDirection { Left, Right }`
Put this as a class, and you can interact with it from any CodeBehind or ViewModel, as shown below:
private readonly Uln2003Driver _uln2003Driver;
Now that you have done the setup, use the above value:
Task.Run(async () => { await _uln2003Driver.TurnAsync(180, TurnDirection.Left, DrivingMethod.FullStep); await _uln2003Driver.TurnAsync(180, TurnDirection.Right, DrivingMethod.WaveDrive); });
The code above just rotates clockwise and counterclockwise, but feel free to
Note. Remember to call _uln2003Driver?.Dispose(); after unloading a page or completing a task to free resources. Also ? is a null conditional statement available in c#6.0 , I know it is obvious, but ran into a similar problem in another answer.
Feel free to use the comments section if you need anything.