Advertisement
Guest User

height measurement tool

a guest
Aug 21st, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Height Measurement Tool
  2. // 2019.06.02
  3. // Bret Wagner
  4.  
  5. #include "SR04.h"
  6. #include <LiquidCrystal.h>
  7. #define TRIG_PIN 14
  8. #define ECHO_PIN 15
  9. SR04 sr04 = SR04(ECHO_PIN, TRIG_PIN);
  10. long distance;
  11. long start;
  12. long height;
  13. long inches;
  14. int feet;
  15. int resetButton = 3;
  16. int measureButton = 4;
  17. int convertButton = 5;
  18. int dataUnit = 0;
  19.  
  20. LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
  21.  
  22. void setup() {
  23.   // put your setup code here, to run once:
  24.   Serial.begin(9600);
  25.   pinMode(resetButton, INPUT_PULLUP);
  26.   pinMode(measureButton, INPUT_PULLUP);
  27.   pinMode(convertButton, INPUT_PULLUP);
  28.   lcd.begin(16,2);
  29.   lcd.print("Measure your");
  30.   lcd.setCursor(0,1);
  31.   lcd.print("height?");
  32. }
  33.  
  34. void loop() {
  35.   // put your main code here, to run repeatedly:
  36.   if (digitalRead(resetButton) == LOW) {
  37.     Serial.println("Tare button Pressed");
  38.     tareMeasurement();
  39.     delay(300);
  40.   }
  41.  
  42.   if (digitalRead(measureButton) == LOW) {
  43.     distance = sr04.Distance();
  44.     height = start-distance;
  45.     Serial.println("Measure button Pressed");
  46.     displayData(height, dataUnit);
  47.     delay(300);
  48.   }
  49.  
  50.   if (digitalRead(convertButton) == LOW) {
  51.     Serial.println("Convert button Pressed");
  52.     dataUnit = switchUnit(dataUnit);
  53.     displayData(height, dataUnit);
  54.     delay(300);
  55.   }
  56. }
  57.  
  58. int switchUnit(int dataUnit) {
  59.   if (dataUnit == 2) {
  60.     dataUnit = 0;
  61.   } else {
  62.     dataUnit = dataUnit + 1;
  63.   }
  64.   Serial.print("data type ");
  65.   Serial.println(dataUnit);
  66.   return dataUnit;
  67. }
  68.  
  69. void displayData(long height, int dataUnit) {
  70.   clearLcd();
  71.   lcd.print("You are");
  72.   if (dataUnit == 0) {
  73.     lcd.setCursor(0,1);
  74.     lcd.print(height);
  75.     lcd.print("cm tall");
  76.     Serial.print(height);
  77.     Serial.println("cm");
  78.   } else if (dataUnit == 1) {
  79.     inches = getInches(height);
  80.     lcd.setCursor(0,1);
  81.     lcd.print(inches);
  82.     lcd.print(" inches tall");
  83.     Serial.print(height);
  84.     Serial.println("cm");
  85.   } else if (dataUnit == 2) {
  86.     feet = getFeet(height);
  87.     inches = getInches(height);
  88.     inches = inches % 12;
  89.     lcd.setCursor(0,1);
  90.     lcd.print(feet);
  91.     lcd.print("'");
  92.     lcd.print(inches);
  93.     lcd.print("\" tall");
  94.     Serial.print(height);
  95.     Serial.println("cm");
  96.   } else {
  97.     lcd.setCursor(0,0);
  98.     lcd.print("\0");
  99.     lcd.setCursor(0,1);
  100.     lcd.print("\0");  
  101.     Serial.println("Error");
  102.   }
  103. }
  104.  
  105. void clearLcd() {
  106.   lcd.setCursor(0,0);
  107.   lcd.print("                   ");
  108.   lcd.setCursor(0,1);
  109.   lcd.print("                   ");
  110.   lcd.setCursor(0,0);
  111. }
  112.  
  113. int getFeet(long height) {
  114.   inches = height/2.54;
  115.   feet = inches / 12;
  116.   return feet;
  117. }
  118.  
  119. int getInches(long height) {
  120.   inches = height/2.54;
  121.   return inches;
  122. }
  123.  
  124.  
  125. long tareMeasurement() {
  126.   start = sr04.Distance();
  127.   Serial.print(start);
  128.   Serial.println("cm");
  129.   return start;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement