Go Down

Topic: Mitutoyo Digimatic SPC (Read 13 times) previous topic - next topic

GoForSmoke

What will you do if the measurement is 1.25 or 1.5?
I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

wildbill

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: [Select]

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.

Steve Spence - KK4HFJ

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: [Select]
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);
}
Steve Spence - ET/AT/R&D KK4HFJ

GoForSmoke

It's okay, as long as it works! May all your bids get the contracts!
I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Steve Spence - KK4HFJ

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

change:

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


to

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


and dump the

Code: [Select]
float formula = round((((maxmm-(range/2.000))-mm)/jump));
int f;
f = (int)formula;
staticl = maxl-f;
Steve Spence - ET/AT/R&D KK4HFJ

Steve Spence - KK4HFJ

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.
Steve Spence - ET/AT/R&D KK4HFJ

giantsfan3

@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.

Steve Spence - KK4HFJ

Steve Spence - ET/AT/R&D KK4HFJ

wildbill

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.

Steve Spence - KK4HFJ

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 ....
Steve Spence - ET/AT/R&D KK4HFJ

tlbruns

I know this is a really old thread, but since this was the first link I found on the topic. I'm posting this here in case there are any others that would like to use this in the future.

I stripped down the code to the bare essentials and compiled it into a library. Now the user simply has to initialize the interface and then use a single fetch() command to receive data. It's actually my first library (and I'm a mechanical engineer) so I'm sure not everything is done by best practices, but it works and should make it quick and easy for others to use.

https://github.com/tlbruns/Digimatic

Steve Spence - KK4HFJ

First gander at it looks awesome, can't wait to test it. Great job!
Steve Spence - ET/AT/R&D KK4HFJ

Go Up