【Follow me第二季第4期】任务一 Blink三色LED / 串口打印
本帖最后由 yilonglucky 于 2024-11-27 18:14 编辑<p>需求分析:</p>
<p>三色LED是连接到NINA芯片上的,Arduino有现成的NINA库可以使用,将LED对应的三个GPIO扩展为了25,26,27,相当于GPIO扩展芯片。</p>
<p>串口打印可以使用默认的通道,即USB CDC,但是在系统启动、初始化期间IDE自带的“串口监视器”是无法使用的,因此计划使用nano的Tx和Rx脚进行硬线连接,但是注意,这组串口实际对应代码中的Serial1。</p>
<p> </p>
<p>所需物料:</p>
<p>nano主板和另外一块串口小板,三根杜邦线。</p>
<p> </p>
<p>设计思路:</p>
<p>系统代码运行后打印必要的信息后,开启一个线程自动翻转RP4010直连的BUILDIN LED用来提示nano代码已经开始运行。</p>
<p>另外一个线程监控Seria1发来的数据,并指定对应的亮灯动作。</p>
<p> </p>
<p>因此将三个亮灯信息编码进一个byte中:</p>
<table style="border-collapse:collapse" width="288">
<colgroup>
<col span="4" width="72" />
</colgroup>
<tbody>
<tr>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; border-top:1px solid black; border-right:1px solid black; border-left:1px solid black">Index</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; background-color:red; border-top:1px solid black; border-right:1px solid black; border-left:none">R</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; background-color:#00b050; border-top:1px solid black; border-right:1px solid black; border-left:none">G</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; background-color:#00b0f0; border-top:1px solid black; border-right:1px solid black; border-left:none">B</td>
</tr>
<tr>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:1px solid black">0</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none">0</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none">0</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none">0</td>
</tr>
<tr>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:1px solid black">1</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none">0</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none">0</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; background-color:#00b0f0; border-top:none; border-right:1px solid black; border-left:none">1</td>
</tr>
<tr>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:1px solid black">2</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none">0</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; background-color:#92d050; border-top:none; border-right:1px solid black; border-left:none">1</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none">0</td>
</tr>
<tr>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:1px solid black">3</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none">0</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; background-color:#92d050; border-top:none; border-right:1px solid black; border-left:none">1</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; background-color:#00b0f0; border-top:none; border-right:1px solid black; border-left:none">1</td>
</tr>
<tr>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:1px solid black">4</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; background-color:red; border-top:none; border-right:1px solid black; border-left:none">1</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none">0</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none">0</td>
</tr>
<tr>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:1px solid black">5</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; background-color:red; border-top:none; border-right:1px solid black; border-left:none">1</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none">0</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; background-color:#00b0f0; border-top:none; border-right:1px solid black; border-left:none">1</td>
</tr>
<tr>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:1px solid black">6</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; background-color:red; border-top:none; border-right:1px solid black; border-left:none">1</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; background-color:#92d050; border-top:none; border-right:1px solid black; border-left:none">1</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none">0</td>
</tr>
<tr>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:1px solid black">7</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; background-color:red; border-top:none; border-right:1px solid black; border-left:none">1</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; background-color:#92d050; border-top:none; border-right:1px solid black; border-left:none">1</td>
<td style="border-bottom:1px solid black; vertical-align:middle; white-space:nowrap; background-color:#00b0f0; border-top:none; border-right:1px solid black; border-left:none">1</td>
</tr>
</tbody>
</table>
<p>这样在串口输入0~7即可控制三色灯的所有组合情况。</p>
<p>实际运行效果:</p>
<p> </p>
<p>源代码:</p>
<pre>
<code class="language-cpp">// Include Scheduler since we want to manage multiple tasks.
#include <Scheduler.h>
#include "WiFiNINA.h"
#define led1 LEDR
#define led2 LEDG
#define led3 LEDB
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial1.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
// Setup the 3 pins as OUTPUT
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
Serial1.println("\n~~~~~~~~~~ RGB Blink Demo on Arduino Nano RP2040 ~~~~~~~~~~");
Serial.println("Hello DigiKey & EEWorld!");
Serial1.println("Hello DigiKey & EEWorld!");
Serial1.println("Please input the index number to blink the RGB LED:");
Serial1.println("Index R G B");
Serial1.println("0: 0 0 0");
Serial1.println("1: 0 0 1");
Serial1.println("2: 0 1 0");
Serial1.println("3: 0 1 1");
Serial1.println("4: 1 0 0");
Serial1.println("5: 1 0 1");
Serial1.println("6: 1 1 0");
Serial1.println("7: 1 1 1");
Scheduler.startLoop(loop1);
Scheduler.startLoop(loop2);
}
/*
* Name: set_rgb
* Para: rgb index
* bit2: red led
* bit1: green led
* bit0: blue led
*/
void set_rgb(char c) {
int r, g, b;
r = (c >> 2) % 2;
g = (c >> 1) % 2;
b = (c % 2);
digitalWrite(led1, (PinStatus)r);
digitalWrite(led2, (PinStatus)g);
digitalWrite(led3, (PinStatus)b);
}
void loop() {
if (Serial1.available()) {
char c = Serial1.read();
set_rgb(c);
}
// IMPORTANT:
// We must call 'yield' at a regular basis to pass
// control to other tasks.
yield();
}
void loop1() {
set_rgb(4);
delay(1000);
set_rgb(2);
delay(1000);
set_rgb(1);
delay(1000);
}
void loop2() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
</code></pre>
<div style="position: fixed; opacity: 1; height: 300px; width: 100%; font-size: 25px; text-align: center; bottom: 200px; left: 0px; display: none; flex-direction: column; justify-content: center; z-index: 1661789940; top: calc(50% + 0px);"> </div>
页:
[1]