-
Notifications
You must be signed in to change notification settings - Fork 0
/
piano.ino
146 lines (134 loc) · 3.27 KB
/
piano.ino
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* TinyTone for ATtiny85 */
// Notes
const int Note_C = 239;
const int Note_CS = 225;
const int Note_D = 213;
const int Note_DS = 201;
const int Note_E = 190;
const int Note_F = 179;
const int Note_FS = 169;
const int Note_G = 159;
const int Note_GS = 150;
const int Note_A = 142;
const int Note_AS = 134;
const int Note_B = 127;
int Speaker = 1;
int octave = 4;
int noise = 0;
int rep = 4;
void TinyTone(unsigned char divisor, unsigned char octave, unsigned long duration)
{
TCCR1 = 0x90 | (8-octave); // for 1MHz clock
// TCCR1 = 0x90 | (11-octave); // for 8MHz clock
OCR1C = divisor-1; // set the OCR
//delay(duration);
//TCCR1 = 0x90; // stop the counter
}
void setup()
{
pinMode(Speaker, OUTPUT);
//pinMode(0, INPUT);
TinyTone(Note_B, octave, 500);
delay(500);
//note(1, 4)
}
int nDuration = 50;
int error = 17;
void loop()
{
//playTune();
//delay(1000);
int sensor3 = analogRead(A3);
int sensor4 = analogRead(A2);
if (sensor3 > 167 - error && sensor3 <= 169 + error){
//if (noise < rep){
//noise += 1;
//} else {
TinyTone(Note_C, octave, 10);
//}
} else if (sensor3 > 200 - error && sensor3 < 202 + error){
//if (noise < rep){
//noise += 1;
//} else {
TinyTone(Note_CS, octave, 10);
//}
} else if (sensor3 > 249 - error && sensor3 < 253 + error){
//if (noise < rep){
//noise += 1;
//} else {
TinyTone(Note_D, octave, 10);
//}
} else if (sensor3 > 335 - error && sensor3 < 339 + error){
//if (noise < rep){
//noise += 1;
//} else {
TinyTone(Note_DS, octave, 10);
//}
} else if (sensor3 > 497 - error && sensor3 < 507 + error){
//if (noise < rep){
//noise += 1;
//} else {
TinyTone(Note_E, octave, 10);
//}
} else if (sensor3 > 983 - error && sensor3 < 1011 + error){
//if (noise < rep){
//noise += 1;
//} else {
TinyTone(Note_F, octave, 10);
//}
} else if (sensor4 > 167 - error && sensor4 <= 169 + error){ //
if (noise < rep){
noise += 1;
} else {
TinyTone(Note_FS, octave, 10);
}
} else if (sensor4 > 200 - error && sensor4 < 202 + error){ //
if (noise < rep){
noise += 1;
} else {
TinyTone(Note_G, octave, 10);
}
} else if (sensor4 > 249 - error && sensor4 < 253 + error){
if (noise < rep){
noise += 1;
} else {
TinyTone(Note_GS, octave, 10);
}
} else if (sensor4 > 335 - error && sensor4 < 339 + error){
if (noise < rep){
noise += 1;
} else {
TinyTone(Note_A, octave, 10);
}
} else if (sensor4 > 497 - error && sensor4 < 507 + error){
if (noise < rep){
noise += 1;
} else {
TinyTone(Note_AS, octave, 10);
}
} else if (sensor4 > 983 - error && sensor4 < 1011 + error){
if (noise < rep){
noise += 1;
} else {
TinyTone(Note_B, octave, 10);
}
} else {
noise = 0;
//noTone(8);
TCCR1 = 0x90; // stop the counter
}
delay(5);
}
// Play a scale
void playTune(void) {
/*
TinyTone(Note_C, octave, 500);
TinyTone(Note_D, octave, 500);
TinyTone(Note_E, octave, 500);
TinyTone(Note_F, octave, 500);
TinyTone(Note_G, octave, 500);
TinyTone(Note_A, octave, 500);
TinyTone(Note_B, octave, 500);
TinyTone(Note_C, octave, 500);
*/
}