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!
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);
}