演示视频:上位机实现Arduino可控RGB变色
Arduino是一个很好的嵌入式开发平台,它有着很好的易用性。对于我们这种电子业余爱好者,有着很强的吸引力。这两天做了一个小Demo分享给大家。
通过控制R,G,B分量来设置RGB色灯的颜色,电位器为调整光的强度。
// Arduino Nano V3.0 Board
int redPin = 3;//红色分量 PWM
int greenPin = 5; //绿色分量 PWM
int bluePin = 6; //蓝色分量 PWM
int readPin = A3; //读电位器
int putToRgbPin = 10; //输出到RGB色灯的亮度 PWM
int onOffPin = A1; //读压电陶瓷片
int beeOutPin = 11; //蜂鸣器输出 PWM
int value = 0; //亮度值
byte serialData[5]; //从串口读取的数据
void setup() {
// 此处定义一系列的引脚
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(readPin, INPUT);
pinMode(onOffPin, INPUT);
pinMode( beeOutPin , OUTPUT);
pinMode(putToRgbPin, OUTPUT);
//开始串口通信
Serial.begin(115200);
Serial.println("Baud Rate: 115200");
//初始RGB色灯不发光
setColor(0x00, 0x00, 0x00);
//定义了一个随机数种子
randomSeed(1397);
}
void loop() {
//读取电位器的模拟值并且转换成PWM输入值
value = map(analogRead(readPin), 0, 1023, 0, 255);
//输入到色灯中
analogWrite(putToRgbPin, value);
//读取压电陶瓷模拟值并且转换成蜂鸣器的输入值
analogWrite(beeOutPin, map(analogRead(onOffPin), 0, 1023, 0, 255) * 20);
//开始读取串口数据
if (Serial.available() > 0) {
//读取一个字节数组
Serial.readBytes(serialData, 5)
//判断首尾类型实现功能种类判断
if (serialData[0] == 0x01 && serialData[4] == 0x02)
{
setColor(serialData[1], serialData[2], serialData[3]); //上位机传入的颜色分量
}
if (serialData[0] == 0x01 && serialData[4] == 0x03)
{
lightFlicker(); //开始自定义闪烁
}
Serial.flush();
}
}
//输出颜色分量到LED
void setColor(int red, int green, int blue) {
analogWrite(redPin, 255 - red);
analogWrite(greenPin, 255 - green);
analogWrite(bluePin, 255 - blue);
analogWrite(beeOutPin, (red + green + blue) / 3); //蜂鸣器的发声由三个颜色分量的平均值决定
}
//随机显示颜色函数
void randColor(int num) {
while (num--) {
setColor(random(0, 255), random(0, 255), random(0, 255));
delay(500);
setColor(0, 0, 0);
delay(200);
}
}
void lightFlicker()
{
int r = 0, g = 0, b = 0;
while (1) {
for (; r <= 255; r++) {
setColor(r, g, b);
delay(4);
}
for (; g <= 255; g++) {
setColor(r, g, b);
delay(4);
}
for (; b <= 255; b++) {
setColor(r, g, b);
delay(4);
}
for (; r >= 0; r--) {
setColor(r, g, b);
delay(4);
}
for (; g >= 0; g--) {
setColor(r, g, b);
delay(4);
}
for (; b >= 0; b--) {
setColor(r, g, b);
delay(4);
}
delay(496);
setColor(0, 0, 0);
delay(300);
setColor(0xff, 0, 0);
delay(500);
setColor(0, 0, 0);
delay(300);
setColor(0, 0xff, 0);
delay(500);
setColor(0, 0, 0);
delay(300);
setColor(0, 0, 0xff);
delay(500);
setColor(0, 0, 0);
delay(300);
randColor(10);
}
}
Arduio nano开发板 如下:
上位机程序:
using System;
using System.Drawing;
using System.IO.Ports;
using System.Windows.Forms;
namespace RGB_Light
{
public partial class Form1 : Form
{
SerialPort sp = new SerialPort(); //定义一个SerialPort实现串口传输支持
byte[] sendBytes = new byte[5]; //定义一个字节数组,包含传输的RGB值
public Form1()
{
InitializeComponent();
sp.DataReceived += Sp_DataReceived;
CheckForIllegalCrossThreadCalls = false;
sendBytes[0] = 0x01;
sendBytes[1] = 0x00;
sendBytes[2] = 0x00;
sendBytes[3] = 0x00;
sendBytes[4] = 0x02;
SetTextColorValue();
comboBox2.SelectedIndex = 5;
}
private void Sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
textBox2.AppendText(sp.ReadLine() + Environment.NewLine);
}
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == -1 || comboBox2.SelectedIndex == -1)
{
MessageBox.Show("请选择一个串口进行通信!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (!sp.IsOpen)
{
SetTextColorValue();
sp.PortName = comboBox1.SelectedItem.ToString(); //用户选择串口输入
sp.BaudRate = Convert.ToInt32(comboBox2.SelectedItem.ToString()); //波特率设置
sp.Open();
Send();
button1.Text = "关闭串口";
}
else
{
sp.Close();
button1.Text = "打开串口";
}
}
private void button2_Click(object sender, EventArgs e)
{
if (sp.IsOpen)
{
MessageBox.Show("发送完毕");
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
sp.Close();
}
private void button3_Click(object sender, EventArgs e)
{
colorDialog1.AllowFullOpen = true;
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
sendBytes[1] = colorDialog1.Color.R; //字节数组1,2,3位是RGB色值 0-255
sendBytes[2] = colorDialog1.Color.G;
sendBytes[3] = colorDialog1.Color.B;
Send();
trackBar1.Value = colorDialog1.Color.R;
trackBar2.Value = colorDialog1.Color.G;
trackBar3.Value = colorDialog1.Color.B;
SetTextColorValue();
SetPicColor();
}
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
sendBytes[1] = Convert.ToByte(trackBar1.Value);
trackBar1.BackColor = Color.FromArgb(0xff, trackBar1.Value, 0, 0);
SetPicColor();
SetTextColorValue();
Send();
}
private void trackBar2_Scroll(object sender, EventArgs e)
{
sendBytes[2] = Convert.ToByte(trackBar2.Value);
trackBar2.BackColor = Color.FromArgb(0xff, 0, trackBar2.Value, 0);
SetPicColor();
SetTextColorValue();
Send();
}
private void trackBar3_Scroll(object sender, EventArgs e)
{
sendBytes[3] = Convert.ToByte(trackBar3.Value);
trackBar3.BackColor = Color.FromArgb(0xff, 0, 0, trackBar3.Value);
SetPicColor();
SetTextColorValue();
Send();
}
private void Send()
{
if (sp.IsOpen)
{
sp.Write(sendBytes, 0, 5);
}
}
private void SetPicColor()
{
Color c = Color.FromArgb(0xff, trackBar1.Value, trackBar2.Value, trackBar3.Value);
pictureBox1.BackColor = c;
}
private void SetTextColorValue()
{
textBox1.Text = string.Format("{0:X2}{1:X2}{2:X2}", trackBar1.Value, trackBar2.Value, trackBar3.Value);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click_1(object sender, EventArgs e)
{
if (!sp.IsOpen)
{
return;
}
sendBytes[4] = 0x03;
Send();
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (var item in SerialPort.GetPortNames())
{
comboBox1.Items.Add(item);
}
}
}
}
窗体布局: