アニメ『TERRAFORMARS』(2014)
見ててテンポが悪いなあとは思いますが、昆虫に詳しくなるかも。ちなみに私は昆虫好きではないです。でも人の生活に欠かせない虫もいますね。虫がいなかったら、はちみつもシルクもありませんし、ハエだって益虫の素質あります。虫を研究することで人の生活も豊かになっていきますね。
もうクーラー付けなくても涼しくなり夜寝やすくなりましたね。しかし窓を開けていると、蚊が入ってきます。まだいますかっ。蚊取り線香やら、ベープマットやらアースマットやら焚くのは、部屋に匂いがこもるのが嫌なんですよねえ。無臭って書いてあっても、無臭の匂いがするんです。しかし対策せずに寝てしまうと、耳元で蚊の羽音がうるさくて起きてしまいます。
そこで匂いのしない音で、蚊を撃退する装置を作ろうと思います。蚊の天敵、トンボの羽音の周波数を出力する装置を作ります。今回は周波数を出力するだけですので、効果のほどはわかりませんけど。(たぶん効果は、ないと思います)
まずは回路。「Raspberry PiからAVR ATtiny85にIntel HEXを書き込む」のLEDの点灯確認をした回路のLEDを圧電スピーカーにしただけの適当回路です。使用するのは、同様にAVRTiny85マイコンです。プログラムの書き込みはラズパイから行います。
avr用にC言語をコンパイルするためにgccと他ツールをRaspberry PIにインストールします。
root@raspberrypi:~# apt-get install gcc-var binutils-avr avr-libc
トンボの音を作る前に、ちゃんとスピーカーが機能するか確認します。ドレミ〜を鳴らすプログラムを作りました。
root@raspberrypi:~# cat -n doremi.c
1 #include <avr/io.h>
2 #include <util/delay.h>
3
4 #define OUTPORT 3
5 #define C 261.626 /* ド */
6 #define D 293.665 /* レ */
7 #define E 329.628 /* ミ */
8 #define F 349.228 /* ファ*/
9 #define G 391.995 /* ソ*/
10 #define A 440 /* ラ*/
11 #define B 493.883 /* シ*/
12 #define C2 523.251 /* ド */
13
14 #define OTO(freq) do{ \
15 int i,j; \
16 for(j=0;j<10;j++){ \
17 for(i=0;i<(freq/10);i++){ \
18 PORTB ^= _BV(OUTPORT); \
19 PORTB ^= _BV(OUTPORT); \
20 _delay_us(1000000/freq);\
21 } \
22 } \
23 }while(0)
24
25 int main()
26 {
27 DDRB = _BV(OUTPORT);
28
29 OTO(C);
30 OTO(D);
31 OTO(E);
32 OTO(F);
33 OTO(G);
34 OTO(A);
35 OTO(B);
36 OTO(C2);
37
38 while(1)
39 ;
40 }
プログラムができたら、ビルドしてHEXを作りますが、最初にmakefileを作っておくと楽です。
root@raspberrypi:~# cat -n makefile
1 all:
2 avr-gcc -g -O2 -mmcu=attiny85 -DF_CPU=1000000UL -c -o sound.o sound.c
3 avr-gcc -g -O2 -mmcu=attiny85 sound.o -o sound
4 avr-objcopy -j .text -j .data -O ihex sound sound.hex
5
6 write:
7 avrdude -p t85 -c linuxgpio -U flash:w:sound.hex
ビルドします。
root@raspberrypi:~# make avr-gcc -g -O2 -mmcu=attiny85 -DF_CPU=1000000UL -c -o sound.o sound.c avr-gcc -g -O2 -mmcu=attiny85 sound.o -o sound avr-objcopy -j .text -j .data -O ihex sound sound.hex root@raspberrypi:~#
マイコンへライトします。
root@raspberrypi:~# make write root@raspberrypi:~#
焼いたAVRマイコンを冒頭にあるブレッドボードの回路で電池を繋げると、ドレミファソラシドの音階が聞こえてきました。ちゃんと鳴るようです。
では蚊の天敵トンボの羽音周波数を作ります。(羽音そのものではありません)
周波数については、
http://nakaikemi.com/konchufqgakki.htm
を参考にさせていただきました。トンボは20〜30Hzらしいので、真ん中の25Hzでスピーカーを鳴らすプログラムを作ります。
root@raspberrypi:~# cp sound.c dragon.c
root@raspberrypi:~# vi dragon.c
#include <avr/io.h>
#include <util/delay.h>
#define OUTPORT 3
#define DRAGON 25 /* トンボ */
#define OTO(freq) do{ \
int i,j; \
for(j=0;j<10;j++){ \
for(i=0;i<(freq/10);i++){ \
PORTB ^= _BV(OUTPORT); \
PORTB ^= _BV(OUTPORT); \
_delay_us(1000000/freq);\
} \
} \
}while(0)
int main()
{
DDRB = _BV(OUTPORT);
while(1)
OTO(DRAGON);
}
root@raspberrypi:~# cp makefile makefile-dragon
root@raspberrypi:~# sed 's/sound/dragon/g' -i makefile-dragon
root@raspberrypi:~#
ビルドします。
root@raspberrypi:~# make -f makefile-dragon avr-gcc -g -O2 -mmcu=attiny85 -DF_CPU=1000000UL -c -o dragon.o dragon.c avr-gcc -g -O2 -mmcu=attiny85 dragon.o -o dragon avr-objcopy -j .text -j .data -O ihex dragon dragon.hex root@raspberrypi:~#
マイコンにライトします。
root@raspberrypi:~# make -f makefile-dragon write
avrdude -p t85 -c linuxgpio -U flash:w:dragon.hex
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.00s
avrdude: Device signature = 0x1e930b
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "dragon.hex"
avrdude: input file dragon.hex auto detected as Intel Hex
avrdude: writing flash (148 bytes):
Writing | ################################################## | 100% 0.08s
avrdude: 148 bytes of flash written
avrdude: verifying flash memory against dragon.hex:
avrdude: load data flash data from input file dragon.hex:
avrdude: input file dragon.hex auto detected as Intel Hex
avrdude: input file dragon.hex contains 148 bytes
avrdude: reading on-chip flash data:
Reading | ################################################## | 100% 0.07s
avrdude: verifying ...
avrdude: 148 bytes of flash verified
avrdude: safemode: Fuses OK (E:FF, H:DF, L:62)
avrdude done. Thank you.
root@raspberrypi:~#
さて、25Hzの音ですが、 リアルに時々鳴るので、何かの昆虫が窓にひっかかってるかと思いました。ということで音が気になり過ぎて、効果確認は結局断念。
トンボは私の天敵でした。



コメント
[…] ← AVRマイコンでトンボの羽根の周波数 […]