Smart distance meter using ultrasonic sensor, Arduino and memory card shield | CODES

Smart distance meter


Measuring a distance by using a smart meter is greet for use now day. In construction and for domestic usage, the measurement is from tape meter and other distance measure equipment. Distance Measurement using microcontroller and Ultrasonic Sensor. 
The ultrasonic sensor is consisting of Transmitter and Receiver modules. Transmitter part ejects the pulse out and the receiver part receives the pulse. If an obstacle is placed before the sensor, the transmitted pulse ejected strike the obstacle and reflected back. The reflected pulse is received by the receiver part. The time between transmission and reception is calculated. This data is processed to calculate distance.



OIT


 CODES


#include <Wire.h>

#include <LiquidCrystal_I2C.h>


/*---------------Distance sensor--------------------------*/

#define trigPin 7

#define echoPin 6

/*---------------LCD-------------------------------------*/


long umbali;

long sensorState;


LiquidCrystal_I2C lcd(0x27,16,2);

/*---------------SD CARD-------------------------------------*/

#include <SPI.h>

#include <SD.h>


File myFile;


/*---------------buttons-------------------------------------*/

#define writeButton 3

#define readBtn 4


void setup() {

  Serial.begin(9600);

  pinMode(trigPin, OUTPUT);  //The transmit pin of the ultrasonic sensor

  pinMode(echoPin, INPUT);

  pinMode(writeButton, INPUT);

  pinMode(readBtn, INPUT);

  pinMode(8, OUTPUT);


 start_up();

  // pima_distance();


}


void loop() {

  

  pima_distance();

  

  if(digitalRead(writeButton) == HIGH)

  {

    write_sd(umbali);

    Serial.println(umbali);

    digitalWrite(8,HIGH);

    delay(50);

  }

 if(digitalRead(readBtn) == HIGH)

  {

    read_sd();

    digitalWrite(8,HIGH);

    delay(50);

  }else{


 pima_distance();

  }

}


void pima_distance()

{

  long drt, distance;


  digitalWrite(trigPin, HIGH);

  delayMicroseconds(100);

  digitalWrite(trigPin, LOW);

  drt = pulseIn(echoPin, HIGH);

  distance = (drt / 2) / 27;

  Serial.print("distance =");

  Serial.println(distance);

delay(1000);

  lcd.clear();

  lcd.setCursor(0, 0);

  lcd.print("Distance measured");

  lcd.setCursor(0, 1);

  lcd.print(distance);

  lcd.print(" cm");

  

  umbali = distance;

 

}

void start_up() {

  lcd.init();// initialize the lcd

  lcd.backlight();// Backlight ON

  lcd.setCursor(0, 0); // 2nd column,1st row

  lcd.print("DISTANCE MEASURE");

  lcd.setCursor(5, 1); // 1st column,2nd row

  lcd.print("DEVICE");

  delay(1000);

  lcd.clear();

  delay(500);

  lcd.setCursor(0, 0);

  lcd.print("Initializing.....");

  delay(500);

  lcd.clear();

  delay(500);

  lcd.setCursor(0, 0);

  lcd.print("SD module check.");

 


  if (!SD.begin(10)) {

    Serial.println("sd initialization failed!");

    lcd.setCursor(0, 1);

    lcd.println("sd failed!");

    while (1);

  }

  delay(500);

}


void read_sd()

{

  if (!SD.begin(10)) {

    Serial.println("initialization failed!");

    while (1);

  }

   lcd.setCursor(16, 0);

  lcd.autoscroll();

  myFile = SD.open("Measures.txt"); // open the file for reading

 

  if (myFile) {

    while (myFile.available()) { //execute while file is available

        Serial.write(myFile.read()); //read next character from file

      lcd.println(myFile.read()); //display character

      delay(300);

    }


    myFile.close(); //close file

      Serial.println("Reading done.");

  }


  lcd.clear();

  pima_distance();


}

void write_sd(long dist)

{

  if (!SD.begin(10)) {

    Serial.println("initialization failed!");

    while (1);

  }

  myFile = SD.open("Measures.txt", FILE_WRITE);

  

    lcd.clear();

    delay(500);

    lcd.setCursor(0, 0);

    lcd.print("Writing distance...");

    Serial.println("Writing");

    if(myFile){

    myFile.print("Distance :");

    myFile.println(dist);

    myFile.close();

      Serial.println("done writting.");

    }


    lcd.setCursor(0, 1);

    lcd.print("Done...");

    delay(500);

    pima_distance();

    

  


}

Post a Comment

0 Comments