11
社区成员
发帖
与我相关
我的任务
分享OK,花了两天的时间把ESP32蓝牙设置红绿灯实验调试完成了
接口如下:
ESP32 OLED 红绿灯
4 SCL
15 SDA
GND GND GND
3.3V VCC
12 G
13 Y
14 R
代码:
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SdFat.h>
#include <ArduinoJson.h>
#include<NewPing.h>
#include <Wire.h>
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
// 引入驱动OLED0.91所需的库
#define SCREEN_WIDTH 128 // 设置OLED宽度,单位:像素
#define SCREEN_HEIGHT 32 // 设置OLED高度,单位:像素
// 自定义重置引脚,虽然教程未使用,但却是Adafruit_SSD1306库文件所必需的
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int positionX = 0;
String input_string;
int tt;
int ok=0;
int state = 0;
const uint8_t cn_xin [] PROGMEM =
{ 0x00, 0x00, 0x10, 0x00, 0x08, 0x04, 0x7F, 0x78, 0x00, 0x40, 0x22, 0x40, 0x14, 0x40, 0xFF, 0x7E,
0x08, 0x48, 0x08, 0x48, 0x7F, 0x48, 0x08, 0x48, 0x2A, 0x48, 0x49, 0x48, 0x88, 0x88, 0x28, 0x88,
0x11, 0x08
};/*"新",0*/
const uint8_t cn_jiang [] PROGMEM =
{ 0x00, 0x00, 0x00, 0x00, 0xFB, 0xFE, 0x08, 0x00, 0x09, 0xFC, 0xF9, 0x24, 0x81, 0xFC, 0x81, 0x24,
0xFB, 0xFE, 0x48, 0x00, 0x49, 0xFC, 0xE9, 0x24, 0x49, 0xFC, 0x69, 0x24, 0x89, 0xFC, 0x50, 0x00,
0x23, 0xFE
};/*"疆",1*/
const uint8_t cn_li [] PROGMEM =
{ 0x00, 0x00, 0x00, 0x00, 0x01, 0xFC, 0xFD, 0x24, 0x11, 0x24, 0x11, 0xFC, 0x11, 0x24, 0x11, 0x24,
0x7D, 0xFC, 0x10, 0x20, 0x10, 0x20, 0x11, 0xFC, 0x10, 0x20, 0x1C, 0x20, 0xE0, 0x20, 0x43, 0xFE,
0x00, 0x00
};/*"理",2*/
const uint8_t cn_gong [] PROGMEM =
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xFC, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0xFF, 0xFE, 0x00, 0x00,
0x00, 0x00
};/*"工",3*/
const uint8_t cn_xue [] PROGMEM =
{ 0x00, 0x00, 0x22, 0x08, 0x11, 0x08, 0x11, 0x10, 0x00, 0x20, 0x7F, 0xFE, 0x40, 0x02, 0x80, 0x04,
0x1F, 0xE0, 0x00, 0x40, 0x01, 0x80, 0xFF, 0xFE, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x05, 0x00,
0x02, 0x00
};/*"学",4*/
const uint8_t cn_yuan [] PROGMEM =
{ 0x00, 0x00, 0x00, 0x40, 0x78, 0x20, 0x4B, 0xFE, 0x52, 0x02, 0x54, 0x04, 0x61, 0xF8, 0x50, 0x00,
0x48, 0x00, 0x4B, 0xFE, 0x48, 0x90, 0x68, 0x90, 0x50, 0x90, 0x41, 0x12, 0x41, 0x12, 0x42, 0x0E,
0x44, 0x00
};/*"院",5*/
#define G 13
#define Y 12
#define R 14
int RT = 0;
int GT = 0;
void setup() {
// put your setup code here, to run once:
Wire.begin(/*SDA*/15,/*SCL*/4);
// 初始化OLED并设置其IIC地址为 0x3C
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
// 清除屏幕
display.clearDisplay();
// 设置字体颜色,白色可见
display.setTextColor(WHITE);
//设置字体大小
display.setTextSize(1);
display.display();
// Clear the buffer
display.clearDisplay();
pinMode(G, OUTPUT);
pinMode(Y, OUTPUT);
pinMode(R, OUTPUT);
}
const int cn_allArray_LEN = 6;
const uint8_t* cn_allArray[6] = {
cn_xin, cn_jiang, cn_li, cn_gong, cn_xue, cn_yuan
};
void loop() {
int len;
ok=0;
char sss;
String temp_string = ""; // 声明变量 temp_string,用于临时存储串口输入的数据
if(SerialBT.available())
{ // 当串口有数据时,循环执行
while(ok<5){
temp_string+=(char)SerialBT.read();
ok++;}
}
Serial.print("ok=");
Serial.println(ok);
delay(20);
Serial.println(sss);
Serial.println("由Serial打印");
input_string = temp_string; // 把引用指针的变量赋值为 tempStr
len = input_string.length();
Serial.println(len);
//Serial.println(intput_string);
display.clearDisplay();
if (input_string != "" && len == 5)
{
tt = input_string.substring(0, 3).toInt(); // 从字符串中截取 秒 的数据并转换成int数据类型
state = input_string.substring(4).toInt(); //状态
}
if (state == 1)
{
RT = tt;
}
if (state == 2)
{
GT = tt;
}
Serial.println(tt);
display.setTextSize(1); //设置字体大小
display.setTextColor(SSD1306_WHITE);//开像素点发光
// testdrawbitmap(cn_allArray[0],16,16);
for (int i = 0; i < 6; i++) { //新疆理工学院第一行
display.drawBitmap(i * 16, 0, cn_allArray[i], 16, 16, 1);
delay(1);
}
// put your main code here, to run repeatedly:
digitalWrite(G, HIGH);
for (unsigned int i = GT; i > 0; i--)
{
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);//开像素点发光
display.setTextSize(1); //设置字体大小
// testdrawbitmap(cn_allArray[0],16,16);
for (int i = 0; i < 6; i++) { //新疆理工学院第一行
display.drawBitmap(i * 16, 0, cn_allArray[i], 16, 16, 1);
delay(1);
}
// delay(1000);
display.setCursor(0, 20);
display.setTextColor(SSD1306_WHITE);//开像素点发光
display.setTextSize(1); //设置字体大小
display.print(i);
display.print('s');
display.display();
Serial.print(i);
Serial.println('s');
delay(1000);
}
digitalWrite(G, LOW);
digitalWrite(Y, HIGH);
for (unsigned int i = 3; i > 0; i--)
{
display.clearDisplay();
// delay(1000);
display.setTextColor(SSD1306_WHITE);//开像素点发光
display.setTextSize(1); //设置字体大小
// testdrawbitmap(cn_allArray[0],16,16);
for (int i = 0; i < 6; i++) { //新疆理工学院第一行
display.drawBitmap(i * 16, 0, cn_allArray[i], 16, 16, 1);
delay(1);
}
display.setCursor(0, 20);
display.setTextColor(SSD1306_WHITE);//开像素点发光
display.setTextSize(1); //设置字体大小
display.print(i);
display.print('s');
Serial.print(i);
Serial.println('s');
display.display();
delay(1000);
}
ok=0;
temp_string = ""; // 声明变量 temp_string,用于临时存储串口输入的数据
if(SerialBT.available())
{ // 当串口有数据时,循环执行
while(ok<5){
temp_string+=(char)SerialBT.read();
ok++;}
}
Serial.print("ok=");
Serial.println(ok);
delay(20);
Serial.println(sss);
Serial.println("由Serial打印");
input_string = temp_string; // 把引用指针的变量赋值为 tempStr
len = input_string.length();
Serial.println(len);
//Serial.println(intput_string);
display.clearDisplay();
if (input_string != "" && len == 5)
{
tt = input_string.substring(0, 3).toInt(); // 从字符串中截取 秒 的数据并转换成int数据类型
state = input_string.substring(4).toInt(); //状态
}
if (state == 1)
{
RT = tt;
}
if (state == 2)
{
GT = tt;
}
Serial.println(tt);
digitalWrite(Y, LOW);
digitalWrite(R, HIGH);
for (unsigned int i = RT; i > 0; i--)
{
display.clearDisplay();
// delay(1000);
display.setTextColor(SSD1306_WHITE);//开像素点发光
display.setTextSize(1); //设置字体大小
// testdrawbitmap(cn_allArray[0],16,16);
for (int i = 0; i < 6; i++) { //新疆理工学院第一行
display.drawBitmap(i * 16, 0, cn_allArray[i], 16, 16, 1);
delay(1);
}
// display.clearDisplay();
display.setCursor(0, 20);
display.setTextColor(SSD1306_WHITE);//开像素点发光
display.setTextSize(1); //设置字体大小
display.print(i);
display.print('s');
Serial.print(i);
Serial.println('s');
display.display();
delay(1000);
}
digitalWrite(R, LOW);
digitalWrite(Y, HIGH);
for (unsigned int i = 3; i > 0; i--)
{
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);//开像素点发光
display.setTextSize(1); //设置字体大小
// testdrawbitmap(cn_allArray[0],16,16);
for (int i = 0; i < 6; i++) { //新疆理工学院第一行
display.drawBitmap(i * 16, 0, cn_allArray[i], 16, 16, 1);
delay(1);
}
// display.clearDisplay();
display.setCursor(0, 20);
display.setTextSize(1); //设置字体大小
display.print(i);
display.print('s');
Serial.print(i);
Serial.println('s');
display.display();
delay(1000);
}
digitalWrite(Y, LOW);
display.display();
delay(20);
}
图片:

好的,我保证你按照正确连线
软件确定可用
最多库函数,ESP32开发板配置
别的绝对可行,祝学习愉快
拿去不谢
先膜拜再观看
需要那些器件麻烦告知一下,谢谢。
你好,有实验的视频?想了解一下制作过程。