Hello
I need to connect micrometer 2 pcs with Arduino nano. , Now i found the code for connect micrometer 1 pcs.
from this link http://www.instructables.com/id/Interfacing-a-Digital-Micrometer-to-a-Microcontrol/but i need to micrometer 2 pcs.
Can you help me,please
Micrometer 1
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
int i = 0; int j = 0; int k = 0;
float Value;
byte mydata[14];
long num;
int gearFamily = 7;
// Mode button
const int buttonPin = 4; // the pin 4 that the pushbutton is attached to
int buttonPushCounter = 7; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup()
{
Serial.begin(9600);
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
pinMode(buttonPin, INPUT);
}
void loop()
{
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
if (buttonState == LOW) {
buttonPushCounter++;
}
}
//for next time through the loop
lastButtonState = buttonState;
if (buttonPushCounter == 8) {
buttonPushCounter = 1;
}
gearFamily = buttonPushCounter;
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;
}
// assemble measurement from bytes
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, 3);
}
Micrometer 2 ** this my code isn't work.
//*** Setup micrometer 1 *************
int req = 5; //mic1 REQ line goes to pin 5
int dat = 2; //mic1 Data line goes to pin 2
int clk = 3; //mic1 Clock line goes to pin 3
int i = 0; int j = 0; int k = 0;
float Value;
byte mydata[14];
long num;
int gearFamily = 7;
const int buttonPin = 4; // the pin that the pushbutton is attached to
int buttonPushCounter = 7; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
//*** Setup micrometer 2 *************
int req2 = 9; //mic2 REQ line goes to pin 9 through q1 (arduino high pulls request line low)
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;
int gearFamily2 = 7;
const int buttonPin2 = 8; // the pin that the pushbutton is attached to
int buttonPushCounter2 = 7; // counter for the number of button presses
int buttonState2 = 0; // current state of the button
int lastButtonState2 = 0; // previous state of the button
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
pinMode(buttonPin, INPUT);
//*** 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
pinMode(buttonPin2, INPUT);
}
void loop()
{
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
if (buttonState != lastButtonState||buttonState2 != lastButtonState2)
{
if (buttonState == LOW)//Micro1
{
buttonPushCounter++;
}
if (buttonState2 == LOW)//Micro2
{
buttonPushCounter2++;
}
}
lastButtonState = buttonState;
lastButtonState2 = buttonState2;
if (buttonPushCounter == 8)
{
buttonPushCounter = 1;
}
if (buttonPushCounter2 == 8)
{
buttonPushCounter2 = 1;
}
gearFamily = buttonPushCounter;
gearFamily2 = buttonPushCounter2;
digitalWrite(req, HIGH); // generate set request
digitalWrite(req2, 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;
}
//********************************************************
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 buf[7];
char buf2[7];
for(int lp=0;lp<6;lp++)
buf[lp]=mydata[lp+5]+'0';
buf[6]=0;
for(int lp2=0;lp2<6;lp2++)
buf2[lp2]=mydata2[lp2+5]+'0';
buf2[6]=0;
num=atol(buf); //assembled measurement, no decimal place added
num2=atol(buf2); //assembled measurement, no decimal place added
Value=num*0.001;
Value2=num2*0.001;
Serial.println(Value, 2);
Serial.println(Value2, 2);
}