关于Arduino蜂鸣器电子琴的制作
需要准备的材料
1、LED灯3
2、开关3
3、有源或无源 蜂鸣器*3
4、面包板及其导线若干
连线图如下
代码如下
void setup()
{
pinMode(8 ,INPUT);
digitalWrite(8 ,HIGH);
pinMode(9 ,INPUT);
digitalWrite(9 ,HIGH);
pinMode(10 ,INPUT);
digitalWrite(10 ,HIGH);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
void loop()
{
if(digitalRead(10) == LOW)
{
tone(7, 2093);
digitalWrite(13, HIGH);
}
else
{
noTone(7);
digitalWrite(13, LOW);
}
if(digitalRead(9) == LOW)
{tone(7, 2349);digitalWrite(12, HIGH);
}
else
{noTone(7);digitalWrite(12, LOW);
}if(digitalRead(8) == LOW)
{tone(7, 2637);digitalWrite(11, HIGH);
}
else
{noTone(7);digitalWrite(11, LOW);
}
}