首先是按键状态的获取,按键模块需要接4根线,VCC、GND、以及两根信号线,由于不知道按键的默认状态是高电平还是低电平,所以先写个代码看一下端口电平,接线如下图所示,两根信号线接的是A0和A1,看了一下原理图对应GPIO18和GPIO17.
测试代码如下:
#include <Arduino.h>
void setup()
{
Serial.begin(115200);
pinMode(18, INPUT);
pinMode(17, INPUT);
}
void loop()
{
Serial.println(digitalRead(18));
Serial.println(digitalRead(17));
delay(1000);
}
测试结果如下,可以看到当按键按下时值为0,按键默认是上拉的。
舵机控制
接下来就是舵机控制,舵机控制代码如下
蓝牙控制
#include <ESP32Servo.h>
#define SERVO_PIN 13
#define MAX_WIDTH 2500
#define MIN_WIDTH 500
// 定义 servo 对象
Servo my_servo;
bool parking = false;
void setup()
{
Serial.begin(9600);
// 分配硬件定时器
ESP32PWM::allocateTimer(0);
// 设置频率
my_servo.setPeriodHertz(50);
// 关联 servo 对象与 GPIO 引脚,设置脉宽范围
my_servo.attach(SERVO_PIN, MIN_WIDTH, MAX_WIDTH);
}
void servoDown()
{
for (int i = 180; i >= 0; i--)
{
my_servo.write(i);
delay(15);
}
}
void servoUp()
{
for (int i = 0; i <= 180; i++)
{
my_servo.write(i);
delay(15);
}
}
void loop()
{
servoUp();
delay(2500);
servoDown();
delay(2500);
}
蓝牙控制舵机部分代码如下
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp
Ported to Arduino ESP32 by Evandro Copercini
updates by chegewara
*/
#include <Arduino.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <ESP32Servo.h>
#define MAX_WIDTH 2500
#define MIN_WIDTH 500
#define SERVO_PIN 13
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_SERVO_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a9"
#include <ESP32Servo.h>
#define SERVO_PIN 13
#define MAX_WIDTH 2500
#define MIN_WIDTH 500
void servoDown();
void servoUp();
// 定义 servo 对象
Servo my_servo;
class BLECallbacks : public BLECharacteristicCallbacks
{
void onWrite(BLECharacteristic *pCharacteristic)
{
std::string value = pCharacteristic->getValue();
// 区分不同的特征值
if (pCharacteristic->getUUID().equals(BLEUUID(CHARACTERISTIC_SERVO_UUID)))
{
if (value.length() > 0)
{
Serial.println("*********");
if (value[0] == '0')
{
Serial.println("Servo down");
servoDown();
}
else if (value[0] == '1')
{
Serial.println("Servo up");
servoUp();
}
Serial.println();
Serial.println("*********");
}
}
}
};
void setup()
{
Serial.begin(115200);
Serial.println("Starting BLE work!");
BLEDevice::init("ESP32");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristicServo = pService->createCharacteristic(
CHARACTERISTIC_SERVO_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE);
pCharacteristicServo->setValue("0");
pCharacteristicServo->setCallbacks(new BLECallbacks());
pService->start();
// BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
Serial.println("Characteristic defined! Now you can read it in your phone!");
// 分配硬件定时器
ESP32PWM::allocateTimer(0);
// 设置频率
my_servo.setPeriodHertz(50);
// 关联 servo 对象与 GPIO 引脚,设置脉宽范围
my_servo.attach(SERVO_PIN, MIN_WIDTH, MAX_WIDTH);
}
void loop()
{
}
void servoDown()
{
for (int i = 180; i >= 0; i--)
{
my_servo.write(i);
delay(15);
}
}
void servoUp()
{
for (int i = 0; i <= 180; i++)
{
my_servo.write(i);
delay(15);
}
}
测光功能使用M5_DLight库,使用方法也很简单。大概流程就是声明一个sensor对象然后设置sensor的测量模式,最后就可以读取sensor的值了。改sensor有以下几种测量模式
// CONTINUOUSLY_H_RESOLUTION_MODE
// CONTINUOUSLY_H_RESOLUTION_MODE2
// CONTINUOUSLY_L_RESOLUTION_MODE
// ONE_TIME_H_RESOLUTION_MODE
// ONE_TIME_H_RESOLUTION_MODE2
// ONE_TIME_L_RESOLUTION_MODE
M5_DLight sensor;
void setup()
{
sensor.begin();
sensor.setMode(CONTINUOUSLY_H_RESOLUTION_MODE);
}
void loop()
{
float lux = 0;
lux = sensor.getLUX();
tft.fillScreen(ST77XX_BLACK);
tft.setCursor(0, 0);
tft.print("Lux: ");
tft.println(lux);
delay(1000);
}
屏幕使用Adafruit_ST7789库对屏幕进行控制,由于我不太会设计UI,因此借鉴了一下群里大佬设计的UI,参考的这篇帖子