-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced_timer.ino
More file actions
103 lines (84 loc) · 1.75 KB
/
advanced_timer.ino
File metadata and controls
103 lines (84 loc) · 1.75 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
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
#include <LiquidCrystal.h>
const int rs = PB11, en = PB10, d4 = PB0, d5 = PB1, d6 = PC13, d7 = PC14;
const int timer_period = 1000000;
const int interface_update_period = 300;
LiquidCrystal lcd( rs, en, d4, d5, d6, d7 );
volatile boolean isMeasuring = false;
volatile int currentInterval = 0;
volatile int bestInterval = 0;
void setup()
{
lcd.begin( 16, 2 );
lcd.print( "MC MIPT project" );
lcd.setCursor( 0, 1 );
lcd.print( "2023" );
delay( 1000 );
lcd.clear();
lcd.print( "by: Tsedrik" );
lcd.setCursor( 0, 1 );
lcd.print( "Vikhlyantsev" );
delay( 2000 );
pinMode( PA1, OUTPUT );
pinMode( PA0, INPUT );
attachInterrupt( PA0, onButtonPush, FALLING );
digitalWrite( PA1, LOW );
Timer3.pause();
Timer3.setPeriod( timer_period );
Timer3.attachInterrupt( TIMER_UPDATE_INTERRUPT, onTimerUpdate );
lcd.clear();
}
void loop()
{
if( isMeasuring )
{
lcd.clear();
lcd.print( "Time:" );
lcd.print( currentInterval );
}
else
{
lcd.clear();
lcd.print( "Loop:" );
lcd.print( currentInterval );
if( bestInterval )
{
lcd.setCursor( 0, 1 );
lcd.print( "Best:" );
lcd.print( bestInterval );
}
}
delay( interface_update_period );
}
void onButtonPush()
{
if( isMeasuring )
{
endMeasurement();
}
else
{
startMeasurement();
}
}
void onTimerUpdate()
{
++currentInterval;
}
void startMeasurement()
{
Timer3.refresh();
Timer3.resume();
currentInterval = 0;
isMeasuring = true;
digitalWrite( PA1, HIGH );
}
void endMeasurement()
{
Timer3.pause();
isMeasuring = false;
digitalWrite( PA1, LOW );
if( !bestInterval || currentInterval < bestInterval )
{
bestInterval = currentInterval;
}
}