第1步:需要什么
硬件
LattePanda/Arduino UNO
软件
Viusal Studio
Arduino IDE
步骤2:C#代码
创建一个新的Windows Form项目。在左侧的工具箱中,从工具箱中拖出2个按钮组件。重命名它们,一个为“ ON”,一个为“ OFF”。
public partial class Form1 : Form
{
SerialPort port;
public Form1()
{
InitializeComponent();
this.FormClosed += new FormClosedEventHandler(Form1_FormClosed);
if (port == null)
{
//Change the portname according to your computer
port = new SerialPort(“COM4”, 9600);
port.Open();
}
}
void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (port != null && port.IsOpen)
{
port.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
PortWrite(“1”);
}
private void button2_Click(object sender, EventArgs e)
{
PortWrite(“0”);
}
private void PortWrite(string message)
{
if (port != null && port.IsOpen)
{
port.Write(message);
}
}
}
第3步:Arduino Sketch
打开Arduino IDE,将以下代码上传到您的电路板上。
const int LedPin = 3;int ledState = 0;
void setup()
{
pinMode(LedPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
char receiveVal;
if(Serial.available() 》 0)
{
receiveVal = Serial.read();
if(receiveVal == ‘1’)
ledState = 1;
else
ledState = 0;
}
digitalWrite(LedPin, ledState);
delay(50);
}
步骤4:Showtime
当您单击“打开”时‘按钮,LED灯将点亮。
到目前为止还好吗?
如果您用其他东西代替LED,那么您可以使用鼠标来控制一切!这是一个非常有用的功能。
-
GUI
+关注
关注
3文章
693浏览量
42848 -
Arduino
+关注
关注
190文章
6515浏览量
195965
发布评论请先 登录
如何在 NuMaker-IoT-M467 板上使用 Arduino IDE 控制 Wi-Fi 模块?
GUI Guider全新优化方案GUI xTurbo-VeloRender初体验:基于i.MX RT平台的LVGL渲染能力突破
【PCA9958HN-ARD】GUI工具的使用
免费分享Arduino入门+进阶(全套例程+书籍)
《ESP32S3 Arduino开发指南》第二章 Arduino基础知识
树莓派GUI应用开发:从零到炫酷的魔法之旅!

如何制作最简单的GUI来控制您的arduino
评论