-
Notifications
You must be signed in to change notification settings - Fork 8
/
netzfrequenz.ino
85 lines (63 loc) · 1.21 KB
/
netzfrequenz.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
/**
* Bestimmung der Netzfrequenz
* Copyright 2015-2022 by Ralf Th. Pietsch <ratopi@abwesend.de>
* LICENSE: MIT
*/
// Activate for more log output
// #define DEBUG
#define PROBE_COUNT 50
#define MICROS 1000000.
// #define KORREKTUR 1.001341341
#define KORREKTUR 1.
int min;
int max;
int val;
int i, j;
float freq;
long start;
long end;
void setup()
{
Serial.begin( 9600 );
analogReference( INTERNAL );
Serial.println( "reading 10000 samples ..." );
for ( i = 0; i < 10000; i++ )
{
analogRead( A0 );
}
Serial.println( "waiting for first period");
period();
end = micros();
Serial.println( "starting..." );
}
void loop()
{
start = end;
min = 1024;
max = 0;
for ( i = 0; i < PROBE_COUNT; i++ )
{
period();
}
end = micros();
freq = ( ( PROBE_COUNT * MICROS * KORREKTUR ) / ( end - start ) );
#ifdef DEBUG
Serial.print( min );
Serial.print( " " );
Serial.print( max );
Serial.print( " " );
#endif
Serial.println( freq, 3 );
}
void period()
{
while ( analogReadPlus() > 50 );
while ( analogReadPlus() < 100 );
}
int analogReadPlus()
{
val = analogRead( A0 );
if ( val < min ) min = val;
if ( val > max ) max = val;
return val;
}