I have a C# code which generates some x and y coordinates and i am sending these coordinates as g-code to the arduino which is programmed for 2-axis cnc code.
I am listing all the coordinates in the listbox and after 750 ms, i am sending each coordinates to the arduino.
What is my problem is that when all coordinates are sent to the arduino, i want to re-send all the coordinates again and again until sending stop command. But when coordinates are re-sent to the arduino, motors move to the position of the first coordinates as expected. However, what i want is that motors take the current coordinates as the first coordinate in the listbox instead of moving back to the first starting point.
Please help me how to readjust the code.
I am adding the current code to the below.
private void btnSendData_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen && listBox1.Items.Count > 1)
{
currentIndex = 0; // Reset the index to start from the first item
timer1.Interval = 750; // in mili-second
timer1.Start();
}
else
{
MessageBox.Show("Port Closed or Not Enough Data");
}
}
private void SendDataToArduino()
{
if (currentIndex < listBox1.Items.Count)
{
string command = listBox1.Items[currentIndex].ToString(); // Get the item as a string
command = command.Replace(": ", ""); // Remove the colon
command = command.Replace(",", ""); // Remove comma if needed
serialPort1.WriteLine(command);
textBox3.Text = command;
currentIndex++; // Move to the next item
}
else
{
// Tüm veriler gönderildi, işlemi yeniden başlat
currentIndex = 0;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
SendDataToArduino();
}
I have tried to add each x value to the first coordinates in the each loop. But this is ridiculous.
Based on what we discussed in the comments, I created a small Winforms project to emulate what you are trying to achieve.
First of all, I added several controls:
I also enabled the Console window in order to see the commands that are being sent to the motor.
First, before sending the commands to the motor, calculate the pattern by clicking on "Calculate Pattern". After that, click on "Send Data" and this will activate the timer (like in your original code) and start sending the data:
You can find the whole solution in this GitHub repository, or alternative download it as a zip here. Let me know if you have any issues opening it.
I didn't know in what format you are sending the data to the motor, so I just used strings with the format "X.Y", separated by a point '.', so you'll probably need to adjust this part with the string format you are using.
This is the code of Form1 with the new changes. I had to move the command variable outside of SendDataToArduino, made several changes to this method and added other methods btnCalculatePattern_Click and UpdateCoordinates: