아두이노를 교육받았다.
이번회엔 시리얼 모니터와 볼륨, 부저 사용법을 익혔다.
볼륨의 3개의 핀중 가장자리는 vcc ,ground 중앙은 아날로그에 연결
부저(피에조 스피커)의 +는 출력에 -는 ground에 연결
시리얼 모니터로 출력하기.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);// 전송률 설정,9600bps
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available())
{
int value = Serial.read(); // 시리얼모니터 입력
Serial.println(value); // 시리얼모니터 출력
}
}
볼륨 사용하기
#define VR A0
void setup() {
// put your setup code here, to run once:
pinMode(VR,INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int value = analogRead(VR); //10bit 아날로그 값 가져오기
Serial.println(value);
}
볼륨에 따른 LED 켜기
#define LED 3
#define VR A0
void setup() {
// put your setup code here, to run once:
pinMode(VR,INPUT);
pinMode(LED,OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int value = analogRead(VR);
int value_led = map(value,0,1023,0,255);//0~1023를 0~255로 변환
Serial.print(value);
Serial.print("\t");
Serial.println(value_led);
if(value_led>220)
{
for(int i=0;i<5;i++)
{
analogWrite(LED,HIGH);
delay(100);
analogWrite(LED,LOW);
delay(100);
}
}
else
delay(500);
}
부저의 사용법
#include "piano.h"
#define PIEZO 8
#define VR A0
int melody[]={ mi, re, do, re, mi, mi, mi, re, re, re, mi, mi, mi, mi, re, do, re, mi, mi, mi, re, re, mi, re ,do};
int rythem[]={ 4, 4, 4, 4, 4, 4, 2, 4, 4, 2, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 1};
int melody1[]={ do, mi, mi, mi, sol, sol, re, fa, fa, ra, si, si, sol, do, ra, si, do1, re1, do1};
int rythem1[]={ 4, 4, 2, 4, 4, 2, 4, 4, 2, 4, 4, 2, 2, 2, 2, 2, 2, 2, 1};
void setup() {
// put your setup code here, to run once:
pinMode(PIEZO,OUTPUT);
pinMode(VR,INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int value = map(analogRead(VR),0,1023,0,255);
Serial.println(value);
if(value>220) miredo();
else domimi();
delay(5000);
}
void miredo(){
for(int i=0;i<25;i++)
{
int mel = melody[i];
int ry = 1000/rythem[i];
int pause = ry*1.3;
Serial.print(mel);
Serial.print("\t");
Serial.print(ry);
Serial.print("\t");
Serial.println(pause);
tone(PIEZO,mel,ry);
delay(pause);
}
}
void domimi(){
for(int i=0;i<20;i++)
{
int mel = melody1[i];
int ry = 1000/rythem1[i];
int pause = ry*1.3;
Serial.print(mel);
Serial.print("\t");
Serial.print(ry);
Serial.print("\t");
Serial.println(pause);
tone(PIEZO,mel,ry);
delay(pause);
}
}
응용 1 - 시리얼 모니터와 LED
#define LED 3
#define G 10
#define R 11
#define B 9
void setup() {
// put your setup code here, to run once:
pinMode(LED,OUTPUT);
pinMode(R,OUTPUT);
pinMode(G,OUTPUT);
pinMode(B,OUTPUT);
Serial.begin(9600);// 전송률 설정,9600bps
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available())
{
int value = Serial.read();
Serial.println(value);
digitalWrite(LED,HIGH);
if(value==49)
{
for(int i = 0;i<=255;i++)
{
analogWrite(R,i);
analogWrite(G,255-i);
analogWrite(B,i);
delay(1);
}
}
else if(value==48)
{
digitalWrite(LED,LOW);
for(int i = 255;i>=0;i--)
{
analogWrite(R,i);
analogWrite(G,i);
analogWrite(B,i);
delay(1);
}
}
}
}
응용 2 - RGB LED 와 볼륨
#define VR A0
#define G 10
#define R 11
#define B 9
void setup() {
// put your setup code here, to run once:
pinMode(VR,INPUT);
Serial.begin(9600);
pinMode(R,OUTPUT);
pinMode(G,OUTPUT);
pinMode(B,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int value = analogRead(VR)/4; //10bit 데이터 값 가져오기
Serial.println(value);
analogWrite(R,value/4);
analogWrite(G,value/2);
analogWrite(B,value);
}
응용 3 - 부저 + 볼륨
#define PIEZO 8
#define VR A0
void setup() {
// put your setup code here, to run once:
pinMode(PIEZO,OUTPUT);
pinMode(VR,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int value = analogRead(VR);
tone(PIEZO,value,100);
}