#概要
arduinoでAMラジオを鳴らしてみた。
#写真
#サンプルコード
#include "avr/pgmspace.h"
#define CLOCK (1000000/8)
#define UA 1
#define SILO 2
#define DO 3
#define DOSH 4
#define RE 5
#define RESH 6
#define MI 7
#define FA 8
#define FASH 9
#define SO 10
#define SOSH 11
#define LA 12
#define SIFL 13
#define SI 14
#define DOHI 15
#define DOHS 16
#define REHI 17
#define REHS 18
#define MIHI 19
#define FAHI 20
#define FAHS 21
#define END 0x00
#define OFF 0x01
#define T03 3
#define T06 6
#define T09 9
#define NOTE 32
int radioPin = 11;
const unsigned char musicData[] PROGMEM = {
DO, T03,
FA, T03,
FA, T09,
FA, T03,
MI, T03,
RE, T03,
DO, T03,
RE, T09,
DO, T03,
DO, T03,
UA, T03,
DO, T03,
RE, T06,
RE, T03,
RE, T03,
RE, T03,
FA, T03,
RE, T03,
MI, T06,
UA, T03,
OFF, T03,
UA, T03,
DO, T03,
RE, T06,
RE, T03,
FA, T03,
SO, T06,
FA, T03,
MI, T03,
FA, T06,
MI, T03,
RE, T03,
RE, T06,
DO, T03,
RE, T06,
RE, T03,
RE, T03,
LA, T03,
FA, T03,
LA, T03,
SO, T09,
OFF,T03,
DO, T03,
FA, T09,
FA, T03,
FA, T03,
MI, T03,
RE, T03,
DO, T03,
RE, T09,
DO, T03,
DO, T03,
UA, T06,
DO, T03,
RE, T03,
RE, T06,
RE, T03,
RE, T03,
FA, T03,
RE, T03,
MI, T03,
UA, T06,
OFF, T03,
UA, T03,
DO, T03,
RE, T06,
RE, T03,
FA, T03,
SO, T06,
FA, T03,
MI, T03,
FA, T06,
MI, T03,
RE, T03,
RE, T06,
DO, T03,
RE, T06,
RE, T03,
RE, T03,
LA, T03,
FA, T03,
LA, T03,
SO, T09,
END,T03
};
void playTone(byte tone, byte tempo)
{
int i,
hz,
itone;
switch (tone)
{
case UA:
hz = 466 / NOTE;
itone = ((int) (CLOCK / 466)) << 3;
break;
case SILO:
hz = 493 / NOTE;
itone = ((int) (CLOCK / 493)) << 3;
break;
case DO:
hz = 523 / NOTE;
itone = ((int) (CLOCK / 523)) << 3;
break;
case DOSH:
hz = 554 / NOTE;
itone = ((int) (CLOCK / 554)) << 3;
break;
case RE:
hz = 587 / NOTE;
itone = ((int) (CLOCK / 587)) << 3;
break;
case RESH:
hz = 622 / NOTE;
itone = ((int) (CLOCK / 622)) << 3;
break;
case MI:
hz = 659 / NOTE;
itone = ((int) (CLOCK / 659)) << 3;
break;
case FA:
hz = 698 / NOTE;
itone = ((int) (CLOCK / 698)) << 3;
break;
case FASH:
hz = 739 / NOTE;
itone = ((int) (CLOCK / 739)) << 3;
break;
case SO:
hz = 783 / NOTE;
itone = ((int) (CLOCK / 783)) << 3;
break;
case SOSH:
hz = 830 / NOTE;
itone = ((int) (CLOCK / 830)) << 3;
break;
case LA:
hz = 880 / NOTE;
itone = ((int) (CLOCK / 880)) << 3;
break;
case SIFL:
hz = 932 / NOTE;
itone = ((int) (CLOCK / 932)) << 3;
break;
case SI:
hz = 987 / NOTE;
itone = ((int) (CLOCK / 987)) << 3;
break;
case DOHI:
hz = 1046 / NOTE;
itone = ((int) (CLOCK / 1046)) << 3;
break;
case DOHS:
hz = 1108 / NOTE;
itone = ((int) (CLOCK / 1108)) << 3;
break;
case REHI:
hz = 1174 / NOTE;
itone = ((int) (CLOCK / 1174)) << 3;
break;
case REHS:
hz = 1244 / NOTE;
itone = ((int) (CLOCK / 1244)) << 3;
break;
case MIHI:
hz = 1318 / NOTE;
itone = ((int) (CLOCK / 1318)) << 3;
break;
case FAHI:
hz = 1396 / NOTE;
itone = ((int) (CLOCK / 1396)) << 3;
break;
case FAHS:
hz = 1480 / NOTE;
itone = ((int) (CLOCK / 1480)) << 3;
break;
default:
hz = 880 / NOTE;
itone = ((int) (CLOCK / 880)) << 3;
break;
}
for ( ; tempo > 0; tempo--)
{
if ((tone == OFF) || (tone == END))
{
delay(1000 / NOTE);
}
else
{
for (i = 0; i < hz; i++)
{
pinMode(radioPin, INPUT);
delayMicroseconds(itone);
pinMode(radioPin, OUTPUT);
delayMicroseconds(itone);
}
}
}
delay(50);
}
void setup()
{
pinMode(radioPin, OUTPUT);
digitalWrite(radioPin, HIGH);
TCCR2A = (1 << COM2A0) | (1 << WGM21);
TCCR2B = (1 << CS00);
OCR2A = 8 - 1;
}
void loop()
{
unsigned int i;
byte tone,
tempo;
i = 0;
tone = OFF;
while (tone != END)
{
tone = pgm_read_byte_near(musicData + i);
i++;
tempo = pgm_read_byte_near(musicData + i);
i++;
playTone(tone, tempo);
}
}
以上。