0
votes

I'm working on a project to read analog data from Arduino and then display it in Visual C# with zedgraph. I have a start button on my GUI that starts the reading of serial data from Arduino. I can do that with private void read_Tick method that open the arduino port, read serial data, display data on zedgraph, and then close the arduino for every 1 second. If you having a trouble understanding my words, here's the method:

private void read_Tick(object sender, EventArgs e)
{
    try
    {

        arduino.Open();

        LineItem kurvaKonsentrasi = zedGraphKonsentrasi.GraphPane.CurveList[0] as LineItem;


        IPointListEdit listKonsentrasi = kurvaKonsentrasi.Points as IPointListEdit;

        double time = (Environment.TickCount - timeStart) / 1000.0;

        float dataKonsentrasi = float.Parse(arduino.ReadLine(), CultureInfo.InvariantCulture.NumberFormat);
        listKonsentrasi.Add(time,Convert.ToDouble(dataKonsentrasi));

        arduino.Close();

        Scale xScale = zedGraphKonsentrasi.GraphPane.XAxis.Scale;
        if (time > xScale.Max - xScale.MajorStep)
        {
            xScale.Max = time + xScale.MajorStep;
            xScale.Min = xScale.Max - 30.0;
        }

        zedGraphKonsentrasi.AxisChange();

        zedGraphKonsentrasi.Invalidate();

    }
    catch (Exception fail)
    {
        if (arduino.IsOpen)
        {
            arduino.Close();
        }
    }

}

This method is called when I clicked start button. So, my problem is, I want to send string data "on" when I click start button. This string data is used for ordering Arduino to move the servo I attached with this code in void loop() before the analog readings.

if(Serial.available()>0){
    start = Serial.read();
      if(start == "on"){
        servoMotor.write(40);
  }
}

I know there's something wrong with what I'm doing because I cant start the servo. Can you give me advice what should I do to make the Visual C# send the command to arduino to start the servo once then arduino start the readings and then Visual C# read it?

1
Do you have the arduino listening for serial input on the other side?swmcdonnell
Sorry, this is what I meant to say. Have you checked to make sure that your code can move the servo, just to make sure that it's a serial input issue and not some other kind of issue? It could be another problem, for example, not setting up the pin or attaching the servo to it correctlyswmcdonnell
@swmcdonnell I have SerialPort arduino = new SerialPort("COM3",115200); on Visual C#bnrfly
@swmcdonnell I have done that, but when I combined with the analog reading, it wont work.bnrfly

1 Answers

0
votes

Serial.read() only returns a single byte, so you're not going to get "on". You'll get "o" the first time, and if you read it again, you'll get "n."

If you follow the "on" with a "\n" (new line character), you can do something like this:

char command[3];
if (Serial.available() > 0) {
   int bytesRead = Serial.readBytesUntil('\n', command, 2);
   command[2] = '\0';
   if strcmp(command, "on") == 0) {
      servoMotor.write(40);
   }
}