https://www.instructables.com/id/Interfacing-a-Digital-Micrometer-to-a-Microcontrol/
Works perfectly well on Arduino Uno. Now I plan to have a set of measurement devices, each based on Wemos D1R2, sending data to a server through wifi.
Specifically, my current version is as follows:
#define Dest 2 //1=Arduino Uno; 2=Wemos D1R2&mini
#if Dest==1 //Uno
int req = 5; //mic REQ line goes to pin 5 through q1 (arduino high pulls request line low)
int dat = 2; //mic Data line goes to pin 2
int clk = 3; //mic Clock line goes to pin 3
#endif
#if Dest==2 //Wemos D1R2&mini
int req = D5; //mic REQ line goes to pin 5 through q1 (arduino high pulls request line low)
int dat = D2; //mic Data line goes to pin 2
int clk = D3; //mic Clock line goes to pin 3
#endif
int i = 0;
int j = 0;
int k = 0;
int signCh = 8;
int sign = 0;
int decimal;
float dpp;
int units;
byte mydata[14];
String value_str;
long value_int; //was an int, could not measure over 32mm
float value;
void setup() {//******************************************************************************
Serial.begin(9600);
Serial.println("setup start");
pinMode(req, OUTPUT);
pinMode(clk, INPUT_PULLUP);
pinMode(dat, INPUT_PULLUP);
digitalWrite(req,LOW); // set request at high
Serial.println("setup end");
}
void loop() { //******************************************************************************
Serial.println("loop start");
digitalWrite(req, HIGH); // generate set request
for( i = 0; i < 13; i++ ) {
k = 0;
for (j = 0; j < 4; j++) {
Serial.print("j="); Serial.println(j);
while( digitalRead(clk) == LOW) {
Serial.println("here");
} // hold until clock is high
while( digitalRead(clk) == HIGH) {
} // hold until clock is low
bitWrite(k, j, (digitalRead(dat) & 0x1));
}
mydata[i] = k;
}
sign = mydata[4];
value_str = String(mydata[5]) + String(mydata[6]) + String(mydata[7]) + String(mydata[8] + String(mydata[9] + String(mydata[10]))) ;
decimal = mydata[11];
units = mydata[12];
value_int = value_str.toInt();
if (decimal == 0) dpp = 1.0;
if (decimal == 1) dpp = 10.0;
if (decimal == 2) dpp = 100.0;
if (decimal == 3) dpp = 1000.0;
if (decimal == 4) dpp = 10000.0;
if (decimal == 5) dpp = 100000.0;
value = value_int / dpp;
if (sign == 0) {
Serial.println(value,decimal);
}
if (sign == 8) {Serial.print("-"); Serial.println(value,decimal);}
digitalWrite(req,LOW);
delay(100);
}
But when using the sketch on Wemos D1R2, I get "Soft WDT reset" errors as follows:
setup start
Soft WDT reset
ctx: cont
sp: 3ffef200 end: 3ffef460 offset: 01b0
>>>stack>>>
3ffef3b0: feefeffe 3ffee32c 3ffee40c 40201f64
3ffef3c0: 0000001c 00000000 3ffee40c 40100574
3ffef3d0: feefeffe 00000001 3ffe84a1 40202d44
3ffef3e0: 00000000 00000008 3ffe844c 3ffee430
3ffef3f0: 3fffdad0 00000009 3ffee40c 402023c5
3ffef400: 3ffe84a0 3ffe8360 3ffee40c 402023c5
3ffef410: 3ffe8444 feefeffe 3ffee40c 402023f0
3ffef420: feefeffe feefeffe 3ffee40c 40202414
3ffef430: 3fffdad0 3ffe8360 3ffee40c 3ffee430
3ffef440: 3fffdad0 00000000 3ffee428 40202b44
3ffef450: feefeffe feefeffe 3ffee440 40100114
<<<stack<<<
I understand there's a different pin layout/functionality on Wemos. I have tried some other pins but it did not help at all. Running out of options at the moment
Any hint would be very appreciated.
Regards
vqvq