Micrometer reader usb

Hello

I have a problem about read micrometer value , Now right i can read one micrometer value
connect to Arduino nana from this link http://www.instructables.com/id/Interfacing-a-Digital-Micrometer-to-a-Microcontrol/step3/Reading-the-Mitutoyo-output/ but i need to one more connecting ( 2 micrometer). I’m trying to write code for support my condition
but it’s not working.
Can you help me about code.

//*** Setup micrometer 1 *************
int req = 5; //mic11 REQ line goes to pin 5 
int dat = 2; //mic11 Data line goes to pin 2
int clk = 3; //mic11 Clock line goes to pin 3
int i = 0; 
int j = 0; 
int k = 0;
float Value;
byte mydata[14];
long num;
//*** Setup micrometer 2 *************
int req2 = 9; //mic2 REQ line goes to pin 9 
int dat2 = 6; //mic2 Data line goes to pin 6
int clk2 = 7; //mic2 Clock line goes to pin 7
int i2 = 0; 
int j2 = 0; 
int k2 = 0;
float Value2;
byte mydata2[14];
long num2;
void setup()

{
  Serial.begin(9600);
  //*** Setup micrometer 1 *************
  pinMode(req, OUTPUT);
  pinMode(clk, INPUT);
  pinMode(dat, INPUT);
  digitalWrite(clk, HIGH); // enable internal pull ups
  digitalWrite(dat, HIGH); // enable internal pull ups
  digitalWrite(req,LOW); // set request at LOW
  //*** Setup micrometer 2 *************
  pinMode(req2, OUTPUT);
  pinMode(clk2, INPUT);
  pinMode(dat2, INPUT);
  digitalWrite(clk2, HIGH); // enable internal pull ups
  digitalWrite(dat2, HIGH); // enable internal pull ups
  digitalWrite(req2,LOW); // set request at LOW
}

void loop()
{
  //***  Micrometer 1*********
  digitalWrite(req2, LOW); 
  digitalWrite(req, HIGH); // generate set request
  for( i = 0; i < 13; i++ ) {
    k = 0;
    for (j = 0; j < 4; j++) {
         while( digitalRead(clk) == LOW) { 
      } // hold until clock is high
      while( digitalRead(clk) == HIGH) { 
      } // hold until clock is low
        bitWrite(k, j, (digitalRead(dat) & 0x1)); // read data bits, and reverse order )
    }
    mydata[i] = k;
  }
  char buf[7];
  for(int lp=0;lp<6;lp++){
    buf[lp]=mydata[lp+5]+'0';
  }
  buf[6]=0;
  num=atol(buf); //assembled measurement, no decimal place added
  Value=num*0.001;
 Serial.println(Value);
 
  //***  Micrometer 1*********
  digitalWrite(req, LOW);   
  digitalWrite(req2, HIGH); // generate set request
  for( i2 = 0; i2 < 13; i2++ ) {
    k2 = 0;
    for (j2 = 0; j2 < 4; j2++) {   
       while( digitalRead(clk2) == LOW) { 
      } // hold until clock is high
      while( digitalRead(clk2) == HIGH) { 
      } // hold until clock is low
      bitWrite(k2, j2, (digitalRead(dat2) & 0x1)); // read data bits, and reverse order )
    }
    mydata2[i2] = k2;
  }
   //********************************************************
  char buf2[7];
  for(int lp2=0;lp2<6;lp2++)
  buf2[lp2]=mydata2[lp2+5]+'0';
  buf2[6]=0;
  num2=atol(buf2); //assembled measurement, no decimal place added
  Value2=num2*0.001;
  Serial.println(Value2);
}

I would suggest the code section that reads and decodes the Micrometer values is written as a function where you pass the pin numbers for req/dat/clk and it returns a float value representing the micrometer reading.

Something like below (Untested)

//*** Setup micrometer 1 *************
byte req = 5; //mic11 REQ line goes to pin 5 
byte dat = 2; //mic11 Data line goes to pin 2
byte clk = 3; //mic11 Clock line goes to pin 3
byte req2 = 9; //mic2 REQ line goes to pin 9 
byte dat2 = 6; //mic2 Data line goes to pin 6
byte clk2 = 7; //mic2 Clock line goes to pin 7

void setup()
{
  Serial.begin(9600);
  //*** Setup micrometer 1 *************
  pinMode(req, OUTPUT);
  pinMode(clk, INPUT_PULLUP);
  pinMode(dat, INPUT_PULLUP);
  digitalWrite(req,LOW); // set request at LOW
}

void loop(){
  float Value = readMicrometer(req,clk,dat);
  Serial.println(Value);
  Value = readMicrometer(req2,clk2,dat2);
  Serial.println(Value);
}

float readMicrometer(byte reqPin, byte clkPin, byte datPin){
  byte mydata[14];
  
  digitalWrite(reqPin, HIGH); // generate set request
  for( byte i = 0; i < 13; i++ ) {
    byte k = 0;
    for (byte j = 0; j < 4; j++) {
      while( digitalRead(clkPin) == LOW) {}; // hold until clock is high
      while( digitalRead(clkPin) == HIGH) {}; // hold until clock is low
      bitWrite(k, j, (digitalRead(datPin) & 0x1)); // read data bits, and reverse order )
    }
    mydata[i] = k;
  }
  char buf[7];
  for(byte lp=0;lp<6;lp++){
    buf[lp]=mydata[lp+5]+'0';
  }
  buf[6]=0;
  long num=atol(buf); //assembled measurement, no decimal place added
  return num*0.001;
}

Thank you so much Riva for your help, but it still not work it just read micrometer 1 and one time as picture below.

matsuo:
Thank you so much Riva for your help, but it still not work it just read micrometer 1 and one time as picture below.

It was not meant as a complete example but a pointer to how to do it. Below is code with (hopefully) all the extra bits needed.

//*** Setup micrometer 1 *************
byte req = 5; //mic11 REQ line goes to pin 5 
byte dat = 2; //mic11 Data line goes to pin 2
byte clk = 3; //mic11 Clock line goes to pin 3
byte req2 = 9; //mic2 REQ line goes to pin 9 
byte dat2 = 6; //mic2 Data line goes to pin 6
byte clk2 = 7; //mic2 Clock line goes to pin 7

void setup()
{
  Serial.begin(9600);
  //*** Setup micrometer 1 *************
  pinMode(req, OUTPUT);
  pinMode(clk, INPUT_PULLUP);
  pinMode(dat, INPUT_PULLUP);
  pinMode(req2, OUTPUT);
  pinMode(clk2, INPUT_PULLUP);
  pinMode(dat2, INPUT_PULLUP);
  digitalWrite(req,LOW); // set request at LOW
  digitalWrite(req2,LOW); // set request at LOW
}

void loop(){
  float Value = readMicrometer(req,clk,dat);
  Serial.println(Value);
  Value = readMicrometer(req2,clk2,dat2);
  Serial.println(Value);
}

float readMicrometer(byte reqPin, byte clkPin, byte datPin){
  byte mydata[14];
  
  digitalWrite(reqPin, HIGH); // generate set request
  for( byte i = 0; i < 13; i++ ) {
    byte k = 0;
    for (byte j = 0; j < 4; j++) {
      while( digitalRead(clkPin) == LOW) {}; // hold until clock is high
      while( digitalRead(clkPin) == HIGH) {}; // hold until clock is low
      bitWrite(k, j, (digitalRead(datPin) & 0x1)); // read data bits, and reverse order )
    }
    mydata[i] = k;
  }
  digitalWrite(reqPin, LOW); // clear set request
  
  char buf[7];
  for(byte lp=0;lp<6;lp++){
    buf[lp]=mydata[lp+5]+'0';
  }
  buf[6]=0;
  long num=atol(buf); //assembled measurement, no decimal place added
  return num*0.001;
}

Thanks you again Riva , I have a one more question , Can you help me?
Now it’s work for connect 2 micrometer but when i just connect one micrometer it will not work!!
How to auto detect for solve this a problem ?

matsuo:
Now it’s work for connect 2 micrometer but when i just connect one micrometer it will not work!!
How to auto detect for solve this a problem ?

Talk about moving goalposts ;D
The below compiles okay but I’m at work so cannot test it. If there is no micrometer attached it will hopefully abort trying to read it after a second or two and return -1.0 as the measured length.

#define timout 1000UL         // Timout (1 second) if no micrometer detected.

//*** Setup micrometer 1 *************
byte req = 5; //mic11 REQ line goes to pin 5 
byte dat = 2; //mic11 Data line goes to pin 2
byte clk = 3; //mic11 Clock line goes to pin 3
byte req2 = 9; //mic2 REQ line goes to pin 9 
byte dat2 = 6; //mic2 Data line goes to pin 6
byte clk2 = 7; //mic2 Clock line goes to pin 7

void setup()
{
  Serial.begin(9600);
  //*** Setup micrometer 1 *************
  pinMode(req, OUTPUT);
  pinMode(clk, INPUT_PULLUP);
  pinMode(dat, INPUT_PULLUP);
  pinMode(req2, OUTPUT);
  pinMode(clk2, INPUT_PULLUP);
  pinMode(dat2, INPUT_PULLUP);
  digitalWrite(req,LOW); // set request at LOW
  digitalWrite(req2,LOW); // set request at LOW
}

void loop(){
  float Value = readMicrometer(req,clk,dat);
  Serial.println(Value);
  Value = readMicrometer(req2,clk2,dat2);
  Serial.println(Value);
}

float readMicrometer(byte reqPin, byte clkPin, byte datPin){
  byte mydata[14];
  
  digitalWrite(reqPin, HIGH); // generate set request
  for( byte i = 0; i < 13; i++ ) {
    byte k = 0;
    for (byte j = 0; j < 4; j++) {
      unsigned long time = millis();
      while( digitalRead(clkPin) == LOW) {
        if ((millis() - time) > timout) return -1.0;
      }; // hold until clock is high
      while( digitalRead(clkPin) == HIGH) {
        if ((millis() - time) > timout) return -1.0;
      }; // hold until clock is low
      bitWrite(k, j, (digitalRead(datPin) & 0x1)); // read data bits, and reverse order )
    }
    mydata[i] = k;
  }
  digitalWrite(reqPin, LOW); // clear set request
  
  char buf[7];
  for(byte lp=0;lp<6;lp++){
    buf[lp]=mydata[lp+5]+'0';
  }
  buf[6]=0;
  long num=atol(buf); //assembled measurement, no decimal place added
  return num*0.001;
}

Thanks Riva . It's work :wink:

matsuo:
Thanks Riva . It's work :wink:

Glad it works okay and nice that you took the time to tell me.