Skillnad mellan versioner av "Grove - Speaker"
Rad 55: | Rad 55: | ||
[[Category:Grove]] | [[Category:Grove]] | ||
+ | [[Category:Grove-Actuator]] | ||
[[Category:Kna-Wiki]] | [[Category:Kna-Wiki]] |
Nuvarande version från 4 februari 2021 kl. 07.42
Kompatibilitet
- Arduino
Port
- Digital
Exempelkod
Arduino
/*macro definition of Speaker pin*/
#define SPEAKER 3
int BassTab[]={1911,1702,1516,1431,1275,1136,1012};//bass 1~7
void setup()
{
pinInit();
}
void loop()
{
/*sound bass 1~7*/
for(int note_index=0;note_index<7;note_index++)
{
sound(note_index);
delay(500);
}
}
void pinInit()
{
pinMode(SPEAKER,OUTPUT);
digitalWrite(SPEAKER,LOW);
}
void sound(uint8_t note_index)
{
for(int i=0;i<100;i++)
{
digitalWrite(SPEAKER,HIGH);
delayMicroseconds(BassTab[note_index]);
digitalWrite(SPEAKER,LOW);
delayMicroseconds(BassTab[note_index]);
}
}