【DigiKey“智造万物,快乐不停”创意大赛】基于Arduino Uno R4智能床头灯项目&6调试2
[复制链接]
本帖最后由 eew_cT3H5d 于 2024-1-12 23:10 编辑
第二部分调试:蓝牙通讯调试
基于Arduino Uno R4智能床头灯项目中蓝牙版本部分最重要的是蓝牙通信,蓝牙通信可以将手机发送指令传送到智能床头灯系统中
实物图
调试效果:
演示视频:
蓝牙调试
调试代码:
#include <SPI.h>
#include <SoftwareSerial.h>
// 设置灯珠数量
#define NUMPIXELS 8
// 设置SPI输出数据引脚
//#define PIN_NEOPIXEL 11
// LED引脚设置
#define PIN_LED 10
#include <Arduino.h>//驱动多合一SEN44程序
#include <SensirionUartSen44.h>
#define SENSOR_SERIAL_INTERFACE Serial1
SensirionUartSen44 sen44;
SoftwareSerial SoftSerial(D3, D2); // Rx, Tx
unsigned int datenum1=0;
unsigned int incomdate[10];
void printModuleVersions() {
uint16_t error;
char errorMessage[256];
uint8_t firmwareMajor;
uint8_t firmwareMinor;
bool firmwareDebug;
uint8_t hardwareMajor;
uint8_t hardwareMinor;
uint8_t protocolMajor;
uint8_t protocolMinor;
error = sen44.getVersion(firmwareMajor, firmwareMinor, firmwareDebug,
hardwareMajor, hardwareMinor, protocolMajor,
protocolMinor);
if (error) {
Serial.print("Error trying to execute getVersion(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
} else {
if (firmwareDebug) {
printf("Development firmware version: ");
}
Serial.print("Firmware: ");
Serial.print(firmwareMajor);
Serial.print(".");
Serial.print(firmwareMinor);
Serial.print(", ");
Serial.print("Hardware: ");
Serial.print(hardwareMajor);
Serial.print(".");
Serial.print(hardwareMinor);
Serial.print(", ");
Serial.print("Protocol: ");
Serial.print(protocolMajor);
Serial.print(".");
Serial.println(protocolMinor);
}
}
void printSerialNumber() {
uint16_t error;
char errorMessage[256];
unsigned char serialNumber[32];
uint8_t serialNumberSize = 32;
error = sen44.getSerialNumber(serialNumber, serialNumberSize);
if (error) {
Serial.print("Error trying to execute getSerialNumber(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
} else {
Serial.print("Serial number: ");
Serial.println((char*)serialNumber);
}
}
unsigned char R;
unsigned char G;
unsigned char B;
unsigned long RGB=0XFF00FF;
// 熄灭的数据
unsigned char default_tx0[24] =
{
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, //G
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, //R
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 //B
};
unsigned char WS2812[24] =
{
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, //G
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, //R
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 //B
};
// 点亮的数据
unsigned char default_tx1[3][24] = {
//绿色-G
{
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0xf8,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0xf8,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0xf8
},
//红色-R
{
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0xf8,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0xf8,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0xf8
},
//蓝-B
{
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0xf8,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0xf8,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0xf8
}
};
void lgiht_Open()
{
for (int i = 0; i < 24; i++)
{
// 显示数据转换
if((RGB>>i)&0X01) { WS2812[23-i]=0xf8; }
else { WS2812[23-i]=0x80; }
}
for (int j = 0; j < 64; j++)
{
for (int i = 0; i < 24; i++)
{
// 显示
SPI.transfer(WS2812[i]);
}
}
}
// 当前灯珠指向
int idx = 0;
void lgiht_n(int n)
{
for(int i=0;i<NUMPIXELS;i++) {
if(i==n) {
for (int i = 0; i < 24; i++)
{
// 点亮
SPI.transfer(default_tx1[ n%3 ][i]);
}
} else {
for (int i = 0; i < 24; i++)
{
// 熄灭
SPI.transfer(default_tx0[i]);
}
}
}
}
void setup(void)
{
SoftSerial.begin(9600);
// 调试串口速率设置
Serial.begin(115200);
Serial1.begin(9600);
// 设置LED3对应的引脚为输出
pinMode(PIN_LED, OUTPUT);
pinMode(D6, INPUT);//触摸电路输入
pinMode(LED_BUILTIN, OUTPUT);//LED指示灯I/O引脚
// 设置SPI时钟频率为6.4MHz,高位先传输,默认操作模式
SPI.beginTransaction(SPISettings(6400000, MSBFIRST, SPI_MODE0));
// 系统时钟分频设置方式
// SPI.begin ();
// SPI.setClockDivider(SPI_CLOCK_DIV16);//divide the clock by 8
SENSOR_SERIAL_INTERFACE.begin(115200);
while (!SENSOR_SERIAL_INTERFACE) {
delay(100);
}
sen44.begin(SENSOR_SERIAL_INTERFACE);
sen44.deviceReset();
sen44.startMeasurement();
pinMode(A0, OUTPUT);//负离子发生器模块控制I/O
pinMode(A1, OUTPUT);//涡轮风扇模块控制I/O
pinMode(D2, INPUT);
RGB=0X000000;lgiht_Open();
}
char inChar;
void loop(void)
{
uint16_t CM;//触摸开关
uint16_t massConcentrationPm1p0;
uint16_t massConcentrationPm2p5;
uint16_t massConcentrationPm4p0;
uint16_t massConcentrationPm10p0;
float vocIndex;
float ambientHumidity;
float ambientTemperature;
while (SoftSerial.available())
{
incomdate[datenum1]= int(SoftSerial.read());
datenum1=datenum1+1;
}
if(datenum1>3)
{
switch (incomdate[0])
{
case 65:
RGB=0XFF000F;lgiht_Open();
break;
case 66:
RGB=0XFF0064;lgiht_Open();
break;
case 67:
RGB=0X5000FF;lgiht_Open();
break;
case 68:
RGB=0X0032FF;lgiht_Open();
break;
case 69:
RGB=0X0064FF;lgiht_Open();
break;
case 70:
RGB=0X00FFFF;lgiht_Open();
break;
case 71:
RGB=0X00FF96;lgiht_Open();
break;
case 72:
RGB=0X00FF00;lgiht_Open();
break;
case 73:
RGB=0X2AFF00;lgiht_Open();
break;
case 74:
RGB=0X84FF00;lgiht_Open();
break;
case 75:
RGB=0XFFC800;lgiht_Open();
break;
case 76:
RGB=0XFF6400;lgiht_Open();
break;
case 77:
RGB=0XFF4500;lgiht_Open();
break;
case 78:
RGB=0XFF6347;lgiht_Open();
break;
case 79:
RGB=0XFF2D19;lgiht_Open();
break;
case 80:
RGB=0XC81905;lgiht_Open();
break;
}
datenum1=0;
}
sen44.readMeasuredMassConcentrationAndAmbientValues(
massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0,
massConcentrationPm10p0, vocIndex, ambientHumidity, ambientTemperature);
if(massConcentrationPm2p5>76)
{digitalWrite(A0, HIGH); digitalWrite(A1, HIGH);}//开启负离子净化系统
else
{digitalWrite(A0, LOW); digitalWrite(A1, LOW);}//关闭负离子净化系统
/*
if(massConcentrationPm2p5<35){RGB=0X00F0FF;lgiht_Open();}//0-35ug/m3
else if(massConcentrationPm2p5<75){RGB=0X00FF00;lgiht_Open();}//36-75ug/m3
else if(massConcentrationPm2p5<115){RGB=0XFFF000;lgiht_Open();}//76-115ug/m3
else if(massConcentrationPm2p5<150){RGB=0XFFFF70;lgiht_Open();}//116-150ug/m3
else if(massConcentrationPm2p5<250){RGB=0XFF0000;lgiht_Open();}//151-250ug/m3
else if(massConcentrationPm2p5<500){RGB=0XF048F0;lgiht_Open();}//251-500ug/m3
*/
Serial.println(massConcentrationPm2p5);
if (SoftSerial.available())
{
inChar+=SoftSerial.read();
if(inChar==0X40){digitalWrite(A1, HIGH);}
if(inChar==0X32){digitalWrite(A1, LOW);}
}
}
app代码及界面:
逻辑代码块
|