Hi guys,
I am having a project which is control a stepper motor by arduino with C# GUI and I have no idea how to change the "n" variable of my for loop from a textbox in C#. For example, i will type a value of n whenever i want to change to angle of stepper motor.
Here my arduino code:
int i;
int n;
void setup() {
Serial.begin(9600);
pinMode(2,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
digitalWrite(9,HIGH);
delay(10);
}
int received = 0;
void loop() {
if (Serial.available() == 1)
{
received = Serial.read();
switch (received)
{
case 50: // Received ASCII 0
digitalWrite(2, HIGH);
break; //Move on
case 51: // Received ASCII 1
digitalWrite(2, LOW);
for(i=0; i<n;i++)
{
delayMicroseconds(300);
digitalWrite(10, LOW);
delayMicroseconds(600);
digitalWrite(10, HIGH);
delayMicroseconds(300);
}
delay(1000);
break; //Move on
}
}
}
Here is my C# code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace Serial_send
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
cbPORT.Items.Add(port);
}
}
private void button2_Click(object sender, EventArgs e)
{
string n = tbgoc.Text.ToString();
string t = cbPORT.Text.ToString();
string s = tbDATA.Text.ToString();
serial(t, s);
}
void serial(string Port_name, string data_send)
{
SerialPort sp = new SerialPort(Port_name, 9600, Parity.None, 8, StopBits.One);
sp.Open();
sp.Write(data_send);
sp.Close();
}
}
}
Thank u for your time to provide any help and sorry for my bad english.
regards