-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharduino_code.ino
More file actions
88 lines (74 loc) · 1.92 KB
/
arduino_code.ino
File metadata and controls
88 lines (74 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <LiquidCrystal.h>
// Pin configurations
const int LCD_RS = 6, LCD_EN = 7, LCD_D4 = 5, LCD_D5 = 4, LCD_D6 = 3, LCD_D7 = 2;
const int TRIG_PIN = 11, ECHO_PIN = 10;
// Constants
const float SOUND_SPEED = 0.034;
const float CM_TO_INCH = 0.393701;
const int LCD_WIDTH = 16, LCD_HEIGHT = 2;
const int BASE_HEIGHT = 180;
LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
void setup() {
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
lcd.begin(LCD_WIDTH, LCD_HEIGHT);
lcd.print(" Your Height");
lcd.setCursor(0, 1);
lcd.print(" Measurement");
delay(3000);
}
float measureDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
return duration * SOUND_SPEED / 2;
}
int calculateHeightAdjustment(int height) {
switch (height / 10) {
case 0:
case 1: return 0;
case 2:
case 3: return 1;
case 4: return 2;
case 5: return 3;
case 6: return 4;
case 7: return 5;
case 8:
if (height <= 85) return 6;
else return 7;
case 9:
if (height <= 95) return 8;
else return 9;
case 10:
if (height <= 105) return 10;
else return 11;
default: return 11;
}
}
void displayHeight(int height) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set 0: ");
lcd.print(height);
lcd.print("cm");
int adjustedHeight = height + calculateHeightAdjustment(height);
lcd.setCursor(0, 1);
lcd.print("Your Height:");
lcd.print(adjustedHeight);
lcd.print("cm");
}
void loop() {
float distanceCm = measureDistance();
float distanceInch = distanceCm * CM_TO_INCH;
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
Serial.print("Distance (inch): ");
Serial.println(distanceInch);
int height = BASE_HEIGHT - distanceCm;
displayHeight(height);
delay(300);
}