【Follow me第二季第1期】+任务提交汇总
[复制链接]
本帖最后由 流水源 于 2024-10-9 20:56 编辑
主要任务提交汇总:
入门开发环境使用的是arduino ide开发的。搭建环境很简单,就不多说了。具体搭建如下帖子。
【Follow me第二季第1期】arduino开发环境的搭建和点灯 https://bbs.eeworld.com.cn/thread-1289879-1-1.html
下面是基础任务一(必做):控制板载炫彩LED,跑马灯点亮和颜色变换。主要使用了Adafruit Circuit Playground Express开发板,通过USB供电。
在如下帖子,实现彩灯跑马变幻颜色。
【Follow me第二季第1期】arduino控制板载炫彩跑马灯 https://bbs.eeworld.com.cn/thread-1289916-1-1.html
上面就是红绿蓝3种不同颜色的灯循环跑马效果了。
代码:
/////////////////////////////////////////////////////////////////////////////////////////
//11111111111111111111111111111111111111111111111111111111111111111111111111111111111111
#if TASK_NUM == 1
#include <Adafruit_CircuitPlayground.h>
#include <Adafruit_Circuit_Playground.h>
//基础任务一(必做):控制板载炫彩LED,跑马灯点亮和颜色变换
Adafruit_CircuitPlayground cplay = Adafruit_CircuitPlayground();
#define LED_PIN 13
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
cplay.begin();
cplay.setBrightness(50);
}
// the loop function runs over and over again forever
void loop() {
Serial.println(cplay.lightSensor());
Serial.println(cplay.temperature());
digitalWrite(LED_PIN, HIGH); // turn the LED on (HIGH is the voltage level)
for (int i = 0; i < 10; i++) {
cplay.setPixelColor(i, 25*i+25, 0, 0); // 设置LED为red色
delay(100);
}
cplay.clearPixels();
delay(100); // wait for a second
digitalWrite(LED_PIN, LOW); // turn the LED off by making the voltage LOW
for (int i = 0; i < 10; i++) {
cplay.setPixelColor(i, 0, 0, 25*i+25); // 设置LED为blue色
delay(100);
}
cplay.clearPixels();
delay(100); // wait for a second
for (int i = 0; i < 10; i++) {
cplay.setPixelColor(i, 0, 25*i+25, 0); // 设置LED为green色
delay(100);
}
cplay.clearPixels();
delay(100); // wait for a second
}
#endif
接着基础任务二(必做):监测环境温度和光线,通过板载LED展示舒适程度。主要使用了Adafruit Circuit Playground Express开发板,通过USB供电。
具体查看如下帖子:
【Follow me第二季第1期】监测环境温度和光线,通过板载LED展示舒适程度 https://bbs.eeworld.com.cn/thread-1290437-1-1.html
主要是取温度和光照一个舒适的数据范围,在范围内,所有10个彩灯为绿色呼吸。
前5颗彩灯标识温度舒适范围,在舒适范围内5个灯绿色呼吸,高于舒适温度范围用红色灯标识,灯的数量越多表示温度越高。低于舒适温度范围用蓝色灯标识,灯的数量越多表示温度越低。
后5颗彩灯标识光照舒适范围,在舒适范围内5个灯绿色呼吸,高于舒适光照范围用红色灯标识,灯的数量越多表示光照越强。低于舒适光照范围用蓝色灯标识,灯的数量越多表示光照越弱。
下面全绿灯表示温度和光照都在设置的舒适数据范围。
下面是光照超出舒适范围,太亮了。有一边显示红灯。
下面是温度不在舒适范围,太冷了。使用蓝色灯标识。
代码如下:
#if TASK_NUM == 2
//2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222
#include <Adafruit_CircuitPlayground.h>
#include <Adafruit_Circuit_Playground.h>
//基础任务二(必做):监测环境温度和光线,通过板载LED展示舒适程度
Adafruit_CircuitPlayground cplay = Adafruit_CircuitPlayground();
#define LED_PIN 13
uint32_t lightbress;
uint8_t l_dir;
int light_val;
int temp_val;
int g_state;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
cplay.begin();
cplay.setBrightness(50);
lightbress = 0;
l_dir = 0;
g_state = 0;
}
// the loop function runs over and over again forever
void loop()
{
light_val = cplay.lightSensor();
temp_val = cplay.temperature() * 100;
Serial.println(light_val);
Serial.println(temp_val);
switch(g_state)
{
case 0:
for (int i = 0; i < 10; i++)
{
cplay.strip.setPixelColor(i, 0, lightbress * 10, 0); // 设置LED为green色
}
cplay.strip.show();
if(l_dir == 0)
{
lightbress++;
if(lightbress == 15) l_dir = 1;
}else
{
if(lightbress) lightbress--;
else l_dir = 0;
}
if((light_val > 510) || (light_val < 190) || (temp_val > 3250)||(temp_val < 2950))
{
lightbress = 0;
g_state = 1;
}
break;
case 1:
//温度
int level;
uint8_t r_color,g_color,b_color;
if(temp_val > 3200)
{
level = (temp_val - 3200)/100;
r_color = lightbress * 10;
b_color = 0;
g_color = 0;
}else if(temp_val < 3000)
{
level = (3000 - temp_val)/100;
b_color = lightbress * 10;
r_color = 0;
g_color = 0;
}else
{
g_color = lightbress * 10;
r_color = 0;
b_color = 0;
}
if(level >= 5) level = 5;
for (int i = 0; i < 5; i++)
{
if(i<level) cplay.strip.setPixelColor(i, r_color, g_color, b_color); // 设置LED为blue red色
else cplay.strip.setPixelColor(i, 0, g_color, 0); // 设置LED为blue red色
}
//光照
if(light_val > 500)
{
level = (light_val - 500)/80;
r_color = lightbress * 10;
b_color = 0;
g_color = 0;
}else if(light_val < 200)
{
level = (200 - light_val)/30;
b_color = lightbress * 10;
r_color = 0;
g_color = 0;
}else
{
g_color = lightbress * 10;
r_color = 0;
b_color = 0;
}
if(level >= 5) level = 5;
for (int i = 5; i < 10; i++)
{
if(i>=(10-level)) cplay.strip.setPixelColor(i, r_color, g_color, b_color); // 设置LED为blue red色
else cplay.strip.setPixelColor(i, 0, g_color, 0); // 设置LED为blue red色
}
cplay.strip.show();
lightbress++;
if(lightbress == 15) lightbress = 0;
if((light_val <= 500) && (light_val >= 200) && (temp_val <= 3250) && (temp_val >= 2950))
{
lightbress = 0;
g_state = 0;
}
break;
}
delay(100);
}
#endif
接着是基础任务三(必做):接近检测——设定安全距离并通过板载LED展示,检测到入侵时,发起声音报警。
使用的是光照传感器来检测距离远近的。用手从远往近挡住光线传感器可以看到亮灯数变化。
具体帖子如下:
【Follow me第二季第1期】接近检测设定安全距离并通过板载LED展示 https://bbs.eeworld.com.cn/thread-1290460-1-1.html
使用光照来检测距离,环境光有很大影响。后面我尝试使用了红外发射和接收管来做了一下。但是使用红外时检测距离不是很大。
代码如下:
#if TASK_NUM == 3
//333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333
//基础任务三(必做):接近检测——设定安全距离并通过板载LED展示,检测到入侵时,发起声音报警
#include <Adafruit_CircuitPlayground.h>
#include <Adafruit_Circuit_Playground.h>
Adafruit_CircuitPlayground cplay = Adafruit_CircuitPlayground();
#define LED_PIN 13
int light_val;
int level;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
cplay.begin();
cplay.setBrightness(50);
pinMode(CPLAY_IR_EMITTER, OUTPUT);
digitalWrite(CPLAY_IR_EMITTER, LOW); // When not sending PWM, we want it low
pinMode(A10, INPUT);
}
void loop()
{
uint8_t r_color,g_color,b_color;
// light_val = cplay.lightSensor();
// Serial.println(light_val);
// if(light_val < 350)
// {
// level = (350-light_val)/30;
// if (CircuitPlayground.slideSwitch()) {
// CircuitPlayground.speaker.enable(true);
// Serial.println("Slide to the left");
// }
// }else
// {
// level = 0;
// CircuitPlayground.speaker.enable(false);
// }
// if(level > 7)
// {
// r_color = 0x50;
// g_color = 0;
// b_color = 0;
// CircuitPlayground.playTone(500 + level * 500, 100);
// }else if(level >4)
// {
// r_color = 0;
// g_color = 0x50;
// b_color = 0;
// CircuitPlayground.playTone(500 + level * 500, 100);
// }else
// {
// r_color = 0;
// g_color = 0;
// b_color = 0x50;
// CircuitPlayground.playTone(500 + level * 500, 100);
// }
// for (int i = 0; i < 10; i++)
// {
// if(i<level) cplay.strip.setPixelColor(i, r_color, g_color, b_color); //
// else cplay.strip.setPixelColor(i, 0, 0, 0); //
// }
// cplay.strip.show();
// Measure the proximity level and use it to light up its LEDs.
digitalWrite(CPLAY_IR_EMITTER, HIGH);
delay(10);
digitalWrite(CPLAY_IR_EMITTER, LOW);
int prox = analogRead(A10);
if(prox > 650) level = 11;
else if(prox < 300) level = 0;
else level = (prox - 300)/34;
Serial.print("IR_Vol:");Serial.println(prox);
Serial.print("Level: ");Serial.println(level);
for (int i = 0; i < 10; i++) {
if(i<level) cplay.strip.setPixelColor(i, 250, 0, 0); //
else cplay.strip.setPixelColor(i, 0, 0, 0); //
}
CircuitPlayground.strip.show();
if (level > 4) {
CircuitPlayground.playTone(330, 100);
}
delay(100);
}
#endif
再接着是进阶任务(必做):制作不倒翁——展示不倒翁运动过程中的不同灯光效果。
这个是通过读取加速度传感器来实现不同姿态显示灯光。通过Z轴加速度来确定倾斜的角度大小,角度越小就显示红色灯,角度越大实现蓝色灯垂下的效果。
通过X,Y轴加速度的数值来确定方位,通过正负值分为4个象限来显示周围的灯。
下面是倾斜不同角度的灯光效果。
当水平放置时,所有灯为红色,并且有个绿灯跑马灯效果。
代码:
#if TASK_NUM == 4
//444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444
//进阶任务(必做):制作不倒翁——展示不倒翁运动过程中的不同灯光效果
#include <Adafruit_CircuitPlayground.h>
#define LED_PIN 13
int x,y,z;
uint8_t r,g,b;
uint8_t led_color[16][4];
uint8_t led_num,led_index;
// the setup function runs once when you press reset or power the board
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
Serial.println("Circuit Playground test!");
CircuitPlayground.begin();
CircuitPlayground.strip.setBrightness(255);
led_num = 6;
led_index = 0;
}
void loop()
{
x = CircuitPlayground.motionX() * 100;
y = CircuitPlayground.motionY() * 100;
z = CircuitPlayground.motionZ() * 100;
if((z > 900) || (z < -900)) led_num = 6;
else if((z > 830) || (z < -830)) led_num = 5;
else if((z > 750) || (z < -750)) led_num = 4;
else if((z > 600) || (z < -600)) led_num = 3;
else if((z > 300) || (z < -300)) led_num = 2;
else led_num = 1;
if(led_num < 6)
{
if((x>0) && (y>0)) //1
{
int ttan = 100*y / x;
if(ttan < 26) led_index = 9;
else if(ttan < 100) led_index = 10;
else if(ttan < 373) led_index = 11;
else led_index = 0;
}else if((x<0) && (y>0)) //2
{
int ttan = 0 - (100*y / x);
if(ttan < 26) led_index = 3;
else if(ttan < 100) led_index = 2;
else if(ttan < 373) led_index = 1;
else led_index = 0;
}else if((x<0) && (y<0)) //3
{
int ttan = (100*y / x);
if(ttan < 26) led_index = 3;
else if(ttan < 100) led_index = 4;
else if(ttan < 373) led_index = 5;
else led_index = 6;
}else if((x>0) && (y<0)) //4
{
int ttan = 0 - (100*y / x);
if(ttan < 26) led_index = 9;
else if(ttan < 100) led_index = 8;
else if(ttan < 373) led_index = 7;
else led_index = 6;
}
}else
{
led_index ++;
led_index %= 12;
}
for (int i = 1; i <= 6; i++)
{
if(i<=led_num)
{
led_color[(led_index + i) % 12 ][0] = 0x80;
led_color[(led_index + i) % 12 ][1] = 0;
led_color[(led_index + i) % 12 ][2] = 0;
led_color[(12 + led_index - i) % 12 ][0] = 0x80;
led_color[(12 + led_index - i) % 12 ][1] = 0;
led_color[(12 + led_index - i) % 12 ][2] = 0;
}else
{
led_color[(led_index + i) % 12 ][0] = 0;
led_color[(led_index + i) % 12 ][1] = 0;
led_color[(led_index + i) % 12 ][2] = 0x30;
led_color[(12 + led_index - i) % 12 ][0] = 0;
led_color[(12 + led_index - i) % 12 ][1] = 0;
led_color[(12 + led_index - i) % 12 ][2] = 0x30;
}
}
led_color[(led_index )][0] = 0;
led_color[(led_index )][1] = 0xF0;
led_color[(led_index )][2] = 0;
for (int i = 1; i < 12; i++)
{
if(i<6)
{
CircuitPlayground.strip.setPixelColor(i-1,led_color[i][0], led_color[i][1], led_color[i][2]); //
}
else if(i>6)
{
CircuitPlayground.strip.setPixelColor(i-2,led_color[i][0], led_color[i][1], led_color[i][2]); //
}
}
CircuitPlayground.strip.show();
/************* TEST ACCEL */
// Display the results (acceleration is measured in m/s*s)
Serial.print("X: "); Serial.print(x);
Serial.print(" \tY: "); Serial.print(y);
Serial.print(" \tZ: "); Serial.print(z);
Serial.println(" m/s^2");
delay(50);
digitalWrite(LED_PIN, HIGH);
delay(50);
digitalWrite(LED_PIN, LOW);
}
#endif
最后是创意任务:钢琴弹奏,通过触摸引脚弹奏音乐,并配合灯光效果。
板上有7个触摸端口,正好可以对应do、re、mi、fa、sol、la、si 7个音节。这里还实现了通过左右按键来改变低,中,高音。
主要代码如下:
#if TASK_NUM == 5
//5555555555555555555555555555555555555555555555555555555555555555555555555555555
//创意任务三:水果钢琴——通过触摸水果弹奏音乐,并配合灯光效果
#include <Adafruit_CircuitPlayground.h>
#define LED_PIN 13
#define TONE_DURATION_MS 80 // Duration in milliseconds to play a tone when touched.
uint16_t CAP_THRESHOLD = 200; // Threshold for a capacitive touch (higher = less sensitive).
uint8_t playSound;
uint16_t cap_val;
uint8_t music_lmh = 1; //0-低音,1-中音,2-高音
uint16_t music_hz_tbl[3][8]= //音频对照表
{
{262, 294, 330, 349, 392, 440, 494, 0},
{523, 587, 659, 698, 784, 880, 988, 0},
{1046, 1175, 1318, 1397, 1568, 1760, 1976, 0},
};
void setup()
{
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
Serial.println("Circuit Playground test!");
CircuitPlayground.begin();
CAP_THRESHOLD = 600;
}
void loop()
{
if (CircuitPlayground.slideSwitch())
{
if(playSound) Serial.println("Slide to the left");
playSound = 0;
} else
{
if(!playSound) Serial.println("Slide to the right");
playSound = 1;
}
/************* TEST BOTH BUTTONS */
if (CircuitPlayground.leftButton()) {
Serial.println("Left level ++");
music_lmh++;
if(music_lmh>2) music_lmh=2;
while(CircuitPlayground.leftButton()) delay(10);
}
if (CircuitPlayground.rightButton()) {
Serial.println("Right level --");
if(music_lmh) music_lmh--;
while(CircuitPlayground.rightButton()) delay(10);
}
//清除灯
for (int i=0; i<10; ++i)
{
CircuitPlayground.strip.setPixelColor(i, 0);
}
CircuitPlayground.strip.show();
//11111111111111111111111111111111
cap_val = CircuitPlayground.readCap(3);
if (cap_val >= CAP_THRESHOLD) {
Serial.print("#1:"); Serial.println(cap_val);
if (playSound) {
for (int i=0; i<10; ++i) CircuitPlayground.strip.setPixelColor(i, CircuitPlayground.colorWheel(0));
CircuitPlayground.strip.show();
CircuitPlayground.playTone(music_hz_tbl[music_lmh][0], TONE_DURATION_MS); // 262hz = C4
}
}
//2222222222222222222222222222222
cap_val = CircuitPlayground.readCap(2);
if (cap_val >= CAP_THRESHOLD) {
Serial.print("\t#2:"); Serial.println(cap_val);
if (playSound) {
for (int i=0; i<10; ++i) CircuitPlayground.strip.setPixelColor(i, CircuitPlayground.colorWheel(256/10));
CircuitPlayground.strip.show();
CircuitPlayground.playTone(music_hz_tbl[music_lmh][1], TONE_DURATION_MS); // 294hz = D4
}
}
//333333333333333333333333333333333
cap_val = CircuitPlayground.readCap(0);
if (cap_val >= CAP_THRESHOLD) {
Serial.print("\t\t#3:"); Serial.println(cap_val);
if (playSound) {
for (int i=0; i<10; ++i) CircuitPlayground.strip.setPixelColor(i, CircuitPlayground.colorWheel(256/10*3));
CircuitPlayground.strip.show();
CircuitPlayground.playTone(music_hz_tbl[music_lmh][2], TONE_DURATION_MS); // 330hz = E4
}
}
//4444444444444444444444444444444444
cap_val = CircuitPlayground.readCap(1);
if (cap_val >= CAP_THRESHOLD) {
Serial.print("\t\t\t#4:"); Serial.println(cap_val);
if (playSound) {
for (int i=0; i<10; ++i) CircuitPlayground.strip.setPixelColor(i, CircuitPlayground.colorWheel(256/10*4));
CircuitPlayground.strip.show();
CircuitPlayground.playTone(music_hz_tbl[music_lmh][3], TONE_DURATION_MS); // 349hz = F4
}
}
//555555555555555555555555555555555
cap_val = CircuitPlayground.readCap(6);
if (cap_val >= CAP_THRESHOLD) {
Serial.print("\t\t\t\t#5:"); Serial.println(cap_val);
if (playSound) {
for (int i=0; i<10; ++i) CircuitPlayground.strip.setPixelColor(i, CircuitPlayground.colorWheel(256/10*5));
CircuitPlayground.strip.show();
CircuitPlayground.playTone(music_hz_tbl[music_lmh][4], TONE_DURATION_MS); // 392hz = G4
}
}
//66666666666666666666666666666666666
cap_val = CircuitPlayground.readCap(9);
if (cap_val >= CAP_THRESHOLD) {
Serial.print("\t\t\t\t\t#6:"); Serial.println(cap_val);
if (playSound) {
for (int i=0; i<10; ++i) CircuitPlayground.strip.setPixelColor(i, CircuitPlayground.colorWheel(256/10*6));
CircuitPlayground.strip.show();
CircuitPlayground.playTone(music_hz_tbl[music_lmh][5], TONE_DURATION_MS); // 440hz = A4
delay(5);
}
}
//7777777777777777777777777777777777777
cap_val = CircuitPlayground.readCap(10);
if (cap_val >= CAP_THRESHOLD) {
Serial.print("\t\t\t\t\t\t#7:"); Serial.println(cap_val);
if (playSound) {
for (int i=0; i<10; ++i) CircuitPlayground.strip.setPixelColor(i, CircuitPlayground.colorWheel(256/10*8));
CircuitPlayground.strip.show();
CircuitPlayground.playTone(music_hz_tbl[music_lmh][6], TONE_DURATION_MS); // 494hz = B4
}
}
delay(30);
}
#endif
代码文件:
最后总结:
第一次参加这个活动,比较顺利完成了上述任务。这次参加活动主要使用了arduino ide来开发,也是初次使用这个arduino。以前也听说了很多arduino的优势,但是没怎么用过。当初参加活动也是为了学习一下arduino的开发方式。所以开始的时候花了不少时间熟悉这个arduino开发环境,总的来说用过之后感觉用这个arduino开发太方便了,外设所有驱动都写好了,直接写应用就可以了。再就是这个Adafruit Circuit Playground Express开发板上面板载外设多,功能强大,不需要外接设备就可以做很多创意了,这也是吸引我的地方。
下面就是整个任务的视频效果。
视频:
汇总提交
|