본문 바로가기

교육 자료/Raduino mini

7. RGB + SOUND

RGB + SOUND 기능이 더해진 라두이노 미니 드론 



  • 준비물 






  • 회로도



  • 코드 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "pitches.h"
 
#define RED PB0
#define GREEN PB1
#define BLUE PB3
#define SOUND PA0
  
int melody[] = {
NOTE_E4,NOTE_E4,NOTE_F4,NOTE_G4,
NOTE_G4,NOTE_F4,NOTE_E4,NOTE_D4,
NOTE_C4,NOTE_C4,NOTE_D4,NOTE_E4,
NOTE_E4,NOTE_D4,NOTE_D4,
NOTE_E4,NOTE_E4,NOTE_F4,NOTE_G4,
NOTE_G4,NOTE_F4,NOTE_E4,NOTE_D4,
NOTE_C4,NOTE_C4,NOTE_D4,NOTE_E4,
NOTE_D4,NOTE_C4,NOTE_C4,
NOTE_D4,NOTE_D4,NOTE_E4,NOTE_C4,
NOTE_D4,NOTE_F4,NOTE_E4,NOTE_C4,
NOTE_D4,NOTE_F4,NOTE_E4,NOTE_D4,
NOTE_C4,NOTE_D4,0,
NOTE_E4,NOTE_E4,NOTE_F4,NOTE_G4,
NOTE_G4,NOTE_F4,NOTE_E4,NOTE_D4,
NOTE_C4,NOTE_C4,NOTE_D4,NOTE_E4,
NOTE_D4,NOTE_C4,NOTE_C4,
};   // 멜로디를 배열로 저장함

int noteDurations[] = {
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,2,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,2,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,2,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,2,
};      //해당 음의 박자 수      
 
 
// We'll use timer 1
HardwareTimer timer(1);
int count=0;
 
void setup() {
    pinMode(RED, OUTPUT);
    pinMode(GREEN, OUTPUT);
    pinMode(BLUE, OUTPUT);
    pinMode(SOUND, OUTPUT);
    // Pause the timer while we're configuring it
    timer.pause();
 
    // Set up period
    timer.setPeriod(1000000); // in 1 microseconds
 
    // Set up an interrupt on channel 1
    timer.setChannel1Mode(TIMER_OUTPUT_COMPARE);
    timer.setCompare(TIMER_CH1, 1);  // Interrupt 1 count after each update
    timer.attachCompare1Interrupt(handler_1ms);
 
    // Refresh the timer's count, prescale, and overflow
    timer.refresh();
 
    // Start the timer counting
    timer.resume();    
}
 
void loop() {
    for (int thisNote = 0; thisNote < 60; thisNote++) {    
    int noteDuration = 1800/noteDurations[thisNote];  // 박자 시간 변환 
    tone(SOUND, melody[thisNote],noteDuration);     
    int pauseBetweenNotes = noteDuration ; // 음 길이를 계산   
    delay(pauseBetweenNotes);
    noTone(SOUND);           // 음 출력 중지
  }
}
 
void handler_1ms(void) {
  if(count == 0) { digitalWrite(RED, LOW); digitalWrite(GREEN, HIGH); digitalWrite(BLUE,HIGH);}
  if(count == 1) { digitalWrite(RED, LOW); digitalWrite(GREEN, LOW); digitalWrite(BLUE,HIGH);}
  if(count == 2) { digitalWrite(RED, HIGH); digitalWrite(GREEN, LOW); digitalWrite(BLUE,HIGH);}
  if(count == 3) { digitalWrite(RED, LOW); digitalWrite(GREEN, HIGH); digitalWrite(BLUE,LOW);}
  if(count == 4) { digitalWrite(RED, HIGH); digitalWrite(GREEN, HIGH); digitalWrite(BLUE,LOW);}
  if(count == 5) { digitalWrite(RED, HIGH); digitalWrite(GREEN, LOW); digitalWrite(BLUE,LOW);}
  if(count == 6) { digitalWrite(RED, LOW); digitalWrite(GREEN, LOW); digitalWrite(BLUE,LOW);}
  count++;
  if(count == 7) {count=0;}
 
}
cs



※ 해당 헤더파일은 


↓↓↓ 


pitches.h


↑↑↑


다운로드 하시면 됩니다.  





  • 동영상




'교육 자료 > Raduino mini' 카테고리의 다른 글

6. SOUND  (0) 2018.10.01
5. RGB  (0) 2018.10.01
4. 라두이노 미니 조종  (0) 2018.09.21
3. 라두이노 미니 개발환경  (0) 2018.09.19
라두이노 미니 핀아웃 다이어그램  (0) 2018.09.19