Go Down

Topic: interfacing arduino uno a 16 x 2 lcd and digital inputs from 2 digital calipers (Read 521 times) previous topic - next topic

papakel

Hi all, I am a retired domestic building and joinery contractor with newfound interests i.e, Arduino and a metal lathe.  I read two posts about Arduino reading data from a digital caliper and connecting an LCD, both of them  I tried with success and I added the two sample codes together with success printing the caliper reading on the LCD, this gave me the idea to read 2 digital calipers and printing the results to the LCD one above the other to use as a cheap digital display for my metal lathe. I have tried to amend the downloaded code to read from two sets of clock and data input pins and then print the results one above the other I have had some success printing  on both lines but I am struggling to alter the read loop to switch the pair of alternate pairs of inputs pins after alternate reads prior to printing on the LCD.
I would be very grateful for any help from you more experienced coders with this project.
 Thanks in anticipation.
papakel 

pert

Please post the full sketch of your best attempt to read both calipers.

If possible, you should always post code directly in the forum thread as text using code tags:
  • Do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code. This will make it easier for you to spot bugs and make it easier for us to read.
  • In the Arduino IDE or Arduino Web Editor, click on the window that contains your sketch code.
  • Press "Ctrl + A". This will select all the text.
  • Press "Ctrl + C". This will copy the selected text to the clipboard.
  • In a forum reply here, click on the reply field.
  • Click the </> button on the forum toolbar. This will add the forum's code tags markup to your reply.
  • Press "Ctrl + V". This will paste the sketch between the code tags.
  • Move the cursor outside of the code tags before you add any additional text to your reply.
  • Repeat the above process if your sketch has multiple tabs.


This will make it easy for anyone to look at it, which will increase the likelihood of you getting help.

If the sketch is longer than the 9000 characters maximum allowed by the forum, then it's OK to add it as an attachment. After clicking the "Reply" button, you will see an "Attachments and other settings" link.

When your code requires a library that's not included with the Arduino IDE please post a link (using the chain links icon on the forum toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger (Sketch > Include Library > Manage Libraries in the Arduino IDE or Libraries > Library Manager in the Arduino Web Editor) then say so and state the full name of the library.

papakel

Code: [Select]
/* Read the caliper data with Arduino and display mm or inch on serial monitor and LCD
   Tutorial on: https://www.electronoobs.com/eng_arduino_tut93.php
   Schematic: https://www.electronoobs.com/eng_arduino_tut93_sch1.php

  Caliper         |       Arduino
  GND (black)             GND + 200 ohm
  DAT (brown)             D9
  CLK (blue)              D8
  VCC 3.3V (red)          3.3V
  LCD VSS                 GND
  LCD VDD                 5V+
  LCD VO                  PIN3 VRES
  LCD RS                  D12
  LCD RW                  GND
  LCD E                   D11
  LCD D0
  LCD D1
  LCD D2
  LCD D3
  LCD D4                  D5
  LCD D5                  D4
  LCD D6                  D3
  LCD D7                  D2
  LCD A                   5V+
  LCD K                   GND
*/

#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);



int count;


void setup()
{
  Serial.begin(115200);
#define CLOCK_PIN 8
#define DATA_PIN  9
  //#define CLOCK1_PIN 6
  //#define DATA1_PIN  7
  count == 0;


  ; lcd.begin(1, 10);

}

char buf[10];             //;was20
unsigned long tmpTime;
int sign;
int inches;
long value;
float result;
bool mm = true; //define mm to false if you want inces values

void loop()
{
  if (count == 0); (CLOCK_PIN == 8); (DATA_PIN == 9);

  if (count == 1); (CLOCK_PIN == 6); (DATA_PIN == 7);

  while (digitalRead(CLOCK_PIN) == LOW) {}
  tmpTime = micros();
  while (digitalRead(CLOCK_PIN) == HIGH) {}
  if ((micros() - tmpTime) < 500) return;
  readCaliper();
  buf[0] = ' ';
  dtostrf(result, 6, 3, buf + 1); strcat(buf, " in ");
  dtostrf(result * 2.54, 6, 3, buf + 1); strcat(buf, " cm ");

  if (mm)
  {
    if (count == 0)lcd.setCursor(0, 0); lcd.print(" Bed  "); lcd.print(result); lcd.println(" mm     ");
    (count == 1); return;

    if (count == 1);
    lcd.setCursor(0, 1); lcd.print("C/SL "); lcd.print(result); lcd.println(" mm     ");
    (count == 0);

  }
  else
  {
    Serial.print(result); Serial.println(" in");
    delay(100);
  }
}
void readCaliper()
{
  sign = 1;
  value = 0;
  inches = 0;
  for (int i = 0; i < 24; i++) {
    while (digitalRead(CLOCK_PIN) == LOW) {}
    while (digitalRead(CLOCK_PIN) == HIGH) {}
    if (digitalRead(DATA_PIN) == HIGH) {
      if (i < 20) value |= (1 << i);
      if (i == 20) sign = -1;
      if (i == 23) inches = 1;
    }
  }
  if (mm)
  {
    result = (value * sign) / 100.0;
  }
  else
  {
    result = (value * sign) / (inches ? 2000.0 : 100.0); //We map the values for inches, define mm to false if you want inces values
  }

}


This includes my latest attempt to add a counter to change the clock and data pins but it does not work

pert

if (count == 0); (CLOCK_PIN == 8); (DATA_PIN == 9);

  if (count == 1); (CLOCK_PIN == 6); (DATA_PIN == 7);
Problems:
Your if statement does nothing. Please take some time to read the reference page until you understand how to use it correctly:
https://www.arduino.cc/reference/en/language/structure/control-structure/if/
You never follow an if statement with a semicolon.
ALWAYS use curly braces, even if the if block only contains a single line.
Don't smash multiple statements together on the same line. This just makes your code harder to read.

You are using the comparison operator (==) where you surely meant to use the assignment operator (=). Please take the time to read both these references until you understand how to use both operators correctly:


It appears your intent was to assign a value to a macro. You can't do that. Use a variable instead.

I think you would be better off to configure your readCaliper function so that it takes the clock and data pin as arguments. Put all the code to read the caliper in the readCaliper function. Make the readCaliper function return the read value instead of using the result global variable to transfer that information. Then you could do something like this:
Code: [Select]
lcd.print(readCaliper(caliperADataPin, caliperAClockPin));
Isn't that nice?

Go Up