-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDigitalCaliper.ino
142 lines (111 loc) · 3.1 KB
/
DigitalCaliper.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
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
133
134
135
136
137
138
139
140
141
142
// include the library code:
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
// The shield uses the I2C SCL and SDA pins. On classic Arduinos
// this is Analog 4 and 5 so you can't use those for analogRead() anymore
// However, you can connect other I2C sensors to the I2C bus and share
// the I2C bus.
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
// These #defines make it easy to set the backlight color
#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7
// The pins the quadrature encoder is connected.
// We connect the second signal of the encoder to pin 4.
// int.1 is free for other use.
int in_a = 2;
int in_b = 3;
// The number of pulses per revolution
// depends on your index disc!!
unsigned int pulsesperturn = 32;
unsigned int pulsespercentimetre = 3;
// The total number of revolutions
int distance = 0;
// Initialize the counter
volatile int pulses = 0;
volatile int numInterrupts = 0;
volatile int numBInterrupts = 0;
volatile unsigned long lastTrigger = 0;
volatile unsigned long lastBTrigger = 0;
#define debounceTime 5 // debounce time in microseconds
void count() {
// This function is called by the interrupt
// If in_b is HIGH increment the counter
// otherwise decrement it
if (micros() - lastTrigger > debounceTime) {
numInterrupts++;
if (digitalRead(in_b)) {
pulses--;
}
else {
pulses++;
}
}
lastTrigger = micros();
}
void countB() {
if (micros() - lastBTrigger > debounceTime) {
numBInterrupts++;
if (digitalRead(in_a)) {
pulses++;
}
else {
pulses--;
}
}
lastBTrigger = micros();
}
void setup() {
Serial.begin(9600);
pinMode(in_a, INPUT);
pinMode(in_b, INPUT);
attachInterrupt(digitalPinToInterrupt(in_b), countB, FALLING);
attachInterrupt(digitalPinToInterrupt(in_a), count, FALLING);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.setBacklight(BLUE);
lcd.print("Distance: ");
}
unsigned long lastPrint = 0;
int printDelay = 500;
void loop() {
if (millis() - printDelay > lastPrint) {
// Convert Pulses to millimetres. Conversion factor experimentally determined
distance = (pulses * 107) / 58;
//int a = digitalRead(in_a);
//int b = digitalRead(in_b);
//Serial.print("A=");
//Serial.println(a?"High":"Low");
//Serial.print(" B=");
//Serial.println(b?"High":"Low");
Serial.print("ints: ");
Serial.print(numInterrupts);
Serial.print(" intB: ");
Serial.print(numBInterrupts);
Serial.print(" pulses: ");
Serial.print(pulses);
Serial.print(" distance: ");
Serial.println(distance);
lcd.setBacklight(random(1,8));
lcd.setCursor(10,0);
lcd.print(" ");
lcd.setCursor(10,0);
lcd.print(distance);
lcd.print(" mm");
lastPrint = millis();
}
uint8_t buttons = lcd.readButtons();
if (buttons & BUTTON_SELECT) {
lcd.clear();
lcd.print("Distance: ");
distance = 0;
pulses = 0;
numInterrupts = 0;
numBInterrupts = 0;
}
}