/*
智能礼拜毯 - 7路 FSR 传感器阵列仿真
硬件架构: ESP32 + CD74HC4051 + PMOS电源控制
*/
// --- 引脚定义 ---
const int PIN_MUX_S0 = 32;
const int PIN_MUX_S1 = 33;
const int PIN_MUX_S2 = 27;
const int PIN_MUX_ADC = 35;
const int PIN_PWR_EN = 14; // PMOS 栅极控制
// --- 传感器名称映射 ---
const char* SENSOR_NAMES[8] = {
"L_FOOT (左脚)", // Ch 0
"R_FOOT (右脚)", // Ch 1
"L_KNEE (左膝)", // Ch 2
"R_KNEE (右膝)", // Ch 3
"L_HAND (左手)", // Ch 4
"R_HAND (右手)", // Ch 5
"HEAD (额头)", // Ch 6
"NONE (空)" // Ch 7
};
// --- 各部位设定的触发阈值 (模拟值 0-4095) ---
// 实际上电后需配合自校准,这里写死仅供演示
// 阻值越小(用力踩) -> 电压越低 -> 读数越小
int thresholds[8] = {
1500, // 脚: 1k上拉,抗误触
1500,
1500, // 膝: 1k上拉
1500,
2000, // 手: 2.2k上拉,中等灵敏
2000,
3000, // 头: 4.7k上拉,极高灵敏 (掉一点电压就算触发)
0
};
void setup() {
Serial.begin(115200);
// 初始化引脚
pinMode(PIN_MUX_S0, OUTPUT);
pinMode(PIN_MUX_S1, OUTPUT);
pinMode(PIN_MUX_S2, OUTPUT);
pinMode(PIN_PWR_EN, OUTPUT);
// 配置 ADC 精度 (ESP32 默认 12bit: 0-4095)
analogReadResolution(12);
Serial.println("System Initialized.");
Serial.println("Power Saving Mode: Active (Sensors OFF)");
delay(1000);
}
int readMux(int channel) {
// 设置地址线 S0, S1, S2
digitalWrite(PIN_MUX_S0, (channel & 1));
digitalWrite(PIN_MUX_S1, (channel & 2));
digitalWrite(PIN_MUX_S2, (channel & 4));
// 关键:等待通道切换和电容充放电稳定
delayMicroseconds(50);
// 读取并简单的软件滤波
long sum = 0;
for(int i=0; i<3; i++) {
sum += analogRead(PIN_MUX_ADC);
delayMicroseconds(10);
}
return sum / 3;
}
void loop() {
Serial.println("--- Scanning ---");
// 1. 唤醒传感器:开启 PMOS 电源 (低电平导通)
digitalWrite(PIN_PWR_EN, LOW);
// 等待电源稳定
delay(5);
// 2. 遍历读取 7 个传感器
for (int i = 0; i < 7; i++) {
int val = readMux(i);
// 打印原始数据
Serial.print(SENSOR_NAMES[i]);
Serial.print(": ");
Serial.print(val);
Serial.print("V");
// 判断触发状态
if (val < thresholds[i]) {
Serial.print(" [ACTIVE/触发] <<<");
} else {
Serial.print(" [Idle]");
}
Serial.println();
}
// 3. 进入睡眠:关闭 PMOS 电源 (高电平关断)
digitalWrite(PIN_PWR_EN, HIGH);
Serial.println("----------------");
delay(1000); // 模拟每秒扫描一次
}