Hi,
as an Arduino beginner I am having difficulties with my project. The goal is to read four Mitutoyo dial indicators in experiments for measuring autogenous shrinkage of cement paste using corrugated tubes test method: Article on corrugated tube protocol
After doing some search I found rather many sources which i could use for building my project, like this in Instructables:
https://www.instructables.com/Interfacing-a-Digital-Micrometer-to-a-Microcontrol/
or this with more advanced codding by Roger Cheng:
Arduino Interface for Mitutoyo SPC Data Port
And of course there are multiple discussions in Arduino forum as well.
As basis for my code i choose the guide from Instructables since it met my level of coding understanding. I edited the code so it would read data from four Mitutoyo dial indicators connected to Arduino Mega board (using schematics from Instructables), which then is connected to laptop and the data is recorded with Excel using Data streamer add in.
Here is my code (please excuse me, I know it is not too elegant):
int req1 = 25; //mic REQ line goes to pin through q1 (arduino high pulls request line low)
int dat1 = 22; //mic Data line goes to pin
int clk1 = 23; //mic Clock line goes to pin
int req2 = 31; //mic REQ line goes to pin through q1 (arduino high pulls request line low)
int dat2 = 28; //mic Data line goes to pin
int clk2 = 29; //mic Clock line goes to pin
int req3 = 37; //mic REQ line goes to pin 5 through q1 (arduino high pulls request line low)
int dat3 = 34; //mic Data line goes to pin
int clk3 = 35; //mic Clock line goes to pin
int req4 = 43; //mic REQ line goes to pin through q1 (arduino high pulls request line low)
int dat4 = 40; //mic Data line goes to pin
int clk4 = 41; //mic Clock line goes to pin
int i = 0;
int j = 0;
int k = 0;
int signCh = 8;
int sign1 = 0;
int sign2 = 0;
int sign3 = 0;
int sign4 = 0;
int decimal;
float dpp;
int units;
byte mydata1[14];
byte mydata2[14];
byte mydata3[14];
byte mydata4[14];
String value_str;
long value_int; //was an int, could not measure over 32mm
float value1;
float value2;
float value3;
float value4;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(req1, OUTPUT);
pinMode(clk1, INPUT_PULLUP);
pinMode(dat1, INPUT_PULLUP);
digitalWrite(req1,LOW); // set request at high
pinMode(req2, OUTPUT);
pinMode(clk2, INPUT_PULLUP);
pinMode(dat2, INPUT_PULLUP);
digitalWrite(req2,LOW); // set request at high
pinMode(req3, OUTPUT);
pinMode(clk3, INPUT_PULLUP);
pinMode(dat3, INPUT_PULLUP);
digitalWrite(req3,LOW); // set request at high
pinMode(req4, OUTPUT);
pinMode(clk4, INPUT_PULLUP);
pinMode(dat4, INPUT_PULLUP);
digitalWrite(req4,LOW); // set request at high
}
void loop() {
// put your main code here, to run repeatedly:
// Input 1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
digitalWrite(req1, HIGH); // generate set request
for( i = 0; i < 13; i++ ) {
k = 0;
for (j = 0; j < 4; j++) {
while( digitalRead(clk1) == LOW) {
} // hold until clock is high
while( digitalRead(clk1) == HIGH) {
} // hold until clock is low
bitWrite(k, j, (digitalRead(dat1) & 0x1));
}
mydata1[i] = k;
}
sign1 = mydata1[4];
value_str = String(mydata1[5]) + String(mydata1[6]) + String(mydata1[7]) + String(mydata1[8] + String(mydata1[9] + String(mydata1[10]))) ;
decimal = mydata1[11];
units = mydata1[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;
value1 = value_int / dpp;
// Input 2 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
digitalWrite(req2, HIGH); // generate set request
for( i = 0; i < 13; i++ ) {
k = 0;
for (j = 0; j < 4; j++) {
while( digitalRead(clk2) == LOW) {
} // hold until clock is high
while( digitalRead(clk2) == HIGH) {
} // hold until clock is low
bitWrite(k, j, (digitalRead(dat2) & 0x1));
}
mydata2[i] = k;
}
sign2 = mydata2[4];
value_str = String(mydata2[5]) + String(mydata2[6]) + String(mydata2[7]) + String(mydata2[8] + String(mydata2[9] + String(mydata2[10]))) ;
decimal = mydata2[11];
units = mydata2[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;
value2 = value_int / dpp;
// Input 3 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
digitalWrite(req3, HIGH); // generate set request
for( i = 0; i < 13; i++ ) {
k = 0;
for (j = 0; j < 4; j++) {
while( digitalRead(clk3) == LOW) {
} // hold until clock is high
while( digitalRead(clk3) == HIGH) {
} // hold until clock is low
bitWrite(k, j, (digitalRead(dat3) & 0x1));
}
mydata3[i] = k;
}
sign3 = mydata3[4];
value_str = String(mydata3[5]) + String(mydata3[6]) + String(mydata3[7]) + String(mydata3[8] + String(mydata3[9] + String(mydata3[10]))) ;
decimal = mydata3[11];
units = mydata3[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;
value3 = value_int / dpp;
// Input 4 ////////////////////////////////////////////////////////////////////////////////////////////////
digitalWrite(req4, HIGH); // generate set request
for( i = 0; i < 13; i++ ) {
k = 0;
for (j = 0; j < 4; j++) {
while( digitalRead(clk4) == LOW) {
} // hold until clock is high
while( digitalRead(clk4) == HIGH) {
} // hold until clock is low
bitWrite(k, j, (digitalRead(dat4) & 0x1));
}
mydata4[i] = k;
}
sign4 = mydata4[4];
value_str = String(mydata4[5]) + String(mydata4[6]) + String(mydata4[7]) + String(mydata4[8] + String(mydata4[9] + String(mydata4[10]))) ;
decimal = mydata4[11];
units = mydata4[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;
value4 = value_int / dpp;
///////////////////////////////////////////////////////////////////////////
if (sign1 == 0) {
Serial.print(value1,decimal); // 1 caliper
Serial.print(",");
}
if (sign1 == 8) {
Serial.print("-"); Serial.print(value1,decimal);
Serial.print(",");
}
if (sign2 == 0) {
Serial.print(value2,decimal); // 2 caliper
Serial.print(",");
}
if (sign2 == 8) {
Serial.print(value2,decimal); // 2 caliper
Serial.print(",");
}
if (sign3 == 0) {
Serial.print(value3,decimal); // 3 caliper
Serial.print(",");
}
if (sign3 == 8) {
Serial.print("-"); Serial.println(value3,decimal);
}
if (sign4 == 0) {
Serial.println(value4,decimal); // 2 caliper
}
if (sign4 == 8) {
Serial.print("-"); Serial.println(value4,decimal);
}
digitalWrite(req1,LOW);
digitalWrite(req2,LOW);
digitalWrite(req3,LOW);
digitalWrite(req4,LOW);
delay(5000);
}
I must say that everything works fine in trials, but I have noticed a problem which might cause big issues for data reading of the experiment. I accidentally noticed that one indicator has bad contacts with data cable. It might seem that everything is connected but the indicator is not sending data and when it happens the serial stream stops. That is it stops for all other indicators as well, despite that they are well connected.
The issue is that the test should last 7 days or maybe up to 28 days in some cases and if connection accident would happen just in one dial the recording would be stopped and data for unknow period of time would be lost for all of rest working units.
If I understand correct the issue is in this part of code:
while( digitalRead(clk1) == LOW) {
} // hold until clock is high
while( digitalRead(clk1) == HIGH) {
} // hold until clock is low
Do I understand correct, if dial is disconnected Arduino waits permanently until clock signal is high? Is there way to go around this situation, e.g. with introducing waiting time for 1 sec and if no signal comes assign value as 0 and then go on with values from other dials.
I would appreciate your help and advises.