]> git.piffa.net Git - arduino/blob - books/ArduinoNextSteps-master/ArduinoNextSteps/timer_interrupt_example/timer_interrupt_example.ino
quinta
[arduino] / books / ArduinoNextSteps-master / ArduinoNextSteps / timer_interrupt_example / timer_interrupt_example.ino
1 #include <TimerOne.h>
2
3 double v[11];
4  
5 void setup() 
6 {
7   // Initialize the digital pin as an output.
8   // Pin 13 has an LED connected on most Arduino boards
9   pinMode(12, OUTPUT);    
10   initFilter();
11   Serial.begin(115200);
12   Timer1.initialize(5000); 
13   Timer1.attachInterrupt( timerIsr ); // attach the service routine here
14 }
15  
16 void loop()
17 {
18   // Main code loop
19   // TODO: Put your regular (non-ISR) logic here
20 }
21  
22 /// --------------------------
23 /// Custom ISR Timer Routine
24 /// --------------------------
25 void timerIsr()
26 {
27     digitalWrite(12, HIGH);
28     int raw = analogRead(A0);
29     double input = (double)raw;
30     double filtered = filterStep(input);
31     Serial.print(input);
32     Serial.print(" ");
33     Serial.println(filtered);
34     digitalWrite(12, LOW);
35 }
36
37 // Sample f 200Hz, band stop 49-50Hz
38 // http://www.schwietering.com/jayduino/filtuino/index.php?characteristic=bu&passmode=bs&order=3&usesr=usesr&sr=200&frequencyLow=47&noteLow=&frequencyHigh=53&noteHigh=&pw=pw&calctype=double&run=Send
39 // Band stop butterworth filter order=3 alpha1=0.235 alpha2=0.265
40 void initFilter()
41 {
42   for(int i=0; i <= 10; i++)
43   {
44     v[i]=0.0;
45   }
46 }
47
48
49 double filterStep(double x)
50 {
51                         v[0] = v[1];
52                         v[1] = v[2];
53                         v[2] = v[3];
54                         v[3] = v[4];
55                         v[4] = v[5];
56                         v[5] = v[6];
57                         v[6] = (8.27971295622e-1 * x)
58                                  + ( -0.6855359773 * v[0])
59                                  + ( -0.0000000000 * v[1])
60                                  + ( -2.3146825811 * v[2])
61                                  + ( -0.0000000000 * v[3])
62                                  + ( -2.6235518066 * v[4])
63                                  + ( -0.0000000000 * v[5]);
64                         return 
65                                  (v[0] + v[6])
66                                 +3 * (v[2] + v[4]);
67 }
68
69