Digital Ruler – Arduino and Ultrasonic based PCB

#include <LiquidCrystal.h>
#define trig 3
#define echo 8
#define mode A0
#define hold A1
#define lzr A2

byte smiley[8] = {
  B10000,
  B11000,
  B11100,
  B11110,
  B11100,
  B11000,
  B10000,
};

//Nisal Git

int modeCount = 1;
int k = 13;

bool holdMode = false;

bool modePre = false;
bool holdPre = false;
bool lzrPre = false;
bool firstTimeLength = true;
bool firstTimeArea = true;
bool firstTimeVolume = true;
bool dot = true;

const int rs = 1, en = 2, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(mode, INPUT_PULLUP);
  pinMode(hold, INPUT_PULLUP);
  pinMode(lzr, INPUT_PULLUP);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  lcd.begin(16, 2);
  lcd.createChar(0, smiley);
  //Serial.begin(250000);

  digitalWrite(10, dot); // Lzr

}

void loop() {


  switch (modeCount) {
    case 1 : distance();
      break;
    case 2 : area();
      break;
    case 3 : volume();
      break;

  }




}

long getDistance() {
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  long T = pulseIn(echo, HIGH);
  return (T / 29 / 2) + k;
}
/////////////////////////////////////////////////////////////
void distance() {
  if (firstTimeLength) {
    lcd.clear();
    lcd.setCursor(1, 0);
    lcd.print("Mesure Distance");
    lcd.setCursor(0, 1);
    lcd.write(byte(0));
    lcd.setCursor(5, 1);
    lcd.print(" cm");

    firstTimeLength = false;

  }

  if ((!digitalRead(hold)) && (!holdPre) ) {
    buz();
    holdMode = !holdMode;

    holdPre = true;
  }
  holdPre = !digitalRead(hold);

  if (holdMode) {
    delay(1);
  }
  else {
    long len = getDistance();
    lcd.setCursor(1, 1);
    lcd.print("     ");
    lcd.setCursor(2, 1);
    lcd.print(len);
  }
  delay(300);
  redDot();
  if (!digitalRead(mode)) {
    buz();
    modeCount = 2;
    firstTimeLength = true;
    delay(200);
    return;
  }

}

///////////////////////////////////////////////////////////

void area() {
  long len;
  long width;
  long area;
  if (firstTimeArea) {

    lcd.clear();
    lcd.setCursor(1, 0);
    lcd.print("Calculate Area");
    delay(1000);

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("L:");
    lcd.setCursor(7, 0);
    lcd.print("W:");
    lcd.setCursor(13, 1);
    lcd.print(" cm");
  }
  do {

    len = getDistance();
    lcd.setCursor(0, 1);
    lcd.print("              ");
    lcd.setCursor(0, 1);
    lcd.print("Length = ");
    lcd.print(len);

    delay(300);
    Serial.println("length");
    if (!digitalRead(mode)) {
      buz();
      modeCount = 3;
      delay(200);
      return;
    }
    redDot();
  } while (digitalRead(hold));
  delay(300);
  lcd.setCursor(3, 0);
  lcd.print(len);


  ///////
  do {

    width = getDistance();
    lcd.setCursor(0, 1);
    lcd.print("              ");
    lcd.setCursor(3, 0);
    lcd.print(len);
    lcd.setCursor(0, 1);
    lcd.print("Width = ");
    lcd.print(width);
    redDot();
    if (!digitalRead(mode)) {
      modeCount = 3;
      buz();
      delay(200);
      return;
    }
    delay(500);
  } while (digitalRead(hold));

  delay(500);
  lcd.setCursor(0, 1);
  lcd.print("                ");
  //lcd.print(len);
  lcd.setCursor(0, 1);
  lcd.setCursor(10, 0);
  lcd.print(width);

  lcd.setCursor(1, 1);
  lcd.print("Area = ");
  area = len * width;
  lcd.print(area);
  lcd.print(" cm2");
  delay(300);
  redDot();
  while (1) {
    delay(10);
    if (!digitalRead(mode)) {
      buz();
      modeCount = 3;
      delay(100);
      return;
    }

    if (!digitalRead(hold)) {
      buz();
      return;
    }
  }

  modePre = !digitalRead(mode);
  holdPre = !digitalRead(hold);

}

/////////////////////////////////////////////////////////////

void volume() {
  long len;
  long width;
  long height;
  long volume;
  if (firstTimeVolume) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Calculate Volume");
    delay(1000);

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("L:");
    lcd.setCursor(5, 0);
    lcd.print("W:");
    lcd.setCursor(10, 0);
    lcd.print("H:");
    lcd.setCursor(13, 1);
    lcd.print(" cm");
  }
  do {

    len = getDistance();
    lcd.setCursor(0, 1);
    lcd.print("              ");
    lcd.setCursor(0, 1);
    lcd.print("Length = ");
    lcd.print(len);

    delay(300);
    Serial.println("length");
    redDot();
    if (!digitalRead(mode)) {
      buz();
      modeCount = 1;
      delay(200);
      return;
    }
  } while (digitalRead(hold));
  delay(500);

  ///////
  do {

    width = getDistance();
    lcd.setCursor(0, 1);
    lcd.print("              ");
    lcd.setCursor(2, 0);
    lcd.print(len);
    lcd.setCursor(0, 1);
    lcd.print("Width = ");
    lcd.print(width);

    delay(300);
    redDot();
    if (!digitalRead(mode)) {
      buz();
      modeCount = 1;
      delay(200);
      return;
    }

  } while (digitalRead(hold));

  lcd.setCursor(7, 0);
  lcd.print(width);

  do {

    height = getDistance();
    lcd.setCursor(0, 1);
    lcd.print("              ");
    lcd.setCursor(0, 1);
    lcd.print("Height = ");
    lcd.print(height);

    delay(300);
    //Serial.println("height");
  } while (digitalRead(hold));

  delay(500);
  lcd.setCursor(0, 1);
  lcd.print("                ");
  //lcd.print(len);
  lcd.setCursor(0, 1);
  lcd.setCursor(12, 0);
  lcd.print(height);

  lcd.setCursor(0, 1);
  lcd.print("Vol = ");
  volume = len * width * height;
  lcd.print(volume);
  lcd.print(" cm3");
  delay(1000);

  while (1) {
    delay(10);
    if (!digitalRead(mode)) {
      modeCount = 1;
      buz();
      delay(100);
      return;
    }

    if (!digitalRead(hold)) {
      buz();
      return;
    }
  }
  redDot();
  modePre = !digitalRead(mode);
  holdPre = !digitalRead(hold);

}

void redDot() {
  if (!digitalRead(lzr)) {
    dot = !dot;
    digitalWrite(10, dot);

    delay(400);
  }
}

void buz() {
  digitalWrite(9, 1);
  delay(100);
  digitalWrite(9, 0);
}

6 thoughts on “Digital Ruler – Arduino and Ultrasonic based PCB

  1. hoda creative ideas godak dakka mn me video seriuos eke. ewagema pcb desining wala improments godak igenagaththa. godak isthuthiii nisal malli oyata.

Leave a Reply

Your email address will not be published. Required fields are marked *