问题描述
我正在为arduino制作一个相当简单的声音脚本。这是我的脚本:
#include <digitalWriteFast.h>
struct AudioHandler {
//Alter these as you require
const int pin = 2;
float frequency = 1;
float dutyCycle = 1;
//These are internal variables that you shouldn't touch
unsigned long prevMicros = 0;
bool onHalf = true; //This just asks which half of the wave the sound is on
//This just checks the time and asks if it should switch to the other half of the wave
void updateSound() {
if ((micros() - prevMicros > (1000000 / frequency) * dutyCycle) && (onHalf)) {
prevMicros = micros();
onHalf = false;
digitalWriteFast(pin,false);
} else if ((micros() - prevMicros > (1000000 / frequency) * (1 - dutyCycle)) && (!onHalf)) {
prevMicros = micros();
onHalf = true;
digitalWriteFast(pin,true);
}
}
};
AudioHandler test_1;
AudioHandler test_2;
AudioHandler test_3;
AudioHandler test_4;
void setup() {
pinModeFast(3,OUTPUT); test_1.frequency = 65.41; test_1.dutyCycle = 0.5;
pinModeFast(4,OUTPUT); test_2.frequency = 82.41; test_2.dutyCycle = 0.5;
pinModeFast(5,OUTPUT); test_3.frequency = 98.00; test_3.dutyCycle = 0.5;
pinModeFast(6,OUTPUT); test_4.frequency = 130.81; test_4.dutyCycle = 0.5;
//I need to set the pin,but Couldn't figure out how to do it in struct declaration
//Also,this
int *pinPointer_1; pinPointer_1 = &test_1.pin; *pinPointer_1 = 3;
int *pinPointer_2; pinPointer_2 = &test_2.pin; *pinPointer_2 = 4;
int *pinPointer_3; pinPointer_3 = &test_3.pin; *pinPointer_3 = 5;
int *pinPointer_4; pinPointer_4 = &test_4.pin; *pinPointer_4 = 6;
}
void loop() {
test_1.updateSound();
test_2.updateSound();
test_3.updateSound();
test_4.updateSound();
}
实际上,我的脚本应该可以正常工作,但是arduino IDE抱怨:
Warning: Board breadboard:avr:atmega328bb doesn't define a 'build.board' preference. Auto-set to: AVR_ATMEGA328BB
C:\Users\aj200\Documents\GitHub\My-Projects\My-Projects\Active Projects\Sound_PWM_Help\Sound_PWM_Help.ino: In member function 'updateSound':
Sound_PWM_Help:19:7: error: call to 'NonConstantUsed' declared with attribute error:
digitalWriteFast(pin,false);
^
Sound_PWM_Help:24:7: error: call to 'NonConstantUsed' declared with attribute error:
digitalWriteFast(pin,true);
^
lto-wrapper.exe: Fatal error: C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-gcc returned 1 exit status
compilation terminated.
c:/program files (x86)/arduino/hardware/tools/avr/bin/../lib/gcc/avr/7.3.0/../../../../avr/bin/ld.exe: error: lto-wrapper Failed
collect2.exe: error: ld returned 1 exit status
exit status 1
call to 'NonConstantUsed' declared with attribute error:
从理论上讲,这应该是完整且实用的。但是,由于某种原因,arduino编译器会说变量“ pin”不是常数(烦人的digitalWriteFast(pin,state)需要常数)。我在这里想念什么?
先谢谢您, 安德烈
解决方法
为什么不创建一个结构构造函数来分配引脚。然后 setup
方法可以使用 pinModeFast()
这是我的看法。为 Arduino Uno 和 Leonardo 成功编译,但目前尚未测试。对我来说,它看起来更干净,更容易跟随和成长
#include <digitalWriteFast.h>
struct AudioHandler {
//Alter these as you require
int _pin;
float frequency;
float dutyCycle;
AudioHandler(int pin){
_pin = pin;
}
//These are internal variables that you shouldn't touch
unsigned long prevMicros = 0;
bool onHalf = true; //This just asks which half of the wave the sound is on
//This just checks the time and asks if it should switch to the other half of the wave
void updateSound() {
if ((micros() - prevMicros > (1000000 / frequency) * dutyCycle) && (onHalf)) {
prevMicros = micros();
onHalf = false;
digitalWriteFast(_pin,LOW); // Changed from false
} else if ((micros() - prevMicros > (1000000 / frequency) * (1 - dutyCycle)) && (!onHalf)) {
prevMicros = micros();
onHalf = true;
digitalWriteFast(_pin,HIGH); // Changed from true
}
}
void setup(int freq,int duty){
pinModeFast(_pin,OUTPUT);
frequency = freq;
dutyCycle = duty;
}
};
// Instantiate your audio handlers passing the pin number according to the constructor
AudioHandler test_1 = AudioHandler(3);
AudioHandler test_2 = AudioHandler(4);
AudioHandler test_3 = AudioHandler(5);
AudioHandler test_4 = AudioHandler(6);
void setup() {
// Set them up passing frequency and duty cycle
test_1.setup(65.41,0.5);
test_2.setup(82.41,0.5);
test_3.setup(98.00,0.5);
test_4.setup(130.81,0.5);
}
void loop() {
test_1.updateSound();
test_2.updateSound();
test_3.updateSound();
test_4.updateSound();
}