Pages: 1 ... 7 8 [9]   Go Down
Author Topic: Mitutoyo Digimatic SPC  (Read 11975 times)
0 Members and 1 Guest are viewing this topic.
Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4801
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What will you do if the measurement is 1.25 or 1.5?
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

New Jersey
Online Online
Faraday Member
**
Karma: 67
Posts: 3677
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Glad to hear it's working. All that business with v1, v2 etc is crying out for arrays, which in fact you already have. Consider replacing my fragment with this:
Code:
long num;
char buf[7];
for(int lp=0;lp<6;lp++)
  buf[lp]=mydata[lp+5]+'0';
buf[6]=0;
num=atol(buf);
Then you can get rid of the v<n> variables entirely.
Logged

Andrews SC
Offline Offline
Full Member
***
Karma: 0
Posts: 239
If Arduino is not the answer, you are asking the wrong question!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

GoForSmoke, I'm using round in my formula, and typecasting float to int. I ended up having to use floats for some formulas, but the project is finished and working as expected. It takes a micrometer reading off a set of gears, and generates a part number so the gear matching people can create gear sets.

Thank you one and all for all your help.

grumpy_mike
wildbill
GoForSmoke
giantsfan3
awol
and others who weighed in on this project!

Code:
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;
int sign = 0;
int decimal;
int units;
int error = 0;


   int staticl;
   float mm;
   int maxl = 47;
   float maxmm = 5.097;
   float range = .003;
   float jump = .004;

byte mydata[14];

void setup()
{
  Serial.begin(19200);
  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 high
}



void loop()
{   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;
      sign = mydata[4];
      decimal = mydata[11];
      units = mydata[12];
           
           
    }
long num;
char buf[7];
for(int lp=0;lp<6;lp++)
  buf[lp]=mydata[lp+5]+'0';
buf[6]=0;
num=atol(buf);

if (units != 0) error=1;
if (sign != 0) error=2;
if (decimal != 3) error=3;

mm = (float)num/1000;
float formula = round((((maxmm-(range/2.000))-mm)/jump));
int f;
f = (int)formula;
staticl = maxl-f;



if (error != 0)
{
  Serial.print ("error ");
  Serial.println (error);
}
else
{
    Serial.println(staticl);
}


    digitalWrite(req,LOW);
    error=0;
    delay(100);
}
Logged

Steve Spence - ET/AT/R&D KK4HFJ

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4801
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's okay, as long as it works! May all your bids get the contracts!
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Andrews SC
Offline Offline
Full Member
***
Karma: 0
Posts: 239
If Arduino is not the answer, you are asking the wrong question!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want to see the actual measurement output in mm:

change:

Code:
else
{
    Serial.println(staticl);
}

to

Code:
else
{
    Serial.println(mm, 3);
}

and dump the

Code:
float formula = round((((maxmm-(range/2.000))-mm)/jump));
int f;
f = (int)formula;
staticl = maxl-f;
Logged

Steve Spence - ET/AT/R&D KK4HFJ

Andrews SC
Offline Offline
Full Member
***
Karma: 0
Posts: 239
If Arduino is not the answer, you are asking the wrong question!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This is working with my µVGA-II(SGC) - from 4D Systems, and my Speakjet with TTS256, text to speech shield I built. Those are topics for another day, but a talking micrometer is pretty cool.
Logged

Steve Spence - ET/AT/R&D KK4HFJ

Portland, OR
Offline Offline
Sr. Member
****
Karma: 7
Posts: 260
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Steve Spence: Congrats for persisting through all the difficulties and getting it finished. I'm interested in testing your code myself when I get a chance in the next couple of weeks.
Since you already have your working code ready in the post above, perhaps you should put it together along with an image of your basic circuit hookup and put it in the Exhibition/Gallery section, or on the Playground.
Logged

Andrews SC
Offline Offline
Full Member
***
Karma: 0
Posts: 239
If Arduino is not the answer, you are asking the wrong question!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Great idea, thanks.
Logged

Steve Spence - ET/AT/R&D KK4HFJ

New Jersey
Online Online
Faraday Member
**
Karma: 67
Posts: 3677
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Before you do, consider cleaning it up a bit - the IDE's autoformat (ctrl T) would help, as would getting rid of some of the surplus blank lines - it'll make it easier for future readers, including yourself.

Personally, I'd also move some of the functionality inside loop into separate functions, such as all of Grumpy Mike's work that does the actual reading of the micrometer.

Finally, you're not that short on space - for the error outputs, how about some more user friendly messages in addition to the codes.
Logged

Andrews SC
Offline Offline
Full Member
***
Karma: 0
Posts: 239
If Arduino is not the answer, you are asking the wrong question!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've made some of those improvements already, and added some more application specific functionality, like printing the gear family on the screen based of size range measured. feature creep ....
Logged

Steve Spence - ET/AT/R&D KK4HFJ

Pages: 1 ... 7 8 [9]   Go Up
Jump to: