我正在尝试从字符串中的字符创建哔声。这是代码:
/*
* Buzzer connected to Arduino uno digital pin 13
* Switch connected to digital pin 2
*/
#include <avr/io.h>
#include <util/delay.h>
const int TBEEP = 1000;
const int TBEEEEP = 3500;
const int TGAP = 500;
const int TGAPLETTER = 2000;
int portb = 0x20;
void beep() {
PORTB = ~portb; _delay_ms(TGAP);
PORTB = portb; _delay_ms(TBEEP);
PORTB = ~portb; _delay_ms(TGAP);
}
void beeeep() {
PORTB = ~portb; _delay_ms(TGAP);
PORTB = portb; _delay_ms(TBEEEEP);
PORTB = ~portb; _delay_ms(TGAP);
}
void gapLetter() {
PORTB = ~portb; _delay_ms(TGAPLETTER);
}
void morse_S() {
beep(); beep(); beep();
gapLetter();
}
void morse_M() {
beeeep(); beeeep();
gapLetter();
}
void morse_SMS() {
morse_S(); morse_M(); morse_S();
}
void morse(char theString[]) {
for (int i = 0; theString[i] != '\0'; i++)
{
if(&theString[i] == "S")
morse_S();
else if(&theString[i] == "M")
morse_M();
}
}
int main (void)
{
DDRB = 0xFF;
DDRD = 0x00;
PORTD = 0x04;
while (1) {
if (PIND & 0x04) {
PORTB = ~0x20;
} else {
//morse_SMS(); // it works
morse("SMS"); // this one doesnt work like morse_SMS() PLEASE HELP!
}
}
return 0;
}
在函数void morse(char theString[]) {...}
中,我想从字符串“SMS”中的每个字符产生哔声。不幸的是,只有最后一个角色可以做到。
我正在使用 Atmel Studio 6。当我构建解决方案 (F7) 时,没有错误,但有我不理解的警告(对不起,我是个菜鸟)
与字符串文字比较会导致未指定的行为 [-Waddress]
如何强制每个角色一个接一个地发出哔哔声?