1
5
6#include <Wire.h>
7#include "rgb_lcd.h"
8
9rgb_lcd lcd;
10
11const int colorR = 255;
12const int colorG = 0;
13const int colorB = 0;
14
15
16int sensorDig = 3;
17
18
19unsigned int updateRate = 500;
20
21
22unsigned int measureTime = 100;
23
24
25int state = 0;
26volatile unsigned int intEnable = 0;
27volatile unsigned long sensorDigCnt = 0;
28volatile int sensorAnalog = 0;
29unsigned long prevMillis = 0;
30
31
32
33void setup() {
34
35 lcd.begin(16, 2);
36 lcd.setRGB(colorR, colorG, colorB);
37
38 lcd.setCursor(0,0);
39 lcd.print("GROVE MAG SENSOR");
40
41
42 pinMode(sensorDig, INPUT);
43
44
45 attachInterrupt(digitalPinToInterrupt(sensorDig), sensorDigHandler, RISING);
46}
47
48
49void loop() {
50
51 unsigned long currMillis = millis();
52
53
54 switch(state){
55
56 case 1:
57
59
60 sensorDigCnt = (sensorDigCnt);
61
62
63 lcdUpdate(sensorDigCnt);
64
65
66 state = 0;
67
68
69 }
70
71
72 if(currMillis - prevMillis >= updateRate) {
73
74 sensorDigCnt = 0;
75
76
77 prevMillis = currMillis;
78
79
80 intEnable = 1;
81 delay(measureTime);
82 intEnable = 0;
83
84
85 state = 1;
86 }
87
88}
89
90
91void sensorDigHandler(){
92 if (intEnable==1)
93 sensorDigCnt++;
94}
95
96
97void lcdUpdate(unsigned long sensorD){
98
99 lcd.setCursor(0,1);
100 lcd.print(" ");
101 if(sensorD>9999){
102 lcd.print(sensorD);
103 }
104 else{
105 lcd.print(" ");
106 lcd.print(sensorD);
107 }
108}