forked from evankale/ArduinoMetalDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoMetalDetector.ino
More file actions
71 lines (58 loc) · 2.21 KB
/
ArduinoMetalDetector.ino
File metadata and controls
71 lines (58 loc) · 2.21 KB
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
/*
* Copyright (c) 2022 S. Zihlmann
*
* This file is part of ArduinoMetalDetector.
*
* ArduinoMetalDetector is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "src/Buzzer/Buzzer.h"
#include "src/BaselineCorrection/BaselineCorrection.h"
// Number of cycles from external counter needed to generate a signal event
// Larger number: Better resolution and longer aquisition time. Max value 255 (8 bit)
#define SIGNAL_CYCLES 5 // Multiplies by 512 (=2'560 cycles)
// Pin definitions
#define SPEAKER_PIN 10
#define LED_PIN 11
// Timer 0 - Colpitts counter
volatile uint8_t TCNT0_overflowCount = 0; // Incremented at every overflow of TIMER0_COMPB
volatile uint8_t newData = 0; // Incremented whenever aquisition cycle finished
// Timer 1 - The Clock
volatile unsigned long timer1_micros = 0;
volatile uint8_t TCNT1_overflowCount = 0;
// Buffer for total elapsed time when measuring colpitts frequency
volatile uint32_t signalTimeDelta;
// Create buzzer instance
Buzzer buzzer = Buzzer(SPEAKER_PIN, LED_PIN);
// Create baseline correction engine
BaselineCorrection baseline = BaselineCorrection();
void setup()
{
setup_timer0();
setup_timer1();
while (newData < 10); // Wait to assign a stable value
baseline.initialize(signalTimeDelta, Millis());
}
void loop()
{
unsigned long cTime = Millis();
if (newData){
newData = 0;
// Calculate difference in signal time
long TimeDelta = (long)(baseline.getValue()) - (long)(signalTimeDelta);
buzzer.update(TimeDelta);
// Update baseline
baseline.update(signalTimeDelta, cTime);
}
buzzer.tick(cTime);
}