Arduino and C# Delay Time

Good day,

I am working in Arduino Mega 2560, motor and encoder, C# to interface the arduino and LED power supply that connected to PC with RS232. That's why I use 2 serial ports in this project, 1 for Arduino and 1 for LED power supply.

This is the sequence that I wish I can do in my project:

  1. C# send code to Arduino
  2. Arduino receive the code, activate the motor, encoder record the position
  3. When encoder reach my desired position, it send signal back to C#
  4. C# send code to LED power supply

For now, I am still learning and unable to conduct sequence 3 and 4. The problem is even the encoder haven't reached my desired position, C# already send code to the LED power supply.

What code should I use to make C# wait till encoder reach my desired position? I hope someone can help me. Thank you.

rudigunawan:
For now, I am still learning and unable to conduct sequence 3 and 4. The problem is even the encoder haven't reached my desired position, C# already send code to the LED power supply.

Sounds like a C# coding issue, not an Arduino problem. That is, unless the Arduino is prematurely sending the message to the PC that the encoder is in position, but you didn't mention that.

Thank you for your reply,

I think I still don't know how to send the message to PC when encoder is in position. How can I do that? Should I use Serial.write? And maybe if you can help me with the C# reading too, how to make C# wait? Thank you

rudigunawan:
Thank you for your reply,

I think I still don't know how to send the message to PC when encoder is in position. How can I do that? Should I use Serial.write? And maybe if you can help me with the C# reading too, how to make C# wait? Thank you

Post your Arduino code and we can work it out. In the mean time, you should do some C# research and maybe post in a C# forum.

You have a problem in either
the arduino app
the C# app
both

As you haven’t posted any code, it’s impossible to figure out where your problem is.

Start by testing the Arduino application using serial monitor (or another terminal program like realterm or putty); once that behaves as expected, you can concentrate on the PC side of things.

And just in case, the updated Serial Input Basics thread to get ideas for implementations.

how to make C# wait?

You should NOT make C# (or the Arduino) wait. C# can be setup with callbacks when (the right amount of) serial data arrives. The C# app can be busy doing other things until the Arduino print()s "Hey, C# dude, I've gotten to the position you wanted me to be at, and I've stopped" (or something simpler).

As for your problem, it is simple. There are stickies at the top of the forum that you were supposed to read BEFORE posting here. It is clear that you did not read them, because there are VERY clear that you MUST POST YOUR CODE in this part of the forum.

Try again.

Good day,

I am working with C# and arduino. I am still a newbie on this. I wish someone can help me. So my project is to send some code to Arduino. Then apply delay on Arduino and send back some string/char back to C#. I gave a textbox on C# to show the result of reading. This is my code:
C# code

    Public form1()
    {
    initializeComponent();
    Serialport1.Open();
    }
    
    public void Read()
    {
    while (Serialport1.IsOpen)
    {
    try
    {
    string message = SerialPort1.ReadLine();
    textbox1.text = message;
    }
    catch (timeoutException)
    {
    }
    }
    }
    private void button1_click(object sender, eventargs e)
    {
    SerialPort1.write("A");
    read();
    }

arduino code:

    int data;
    void setup()
    {
    Serial.begin(9600);
    Serial.print("START\n");
    }
    
    void loop()
    {
    if(Serial.available())
    {
    data = Serial.read();
    if(data=='A')
    {
    delay(3000);
    Serial.print("B");
    }
    }
    }

What I wish is textbox result write B, but nothing is come and the textbox keep blank. What did I do wrong? I hope someone can explain to me. Thank you

Wouldn’t it be easier to start out with the Serial Monitor instead of a C# program?
This will help you make sure that your Arduino sketch is working properly.
Then you write your C# program.

yes, I have tried with the Serial Monitor from Arduino, and it works fine, then I give input A, after 3s output B will printed on the serial monitor. But I still have no idea how to connect it with the C# interface.

rudigunawan:
yes, I have tried with the Serial Monitor from Arduino, and it works fine, then I give input A, after 3s output B will printed on the serial monitor. But I still have no idea how to connect it with the C# interface.

You don’t connect the Serial Monitor to anything.

Now you need to replace what you + Serial Monitor are doing with just the C# program.

Hello,

Does this problem already solved? i have the same problem if you have any solution please give me the link. Thank you :slight_smile: