I'm trying to rewrite some code I already have for Arduino in C# for use on the Raspberry Pi 3 (Windows 10 IOT). The Arduino code is I'm having trouble with is the following:
void loop()
{
while(true)
{
Step();
delay(100);
}
}
void Step()
{
digitalWrite(StepPin, HIGH); //Sets the output to HIGH
delayMicroseconds(2000); //Waits 2ms
digitalWrite(StepPin, LOW); //Sets the output to LOW
}
This is code is to control a stepper motor using an A4988 stepper driver. The code objective is to give the driver board a pulse of 2ms every 100ms.
What I've done in C# so far is this:
public void Runner()
{
while(true)
{
Step();
//100ms delay here
}
}
public void Step()
{
StepPin.Write(GpioPinValue.High);
//2ms delay here
StepPin.Write(GpioPinValue.Low);
}
I've tried Thread.Sleep() but it doesn't seem appropriate and also this doesn't seem better:
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
while(stopwatch.ElapsedMilliseconds < 2) ;
What would the optimal way to implement this kind of delay be?