Arduino编译报错求助:'PCM'未定义等多类编译错误
Arduino编译错误修复方案
错误分析与解决步骤
1. 核心错误:'PCM' does not name a type
这是引发后续所有错误的根源,原因包括:
- 未安装对应
PCM音频库,或安装的库与代码预期不匹配 - 库文件路径错误,Arduino IDE无法找到
PCM.h头文件
解决方法:
- 打开Arduino IDE,通过「项目」→「加载库」→「管理库」搜索
PCM,选择支持PROGMEM音频播放的官方/主流库完成安装 - 若手动添加库,确保
PCM.h文件放置在Arduino库目录下,且头文件内正确声明了PCM类
2. 'player' was not declared in this scope
此错误是PCM类型未被识别导致的连锁问题,解决第一个错误后,该问题会自动消失。
3. 'analogWriteResolution' was not declared in this scope
analogWriteResolution()仅支持ESP32、Arduino Due/Zero等特定型号板卡,普通AVR架构板卡(如Uno、Nano)无此函数。
解决方法:
- 若使用AVR板卡,直接删除以下代码:
Serial.println(F("PWM supported: ")); Serial.println(analogWriteResolution(12)); //checking PWM support for pin 12 - 若使用支持该函数的板卡,确认工具菜单中已选择正确的开发板型号
额外代码补全
代码中audio4至audio10未定义,需补全这些音频数组的内容,否则编译仍会报错:
const char audio4[] PROGMEM = { ... }; const char audio5[] PROGMEM = { ... }; // ... 依次补全到audio10
修复后示例代码
#include <PCM.h> #include <avr/pgmspace.h> const int numButtons = 10; const int buttons[numButtons] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; const int numAudioFiles = 10; // 补全所有音频数组定义 const char audio1[] PROGMEM = { ... }; const char audio2[] PROGMEM = { ... }; const char audio3[] PROGMEM = { ... }; const char audio4[] PROGMEM = { ... }; const char audio5[] PROGMEM = { ... }; const char audio6[] PROGMEM = { ... }; const char audio7[] PROGMEM = { ... }; const char audio8[] PROGMEM = { ... }; const char audio9[] PROGMEM = { ... }; const char audio10[] PROGMEM = { ... }; const char* audioFiles[numAudioFiles] = {audio1, audio2, audio3, audio4, audio5, audio6, audio7, audio8, audio9, audio10}; PCM player; void setup() { for (int i = 0; i < numButtons; i++) { pinMode(buttons[i], INPUT); } player.setPin(12); player.begin(); Serial.begin(9600); // 如需串口输出,需添加初始化代码 } void loop() { for (int i = 0; i < numButtons; i++) { if (digitalRead(buttons[i]) == HIGH) { player.play_P(audioFiles[i]); while (player.isPlaying()); } } }
内容的提问来源于stack exchange,提问作者Akshay Dixit
相关产品推荐
相关产品推荐

