0

I am making a Visual C# executable file, and I want to use a for loop which sends 'A' and 'B' for the two cases defined in Arduino code below for 3 LEDs:

int data;
void setup() {
  Serial.begin(9600);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}

void loop() {
  if (Serial.available()){
    data=Serial.read();
    if (data=='A'){
        digitalWrite(12, HIGH);   
        digitalWrite(11, LOW);
        delay(2000);
        digitalWrite(13, HIGH);
        delay(500);
        digitalWrite(13, LOW);
        delay(500);
        digitalWrite(13, HIGH);
        delay(500);
        digitalWrite(13, LOW);
        delay(500);
    }

    else{
        digitalWrite(12, LOW);   
        digitalWrite(11, HIGH);
        delay(10000);
  }
  }
}

And this is my Visual C# code to get the number of loop from a textbox and then do AB, AB, ... (n times - equal to the number in the textbox).

if (textBox1.Text != "")
            {
            int LoopNum = Convert.ToInt32(textBox1.Text);
            for (int i=0; i < LoopNum; i++)
            {
                serialPort1.Write("A");
                serialPort1.Write("B");
            }
        }
 else
            {
                MessageBox.Show("Please select the number of cycle!", "My Application", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

I realised the loop in my programme finishes very soon (sends a bunch of ABs and then stops) while my Arduino keeps going until it finishes. Is there any way that I can add delay to my programme? I read that thread option is not a suitable way as it creates problems... Any suggestion?

Kamran
  • 75
  • 7

1 Answers1

0

If you are just looking for a delay in C# you could make the main thread sleep:

Thread.Sleep(500);

That essentially works like the arduino delay function. Of course remember that often you don't want to delay stuff in arduino. When receiving data things can get out of sync which can create problems in your setup.

7kasper
  • 35
  • 9