养生 装修 购物 美食 感冒 便秘 营销 加盟 小吃 火锅 管理 创业 搭配 减肥 培训 旅游

arduino控制舵机,上位机C# 或winform控制舵机

时间:2024-09-22 04:02:48

这是一个很好用的调试舵机的程序,不用反复向arduino里输入值,而是直接向上位机控制舵机,直接想输入啥值,直接输入就可以了,还可以自己调控差值进行调试。直接按键盘就可以调。非常方便,下面就把这个方法介绍给大家。

arduino控制舵机,上位机C# 或winform控制舵机

工具/原料

常用舵机,杜邦线,攻线,arduinoUNO

VS2010,arduinoIDE

方法/步骤

1、第一步,烧录arduino程序。源程序如下:#include<Servo.h>挢旗扦渌;//引入libServo罪焐芡拂myservo;//创建一个伺服电机对象charinByte=0;//串口接收的数据intangle=0;//角度值Stringtemp="";//临时字符变量,又或者说是缓存用的吧voidsetup(){myservo.attach(9);//定义舵机的引脚为9,舵机只能是10,或者9引脚Serial.begin(9600);//设置波特率}voidloop(){while(Serial.available()>0)//判断串口是否有数据{inByte=Serial.read();//读取数据,串口一次只能读1个字符temp+=inByte;//把读到的字符存进临时变量里面缓存,//再继续判断串口还有没有数据,知道把所有数据都读取出来}if(temp!="")//判断临时变量是否为空{angle=temp.toInt();//把变量字符串类型转成整型Serial.println(angle);//输出数据到串口上,以便观察}temp="";//请看临时变量myservo.write(angle);//控制舵机转动相应的角度。delay(50);//延时100毫秒}大家有什么问题还可以问我。

arduino控制舵机,上位机C# 或winform控制舵机

2、第二步,arduino与舵机连线,如下图所示。

arduino控制舵机,上位机C# 或winform控制舵机

3、第三步,VS2010新建项目,打开vs2010,新建项目——其他语言——选择C#

arduino控制舵机,上位机C# 或winform控制舵机

arduino控制舵机,上位机C# 或winform控制舵机

4、第四部,写C#上位机程序usingSystem;usingSystem.Collect坡纠课柩ions.Generic;锇栀劐箨usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.Threading;usingSystem.IO.Ports;usingSystem.Diagnostics;usingSystem.Globalization;usingSystem.Text.RegularExpressions;namespace遥控器{publicpartialclassForm1:Form{Stopwatchsw=newStopwatch();booldrawornot=false;SerialPortserialPort1=newSerialPort();publicForm1(){InitializeComponent();sw.Start();Control.CheckForIllegalCrossThreadCalls=false;//防止跨线程出错//定时器中断时间comboBox1.Items.Add("1200");comboBox1.Items.Add("2400");comboBox1.Items.Add("4800");comboBox1.Items.Add("9600");comboBox1.Items.Add("14400");comboBox1.Items.Add("19200");comboBox1.Items.Add("28800");comboBox1.Items.Add("38400");//常用的波特率try{string[]ports=SerialPort.GetPortNames();//得到接口名字//将端口列表添加到comboBoxthis.comboBox2.Items.AddRange(ports);///设置波特率serialPort1.BaudRate=Convert.ToInt32(comboBox1.Text);}catch(Exceptionex){}}protectedoverrideboolProcessCmdKey(refMessagemsg,KeyskeyData){if(keyData==Keys.A){button3.PerformClick();}if(keyData==Keys.D){button2.PerformClick();}returntrue;}privatevoidbutton5_Click(objectsender,EventArgse){if(serialPort1.IsOpen)////(更新)如果按下按钮之前串口是开的,就断开//如果按下按钮之前flag的内容是false按下之后内容改成true然后打开串口{serialPort1.Close();button5.Text="连接";}else{//要打开串口要看波特率串口等有没有设置对boolno_error_flag=true;try{serialPort1.BaudRate=Convert.ToInt32(comboBox1.SelectedItem);}catch(ArgumentExceptione1){this.errorProvider1.SetError(this.comboBox1,"不能为空");no_error_flag=false;}try{serialPort1.PortName=Convert.ToString(comboBox2.SelectedItem);}catch(ArgumentExceptione2){this.errorProvider1.SetError(this.comboBox2,"不能为空");no_error_flag=false;}try{serialPort1.Open();}catch{MessageBox.Show("端口错误","警告");no_error_flag=false;}if(no_error_flag){button5.Text="断开连接";}}}inti=0;intangle=0;privatevoidbutton3_Click(objectsender,EventArgse){angle=angle+i;if(angle>180){angle=180;}stringstm=Convert.ToString(angle);serialPort1.Write(stm);}privatevoidbutton2_Click(objectsender,EventArgse){angle=angle-i;if(angle<0){angle=0;}stringstm=Convert.ToString(angle);serialPort1.Write(stm);}privatevoidbutton6_Click_2(objectsender,EventArgse){stringmessage=textBox1.Text;serialPort1.Write(message);}privatevoidbutton7_Click(objectsender,EventArgse){textBox1.Clear();}privatevoidbutton1_Click_1(objectsender,EventArgse){i=Convert.ToInt16(textBox2.Text);angle=Convert.ToInt16(textBox3.Text);}privatevoidbutton4_Click(objectsender,EventArgse){textBox2.Clear();}privatevoidbutton8_Click(objectsender,EventArgse){string[]ports=SerialPort.GetPortNames();this.comboBox2.Items.Clear();this.comboBox2.Items.AddRange(ports);}privatevoidbutton10_Click(objectsender,EventArgse){textBox3.Clear();}}}这是程序啦,下面我们看一下效果

arduino控制舵机,上位机C# 或winform控制舵机

© 一点知识