-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudioanalyzer.ino
82 lines (71 loc) · 2.64 KB
/
audioanalyzer.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
//Audioanalyzer
int main(void){
char audiopin = A0;
int audio = 0;
int schwelle = 50;
int arrayLength = 1024;
String audioString = "";
boolean toggle1 = 0; //storage variable
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(13, OUTPUT);
// SRC: https://www.instructables.com/id/Arduino-Timer-Interrupts/
cli(); //stop interrupts
//set timer0 interrupt at 40kHz
TCCR0A = 0; // set entire TCCR0A register to 0
TCCR0B = 0; // same for TCCR0B
TCNT0 = 0; //initialize counter value to 0
// set compare match register for 40khz increments
OCR0A = 49; // = (16*10^6) / (40000*8) - 1 (must be <256)
TCCR0A |= (1 << WGM01); // turn on CTC mode
TCCR0B |= (1 << CS01); // Set CS01(=1) bit for 8 prescaler
TIMSK0 |= (1 << OCIE0A); // enable timer compare interrupt
// set timer1 interrupt at 1Hz
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
TCNT1 = 0; // initialize counter value to 0
// set compare match register for 1hz increments
OCR1A = 15624; // = (16*10^6) / (1*1024) - 1 (must be <65536)
TCCR1B |= (1 << WGM12); // turn on CTC mode
TCCR1B |= (1 << CS12) | (1 << CS10); // Set CS10(=0) and CS12(=2) bits for 1024 prescaler
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
//DDRC = DDRC | B11111110; // Set Pin A0 input
sei(); // allow interrupts
//*************************Cyclic*Code******************
while(1) {
if (toggle1){
// Every Second
}
audio = analogRead(audiopin);
audioString = "";
int i = 0;
while (i < audio/6){
audioString.concat("|");
i++;
}
if (audio > schwelle){
digitalWrite(2,HIGH);
}
else{
digitalWrite(2,LOW);
}
//Serial.println(audioString);
}
}
//*************************ISRs**************************
// Interrupt-handler timer0 @40kHz
ISR(TIMER0_COMPA_vect){
// Read A1
}
// Interrupt-handler timer1 @1Hz
ISR(TIMER1_COMPA_vect){
//generates pulse wave of frequency 1Hz/2 = 0.5kHz (takes two cycles for full wave- toggle high then toggle low)
if (toggle1){
digitalWrite(13,HIGH);
toggle1 = 0;
}
else{
digitalWrite(13,LOW);
toggle1 = 1;
}
}