Digitial calipers {SOLVED}

After doing some searching and learning, I’ve been able to read the incoming data from those cheap digital callipers and got it displaying both metric and inches on a LCD.

Now I would like to add an 7 segment display using a Max7219 but seem to be struggling on how to get it work, The code below is what I am trying it’s kind of displaying some data but not the correct data.

I only want it to display the metric reading, I’m trying to get my head around on how to get it to display the reading like it on the LCD. LCD displays 13.3350 and on the LED display only 333.1 ?.

I tried watching videos done lots of playing but cant get my head around, I’d be grateful if someone could point me in the right direction.

Here’s the cod I have working

#include <PinChangeInt.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

//#define  DEBUGLCD      // Flag set for debugging LCD Device operation
//#define  DEBUGCALIPER0 // Flag set for Caliper0
#define XCLK_Pin 2
#define XDAT_Pin 3
#define  XZERO_Pin 11
//LCD max 7219
int pomocni1;
int pomocni2;
int ostatak1;
int ostatak2;
int pozicija1 = 4;
int pozicija2 = 0;

#include <LedControl.h>
//LedControl lc=LedControl(12,11,10,1);
LedControl lc = LedControl(50, 51, 53, 1);

int xpos = 0;
char cval = 0x70;
// ISR Variables

// X Block
unsigned long pre_posVal0;
unsigned long old_posVal0 = 0;
volatile unsigned long passVal0 = 0;
volatile unsigned long posVal0;
volatile unsigned long intervalT0;
volatile int bitrunner0 = 0;
volatile boolean bitin0;
double metric = 0;
float average = 5.36;
// Static Variables for displaying
long marker0;        // Current positions for X, Y, and Z
long oldMarker0 = 0; // Previous positions for X, Y, and Z
long adjuster0 = 0;  // Zero positions for reference
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 100;
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// End Front Matter.  Begin SETUP Routine here.
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// the setup routine runs once when you press reset:
void setup() {
  lc.shutdown(0, false); // Enable display
  lc.setIntensity(0, 10); // Set brightness level (0 is min, 15 is max)
  lc.clearDisplay(0); // Clear display register
  pinMode( XCLK_Pin, INPUT);
  pinMode( XDAT_Pin, INPUT);
  pinMode( XZERO_Pin, INPUT);
  digitalWrite(XZERO_Pin, HIGH);    // Enable interna; pullup
  lcd.begin(16, 2);
  lcd.clear();
#if defined( DEBUGLCD ) || defined( DEBUGCALIPER0 )
  Serial.begin(9600);      // Wake up the serial line at 9600 baud
  Serial.println("\n\nHello World - System Starting.");
#endif


  delay(2);
  lcd.setCursor(9, 0);
  lcd.print("MM");
  lcd.setCursor(9, 1);
  lcd.print("INCHES");

  intervalT0 = micros();

  attachInterrupt(0, xclkISR, RISING);  // Int zero on pin two


}
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// End Setup.  Begin MAIN LOOP Routine here.
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
// the loop routine runs over and over again forever:
void loop() {


  // Check if X is new
  if (bitrunner0 == 24) { // we have shifted in a full value
    pre_posVal0 = passVal0;                            // Grab the passed up value before it changes
    if ((old_posVal0 != pre_posVal0) || (!digitalRead(XZERO_Pin))) {
      old_posVal0 = pre_posVal0;
      marker0 = convertCalVal( pre_posVal0 );
      if (!digitalRead(XZERO_Pin)) adjuster0 = marker0, metric = 0;
      lcd.setCursor(0, 1);
      printPosition( marker0 - adjuster0 );
      metric =  marker0  * 25.4 / 10000;
      lcd.setCursor(0, 0);
      lcd.print( metric, 4 );
      lcd.print(" ");
    }
  }

  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    ispisNaLcd1(metric);
    lc.setLed(0, 6, 0, true);
  }


}

long convertCalVal( unsigned long pVal) {
  long marker;
  marker = (long)((~pVal) & 0xFFFFF) >> 2; // Should be absolute value positive number
  marker = marker * 10;
  if (!(pVal & 0x02)) marker += 5;
  if (!(pVal & 0x200000)) marker = marker * -1;
  return ( marker );
}

void printPosition ( long pVal ) {

  if (pVal > 0) {
    lcd.write('+');
  }
  else {
    lcd.write('-');
    pVal = 0 - pVal;
  }

  // lcd.write(HextoASCII( (unsigned int) (pVal / 100000)));//  For greater than 6Ich calpier Not needed for 6"
  // pVal = pVal % 100000; //  For greater than 6Ich calpier not need fro 6"
  lcd.write(HextoASCII( (unsigned int) (pVal / 10000)));
  pVal = pVal % 10000;
  lcd.write('.');
  lcd.write(HextoASCII( (unsigned int) (pVal / 1000)));
  pVal = pVal % 1000;
  lcd.write(HextoASCII( (unsigned int) (pVal / 100)));
  pVal = pVal % 100;
  lcd.write(HextoASCII( (unsigned int) (pVal / 10)));
  pVal = pVal % 10;
  lcd.write(HextoASCII( (unsigned int) (pVal)));

}


void xclkISR() {
  if ((micros() - intervalT0) > 70000) {
    bitrunner0 = 0;
    passVal0 = posVal0;
    posVal0 = 0;
  }
  bitin0 = digitalRead(XDAT_Pin);
  posVal0 = posVal0 | ( ((unsigned long) bitin0) << bitrunner0++ );
  intervalT0 = micros();
}


char HextoASCII( unsigned char inbyte)
{
  // take the least signifigant nibble and convert to hex ASCII equivalent
  inbyte &= 0x0F;
  inbyte += 0x30;
  if (inbyte > 0x39) inbyte += 0x07;
  return (inbyte);
}
void ispisNaLcd1(float broj1) //for voltage
{

  pomocni1 = broj1 * 100;
  while (pomocni1 > 0)
  {

    ostatak1 = pomocni1 % 10;
    lc.setDigit(0, pozicija1, ostatak1, false);
    pomocni1 = pomocni1 / 10;
    pozicija1++;

  }
  pozicija1 = 4;
}

if you want to print values on max7219 7 segment displays I suggest you have a look on this (my) page:

Arduino Noiasca LED Control with print method (MAX7219/MAX7221).

I have modified the lc library to use stream/print class and you can simply use a

lc.print(“2nd IC”); // print text to LED Display
or
lc.print( metric, 4 );

like you do with your lcd.

install the adopted library, try the strand tests with your pin mapping. Than adopt your exisiting code.

noiasca:
if you want to print values on max7219 7 segment displays I suggest you have a look on this (my) page:

Arduino Noiasca LED Control with print method (MAX7219/MAX7221).

I have modified the lc library to use stream/print class and you can simply use a

lc.print("2nd IC"); // print text to LED Display
or
lc.print( metric, 4 );

like you do with your lcd.

install the adopted library, try the strand tests with your pin mapping. Than adopt your exisiting code.

Wow Thanks, I shall be reading this and get a good understanding hopefully of how it works and set it up.
Will let you know how I get on
once again thanks for this

Steve

After finding out that the display I made years ago was made incorrect (which worked on another project but backwards, This was corrected in software. but not for Arduino) also I counted the pinout wrong on the 7 Segment display's I've got as there are only 12 pins and I found out which Pin one is which I thought it was 12.

But after running the examples that came with your library I got it all working and I now have it working in my DRO.

Excellent job in modifying it to make it easy for everyone.

thank you for your feedback, appreciate.