|
| 1 | +/** |
| 2 | + Digital caliper -> VFD |
| 3 | +*/ |
| 4 | + |
| 5 | +#include <SPI_VFD.h> |
| 6 | + |
| 7 | +//uint8_t data, uint8_t clock, uint8_t strobe, |
| 8 | +SPI_VFD vfd(3, 6, 4); |
| 9 | + |
| 10 | +const int numRows = 2; |
| 11 | +const int numCols = 40; |
| 12 | + |
| 13 | +#define DC_CLK 11 |
| 14 | +#define DC_DATA 10 |
| 15 | + |
| 16 | +void setup() { |
| 17 | + Serial.begin(115200); |
| 18 | + |
| 19 | + // set up the VFD's number of columns and rows: |
| 20 | + vfd.begin(numCols, numRows); |
| 21 | + |
| 22 | + // Print a message to the VFD. |
| 23 | + vfd.setCursor(0, 0); |
| 24 | + vfd.print("Hello Digital Caliper"); |
| 25 | + vfd.setCursor(0, 1); |
| 26 | + vfd.print("Please wait 5 seconds..."); |
| 27 | + vfd.blink(); |
| 28 | + delay(5000); |
| 29 | + vfd.noBlink(); |
| 30 | + vfd.clear(); |
| 31 | +} |
| 32 | + |
| 33 | +int bit_array[25]; // For storing the data bit. bit_array[0] = data bit 1 (LSB), bit_array[23] = data bit 24 (MSB). |
| 34 | +float last = -100000.0; |
| 35 | + |
| 36 | +void loop() { |
| 37 | + long value = 0, power = 0; |
| 38 | + int inches = 0; |
| 39 | + while (digitalRead(DC_CLK) == LOW) {} |
| 40 | + // it went high, now wait for it to fall. |
| 41 | + for (int i = 1; i <= 24; ++i) { |
| 42 | + while (digitalRead(DC_CLK) == HIGH) {} |
| 43 | + // it fell, read the data. |
| 44 | + int data = 1 - digitalRead(DC_DATA); |
| 45 | + bit_array[i] = data; |
| 46 | + if (i > 1 && i < 21) { |
| 47 | + // shift data "power" bits to the left, add to value |
| 48 | + if (data) { |
| 49 | + value = value + (1L << power); |
| 50 | + } |
| 51 | + power++; |
| 52 | + } |
| 53 | + if (i == 21 && data == 1) { |
| 54 | + // sign |
| 55 | + value = value * -1; |
| 56 | + } |
| 57 | + if (i == 24) { |
| 58 | + inches = data; |
| 59 | + } |
| 60 | + // wait for clock to go low again |
| 61 | + while (digitalRead(DC_CLK) == LOW) {} |
| 62 | + } |
| 63 | + for (int i = 1; i <= 24; ++i) { |
| 64 | + // Serial.print(i); Serial.print(":"); Serial.println(bit_array[i]); |
| 65 | + } |
| 66 | + float actual = value; |
| 67 | + if (inches == 1) { |
| 68 | + actual = actual / 1000.0; |
| 69 | + } else { |
| 70 | + actual = actual / 50.0; |
| 71 | + } |
| 72 | + if (actual != last) { |
| 73 | + last = actual; |
| 74 | + vfd.clear(); |
| 75 | + vfd.setCursor(0, 0); |
| 76 | + vfd.print(actual, 2 + inches); |
| 77 | + vfd.setCursor(9, 0); |
| 78 | + if (inches == 1) { |
| 79 | + vfd.print("in"); |
| 80 | + } else { |
| 81 | + vfd.print("mm"); |
| 82 | + } |
| 83 | + } |
| 84 | + delay(100); |
| 85 | +} |
0 commit comments