-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLaser_Tachometer_V1_0_0.ino
More file actions
132 lines (88 loc) · 2.63 KB
/
Copy pathLaser_Tachometer_V1_0_0.ino
File metadata and controls
132 lines (88 loc) · 2.63 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//Laser Tachometer by Elite Worm (YouTube.com/c/EliteWorm)
//Version 1.0.0
//Make sure you have all the libraries installed!
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const byte sensor = 2;
volatile unsigned long t_pulse_started_volatile = 0;
volatile unsigned long t_pulse_duration_volatile = 0;
unsigned long t_pulse_started = 0;
unsigned long t_pulse_duration = 0;
long rpm_sum = 0;
long rpm_reading[100];
long rpm_average = 0;
byte n_max = 0;
byte n = 0;
volatile bool timeout = 1;
volatile bool newpulse = 0;
void setup() {
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for(;;);
}
pinMode(sensor, INPUT);
attachInterrupt(digitalPinToInterrupt(sensor), ISR_sensor, RISING);
}
void loop() {
noInterrupts();
t_pulse_started = t_pulse_started_volatile;
t_pulse_duration = t_pulse_duration_volatile;
interrupts();
if(((micros() - t_pulse_started) > 2000000) && timeout == 0 && newpulse == 0) {
timeout = 1;
rpm_average = 0;
n = 0;
};
if(timeout == 0){
if(newpulse){
rpm_reading[n] = (60000000 / t_pulse_duration);
n_max = constrain(map(rpm_reading[n], 60, 100000, 0, 100), 0, 100);
n++;
newpulse = 0;
if(n > n_max){
for (byte i = 0; i <= n_max; i++) {
rpm_sum = rpm_sum + rpm_reading[i];
};
rpm_average = rpm_sum / (n_max + 1);
rpm_sum = 0;
n = 0;
}
}
}
updatedisplay();
}
void updatedisplay() {
byte x = 0;
display.clearDisplay();
display.setTextSize(3);
if(rpm_average < 10) x = 80;
if(rpm_average >= 10 && rpm_average < 100) x = 62;
if(rpm_average >= 100 && rpm_average < 1000) x = 44;
if(rpm_average >= 1000 && rpm_average < 10000) x = 26;
if(rpm_average >= 10000 && rpm_average < 100000) x = 8;
display.setTextColor(SSD1306_WHITE);
if(rpm_average < 100000){
display.setCursor(x, 6);
display.print(rpm_average);
display.setTextSize(1);
display.setCursor(104, 20);
display.print(F("RPM"));
display.display();
} else {
display.setTextSize(2);
display.setCursor(13, 8);
display.print(F("MAX LIMIT"));
display.display();
}
}
void ISR_sensor() {
t_pulse_duration_volatile = micros() - t_pulse_started_volatile;
t_pulse_started_volatile = micros();
timeout = 0;
newpulse = 1;
}