Created
November 16, 2020 17:09
-
-
Save brvdboss/9d3a2460cfbea6b264cfe2f95c626164 to your computer and use it in GitHub Desktop.
Arduino code to read out digital dial indicator with data output port
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int i; | |
int sign; | |
long value; | |
float result; | |
int clockpin = 4; | |
int datapin = 5; | |
int type = 0; | |
#define INCH 1 | |
#define MM 0 | |
unsigned long tempmicros; | |
void setup() { | |
Serial.begin(115200); | |
pinMode(clockpin, INPUT); | |
pinMode(datapin, INPUT); | |
} | |
void loop() { | |
while (digitalRead(clockpin)==LOW) {} //if clock is HIGH wait until it turns to LOW | |
tempmicros=micros(); | |
while (digitalRead(clockpin)==HIGH) {} //wait for the end of the LOW pulse | |
if ((micros()-tempmicros)>500) { //if the LOW pulse was longer than 500 micros we are at the start of a new bit sequence | |
decode(); //decode the bit sequence | |
} | |
} | |
void decode() { | |
sign=1; | |
type=MM; | |
value=0; | |
long value2=0; | |
for (i=0;i<24;i++) { | |
while (digitalRead(clockpin)==HIGH) { } //wait until clock returns to HIGH- the first bit is not needed | |
while (digitalRead(clockpin)==LOW) {} //wait until clock returns to LOW | |
if (digitalRead(datapin)==HIGH) { | |
if (i<20) { | |
value|= 1<<i; | |
} else { | |
value2|= 1<<i; | |
} | |
if (i==20) { | |
sign=-1; | |
} | |
if (i==23) { | |
type=INCH; | |
} | |
} | |
} | |
result=value/100.00*sign; | |
if(type==INCH) { | |
result/=20; //inches include "halves" (and the binary value is thus higher order) | |
Serial.println(result,4); //print result with 4 decimals | |
//Serial.println("\""); | |
} else { | |
Serial.println(result,2); //print result with 2 decimals | |
//Serial.println("mm"); | |
} | |
//delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment