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?
SerialPort arduino = new SerialPort("COM3",115200);
on Visual C# – bnrfly