BASIC4MCU | 질문게시판 | 답변 : 아두이노 수동부저와 led제어
페이지 정보
작성자 master 작성일2018-06-19 10:51 조회11,724회 댓글0건
https://www.basic4mcu.com/bbs/board.php?bo_table=gac&wr_id=2006
본문
// MCU BASIC: https://www.basic4mcu.com// DateTime : 2018-06-19 오전 10:51:17// by Ok-Hyun Park//#define NOTE_C5 523 //도#define NOTE_D5 587 //레#define NOTE_E5 659 //미#define NOTE_F5 698 //파#define NOTE_G5 784 //솔#define NOTE_A5 880 //라#define NOTE_B5 988 //시#define NOTE_C6 1047 //도<<계이름 설정int tonepin=8;int led1=4;int led2=5;int led3=6;int led4=7;//int melody[]={ //떳다 떳다 비행기 계이름NOTE_E5,NOTE_D5,NOTE_C5,NOTE_D5,NOTE_E5,NOTE_E5,NOTE_E5,//미레도레미미미NOTE_D5,NOTE_D5,NOTE_D5,//레레레NOTE_E5,NOTE_G5,NOTE_G5,//미솔솔NOTE_E5,NOTE_D5,NOTE_C5,NOTE_D5,NOTE_E5,NOTE_E5,NOTE_E5,//미레도레미미미NOTE_D5,NOTE_D5,NOTE_E5,NOTE_D5,NOTE_C5,//레레미레도};int noteDurations[]={ //박자 계산 2분음표,4분음표,8분음표2,8,4,4,4,4,2,4,4,2,4,4,2,2,8,4,4,4,4,2,4,4,4,4,2};//void setup(){pinMode(led1,OUTPUT); pinMode(led2,OUTPUT); pinMode(led3,OUTPUT); pinMode(led4,OUTPUT);}//void loop(){if(digitalRead(melody[NOTE_C5])){ digitalWrite(led1,1); } else{ digitalWrite(led1,0); }if(digitalRead(melody[NOTE_D5])){ digitalWrite(led2,1); } else{ digitalWrite(led2,0); }if(digitalRead(melody[NOTE_E5])){ digitalWrite(led3,1); } else{ digitalWrite(led3,0); }if(digitalRead(melody[NOTE_F5])){ digitalWrite(led4,1); } else{ digitalWrite(led4,0); }//for(int i=0; i<49; i++){int Durations=1000/noteDurations[i]; //음계의 음길이 계산tone(tonepin,melody[i],Durations); delay(Durations*1.3); noTone(tonepin);}}이런 방식으로 LED를 구동하려면
LED 표시를 for()루프 안에서 해야합니다.
// MCU BASIC: https://www.basic4mcu.com// DateTime : 2018-06-19 오전 10:54:59// by Ok-Hyun Park//#define NOTE_C5 523 //도#define NOTE_D5 587 //레#define NOTE_E5 659 //미#define NOTE_F5 698 //파#define NOTE_G5 784 //솔#define NOTE_A5 880 //라#define NOTE_B5 988 //시#define NOTE_C6 1047 //도<<계이름 설정int tonepin=8;int led1=4;int led2=5;int led3=6;int led4=7;//int melody[]={ //떳다 떳다 비행기 계이름NOTE_E5,NOTE_D5,NOTE_C5,NOTE_D5,NOTE_E5,NOTE_E5,NOTE_E5,//미레도레미미미NOTE_D5,NOTE_D5,NOTE_D5,//레레레NOTE_E5,NOTE_G5,NOTE_G5,//미솔솔NOTE_E5,NOTE_D5,NOTE_C5,NOTE_D5,NOTE_E5,NOTE_E5,NOTE_E5,//미레도레미미미NOTE_D5,NOTE_D5,NOTE_E5,NOTE_D5,NOTE_C5,//레레미레도};int noteDurations[]={ //박자 계산 2분음표,4분음표,8분음표2,8,4,4,4,4,2,4,4,2,4,4,2,2,8,4,4,4,4,2,4,4,4,4,2};//void setup(){pinMode(led1,OUTPUT); pinMode(led2,OUTPUT); pinMode(led3,OUTPUT); pinMode(led4,OUTPUT);}//void loop(){for(int i=0; i<49; i++){if(melody[i]==NOTE_C5){ digitalWrite(led1,1); } else{ digitalWrite(led1,0); }if(melody[i]==NOTE_D5){ digitalWrite(led2,1); } else{ digitalWrite(led2,0); }if(melody[i]==NOTE_E5){ digitalWrite(led3,1); } else{ digitalWrite(led3,0); }if(melody[i]==NOTE_F5){ digitalWrite(led4,1); } else{ digitalWrite(led4,0); }int Durations=1000/noteDurations[i]; //음계의 음길이 계산tone(tonepin,melody[i],Durations); delay(Durations*1.3); noTone(tonepin);}}
댓글 0
조회수 11,724등록된 댓글이 없습니다.