我正在尝试构建一个按下按钮时听起来像一个扬声器的电路板,我完全迷失了,而且我对arduino和编码很新。这是我的第一个项目,我正在没有教程或其他任何工作,所以如果编码有点乱,我很抱歉。提前谢谢!
#include "pitches.h"
int sensorPin = 0;
int val = 9;
int Buzzer1 = 2;
int buttonState1 = 1;
int buttonState2 = 5;
int buttonState3 = 6;
int buttonState4 = 7;
const int buttonPlay = 1;
const int buttonBlue = 5;
const int buttonGreen = 6;
const int buttonYellow = 7;
const int greenLight = 13;
const int yellowLight1 = 12;
const int yellowLight2 = 11;
const int redLight = 10;
int melody[] = { //SECRET SONG
NOTE_E5, NOTE_E3, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_D5, NOTE_C5,
NOTE_B4, NOTE_A4, NOTE_A3, NOTE_A4, NOTE_C5, NOTE_E5, NOTE_A3, NOTE_D5,
NOTE_C5, NOTE_B4, NOTE_E4, NOTE_G4, NOTE_C5, NOTE_D5, NOTE_E3, NOTE_E5,
NOTE_E3, NOTE_C5, NOTE_A3, NOTE_A4, NOTE_A3, NOTE_A4, NOTE_A3, NOTE_B2,
NOTE_C3, NOTE_D3, NOTE_D5, NOTE_F5, NOTE_A5, NOTE_C5, NOTE_C5, NOTE_G5,
NOTE_F5, NOTE_E5, NOTE_C3, 0, NOTE_C5, NOTE_E5, NOTE_A4, NOTE_G4, NOTE_D5,
NOTE_C5, NOTE_B4, NOTE_E4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_G4, NOTE_E5,
NOTE_G4, NOTE_C5, NOTE_E4, NOTE_A4, NOTE_E3, NOTE_A4, 0,
NOTE_E5, NOTE_E3, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_D5, NOTE_C5,
NOTE_B4, NOTE_A4, NOTE_A3, NOTE_A4, NOTE_C5, NOTE_E5, NOTE_A3, NOTE_D5,
NOTE_C5, NOTE_B4, NOTE_E4, NOTE_G4, NOTE_C5, NOTE_D5, NOTE_E3, NOTE_E5,
NOTE_E3, NOTE_C5, NOTE_A3, NOTE_A4, NOTE_A3, NOTE_A4, NOTE_A3, NOTE_B2,
NOTE_C3, NOTE_D3, NOTE_D5, NOTE_F5, NOTE_A5, NOTE_C5, NOTE_C5, NOTE_G5,
NOTE_F5, NOTE_E5, NOTE_C3, 0, NOTE_C5, NOTE_E5, NOTE_A4, NOTE_G4, NOTE_D5,
NOTE_C5, NOTE_B4, NOTE_E4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_G4, NOTE_E5,
NOTE_G4, NOTE_C5, NOTE_E4, NOTE_A4, NOTE_E3, NOTE_A4, 0,
NOTE_E4, NOTE_E3, NOTE_A2, NOTE_E3, NOTE_C4, NOTE_E3, NOTE_A2, NOTE_E3,
NOTE_D4, NOTE_E3, NOTE_GS2, NOTE_E3, NOTE_B3, NOTE_E3, NOTE_GS2, NOTE_E3,
NOTE_C4, NOTE_E3, NOTE_A2, NOTE_E3, NOTE_A3, NOTE_E3, NOTE_A2, NOTE_E3,
NOTE_GS3, NOTE_E3, NOTE_GS2, NOTE_E3, NOTE_B3, NOTE_E3, NOTE_GS2, NOTE_E3,
NOTE_E4, NOTE_E3, NOTE_A2, NOTE_E3, NOTE_C4, NOTE_E3, NOTE_A2, NOTE_E3,
NOTE_D4, NOTE_E3, NOTE_GS2, NOTE_E3, NOTE_B3, NOTE_E3, NOTE_GS2, NOTE_E3,
NOTE_C4, NOTE_E3, NOTE_E4, NOTE_E3, NOTE_A4, NOTE_E3, NOTE_A2, NOTE_E3,
NOTE_GS4, NOTE_E3, NOTE_GS2, NOTE_E3, NOTE_GS2, NOTE_E3, NOTE_GS2, NOTE_E3,
NOTE_E5, NOTE_E3, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_D5, NOTE_C5,
NOTE_B4, NOTE_A4, NOTE_A3, NOTE_A4, NOTE_C5, NOTE_E5, NOTE_A3, NOTE_D5,
NOTE_C5, NOTE_B4, NOTE_E4, NOTE_G4, NOTE_C5, NOTE_D5, NOTE_E3, NOTE_E5,
NOTE_E3, NOTE_C5, NOTE_A3, NOTE_A4, NOTE_A3, NOTE_A4, NOTE_A3, NOTE_B2,
NOTE_C3, NOTE_D3, NOTE_D5, NOTE_F5, NOTE_A5, NOTE_C5, NOTE_C5, NOTE_G5,
NOTE_F5, NOTE_E5, NOTE_C3, 0, NOTE_C5, NOTE_E5, NOTE_A4, NOTE_G4, NOTE_D5,
NOTE_C5, NOTE_B4, NOTE_E4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_G4, NOTE_E5,
NOTE_G4, NOTE_C5, NOTE_E4, NOTE_A4, NOTE_E3, NOTE_A4, 0,
NOTE_E5, NOTE_E3, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_D5, NOTE_C5,
NOTE_B4, NOTE_A4, NOTE_A3, NOTE_A4, NOTE_C5, NOTE_E5, NOTE_A3, NOTE_D5,
NOTE_C5, NOTE_B4, NOTE_E4, NOTE_G4, NOTE_C5, NOTE_D5, NOTE_E3, NOTE_E5,
NOTE_E3, NOTE_C5, NOTE_A3, NOTE_A4, NOTE_A3, NOTE_A4, NOTE_A3, NOTE_B2,
NOTE_C3, NOTE_D3, NOTE_D5, NOTE_F5, NOTE_A5, NOTE_C5, NOTE_C5, NOTE_G5,
NOTE_F5, NOTE_E5, NOTE_C3, 0, NOTE_C5, NOTE_E5, NOTE_A4, NOTE_G4, NOTE_D5,
NOTE_C5, NOTE_B4, NOTE_E4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_G4, NOTE_E5,
NOTE_G4, NOTE_C5, NOTE_E4, NOTE_A4, NOTE_E3, NOTE_A4, 0,
NOTE_E4, NOTE_E3, NOTE_A2, NOTE_E3, NOTE_C4, NOTE_E3, NOTE_A2, NOTE_E3,
NOTE_D4, NOTE_E3, NOTE_GS2, NOTE_E3, NOTE_B3, NOTE_E3, NOTE_GS2, NOTE_E3,
NOTE_C4, NOTE_E3, NOTE_A2, NOTE_E3, NOTE_A3, NOTE_E3, NOTE_A2, NOTE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8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
};
void setup() {
{ //OUTPUTS
pinMode(greenLight, OUTPUT); //Green Wire PIN13
pinMode(yellowLight1, OUTPUT); //Yellow Wire PIN12
pinMode(yellowLight2, OUTPUT); //Yellow Wire PIN11
pinMode(redLight, OUTPUT); //Yellow Wire PIN10
pinMode(Buzzer1, OUTPUT); //Black Wire PIN2
}
{ //INPUTS
pinMode(buttonPlay, INPUT); //Green Wire PIN1
pinMode(buttonBlue, INPUT); //Blue Wire PIN5
pinMode(buttonGreen, INPUT); //Green Wire PIN6
pinMode(buttonYellow, INPUT); //White Wire PIN7
}
}
void loop() {
{ //BUTTON STATES
buttonState1 = digitalRead(buttonPlay);
buttonState2 = digitalRead(buttonBlue);
buttonState3 = digitalRead(buttonGreen);
buttonState4 = digitalRead(buttonYellow);
}
{ //SECRET SONG
if(buttonState1 == LOW,buttonState3 == LOW){
for (int thisNote = 0; thisNote < 1000; thisNote++){
int noteDuration = 1000/noteDurations[thisNote];
tone(2, melody[thisNote],noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(2); //stop the tone playing:
}
digitalWrite(2,LOW);
}
}
{ //READPOTENTIOMETER
val = analogRead(sensorPin);
delay(10);
}
{ //BUTTONBLUE
if(buttonState2 == LOW){
tone(Buzzer1,200,100);
}
}
{ //BUTTONGREEN
if(buttonState3 == LOW){
if(val < 225){tone(Buzzer1,400,250);
}else{if(val >= 225,val < 510){tone(Buzzer1,350,225);
}}else{if(val >= 510,val < 735){tone(Buzzer1,300,200);
}}else{if(val >= 735){tone(Buzzer1,250,175);
}}
}
}
{ //BUTTONYELLOW
if(buttonState4 == LOW){
}
}
{ //LIGHTGREEN
if(val < 255){
digitalWrite(13,HIGH);
}
}
{ //LIGHTYELLOW1
if(val > 255){
digitalWrite(12,HIGH);
}
else{ digitalWrite(12,LOW);
}
}
{ //LIGHTYELLOW2
if(val > 510){
digitalWrite(11,HIGH);
}
else{ digitalWrite(11,LOW);
}
}
{ //LIGHTRED
if(val > 765){
digitalWrite(10,HIGH);
}
else{ digitalWrite(10,LOW);
}
}
}
完整错误代码:
Good_Meme.ino: In function 'void loop()':
Good_Meme:144: error: expected '}' before 'else'
}}else{if(val >= 510,val < 735){tone(Buzzer1,300,200);
^
Good_Meme:145: error: 'else' without a previous 'if'
}}else{if(val >= 735){tone(Buzzer1,250,175);
^
Good_Meme.ino: At global scope:
Good_Meme:150: error: expected unqualified-id before '{' token
{ //BUTTONYELLOW
^
expected '}' before 'else'
答案 0 :(得分:0)
简而言之, BUTTONGREEN 段中的if / else嵌套不正确。让我们阻止你的if
结构,以便更容易阅读...
if(buttonState3 == LOW)
{
if(val < 225)
{
tone(Buzzer1,400,250);
}
else
{
if(val >= 225,val < 510)
{
tone(Buzzer1,350,225);
}
}
else
{
if(val >= 510,val < 735)
{
tone(Buzzer1,300,200);
}
}
else
{
if(val >= 735)
{
tone(Buzzer1,250,175);
}
}
}
现在,正如你所看到的,你基本上是在说,“如果是这样的话,那么。还有别的。别的,还有别的。还有别的。”您在else
中只能有一个if
语句,因为它将被视为逻辑中的“最终解决方案”(如果其他所有方法都失败了,请执行[this])。
所以,我认为您要做的是重新格式化以使用else if
逻辑。我相信一个更正确的结构是:
if(buttonState3 == LOW)
{
if(val < 225)
{
tone(Buzzer1,400,250);
}
else if (val >= 225,val < 510)
{
tone(Buzzer1,350,225);
}
else if(val >= 510,val < 735)
{
tone(Buzzer1,300,200);
}
else if(val >= 735)
{
tone(Buzzer1,250,175);
}
}
但是!!! 在最后else if
条件下,您可以将其更改为else
并忽略{ {1}}。因为,在路的尽头,如果你已经走得那么远,你几乎可以肯定if(val >= 735)
。此外,您可以考虑删除val >= 735
条件的第一部分。因为,如果您处于第二个条件(else if
),则您已经知道(val >= 225, val < 510)
。所以,你应能够说:
val >= 225