-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometry-calculator.ino
161 lines (137 loc) · 3.98 KB
/
geometry-calculator.ino
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
bool shouldRun = true; //This decides if the code will still run
void setup() {
Serial.begin(9600); //This is the delay in which it begins
geometryCalcuMenu(); // Display the Geometry Calculator Choices the 1-4
}
void loop() {
if (!shouldRun) {
return; // Exit the loop and end the program
}
while (Serial.available()) {
Serial.read();
}
char choice = userPrompt();
switch (choice) {
case '1':
circleCalcu();
break;
case '2':
rectangleCalcu();
break;
case '3':
triangleCalcu();
break;
case '4':
Serial.println("Exiting program.");
shouldRun = false;
break;
default:
Serial.println("Invalid choice. Please enter a number from 1 to 4.");
}
if (shouldRun) {
while (Serial.available()) {
Serial.read();
}
char tryAgain;
do {
Serial.println("Do you want to try again (y/n)?");
while (!Serial.available()); // Wait for user input
tryAgain = Serial.read();
if (tryAgain == 'n' || tryAgain == 'N') {
Serial.println("Exiting program.");
shouldRun = false;
break;
}
} while (tryAgain != 'y' && tryAgain != 'Y');
if (shouldRun) {
geometryCalcuMenu();
}
}
}
void geometryCalcuMenu() {
Serial.println("<<<<< Geometry Calculator >>>>>");
Serial.println("1. Calculate the Area of a Circle");
Serial.println("2. Calculate the Area of a Rectangle");
Serial.println("3. Calculate the Area of a Triangle");
Serial.println("4. Quit");
Serial.println("Enter your choice (1-4):");
}
char userPrompt() {
while (!Serial.available());
return Serial.read();
}
void circleCalcu() {
Serial.println("*** Computing for the area of the circle ***");
Serial.println("Enter radius of the circle: ");
float radius = positiveValidation();
float area = 3.14159 * radius * radius;
Serial.print("The area of the circle with a radius of ");
Serial.print(radius);
Serial.print(" is ");
Serial.println(area);
}
void rectangleCalcu() {
// Clear serial buffer
while (Serial.available()) {
Serial.read();
}
Serial.println("*** Computing for the area of the rectangle ***");
Serial.println("Enter length of the rectangle: ");
float length = positiveValidation();
Serial.println("Enter width of the rectangle: ");
float width = positiveValidation();
float area = length * width;
Serial.print("The area of the rectangle with length ");
Serial.print(length);
Serial.print(" and width ");
Serial.print(width);
Serial.print(" is ");
Serial.println(area);
}
void triangleCalcu() {
// Clear serial buffer
while (Serial.available()) {
Serial.read();
}
Serial.println("*** Computing for the area of the triangle ***");
Serial.println("Enter length of the triangle's base: ");
float base = positiveValidation();
Serial.println("Enter height of the triangle: ");
float height = positiveValidation();
float area = 0.5 * base * height;
Serial.print("The area of the triangle with base ");
Serial.print(base);
Serial.print(" and height ");
Serial.print(height);
Serial.print(" is ");
Serial.println(area);
}
float positiveValidation() {
float input;
// Clear serial buffer
while (Serial.available()) {
Serial.read();
}
while (true) {
while (!Serial.available()); // Wait for user input
input = Serial.parseFloat();
if (input >= 0) {
return input;
} else {
Serial.println("Error: Please enter a positive value.");
Serial.println("Do you want to add the positive value? (y/n)?");
char tryAgain;
while (!Serial.available()); // Wait for user input
tryAgain = Serial.read();
if (tryAgain == 'n' || tryAgain == 'N') {
Serial.println("Exiting program.");
exit(0); // Exit the program
} else if (tryAgain == 'y' || tryAgain == 'Y') {
Serial.println("Enter the value again:");
} else {
Serial.println("Invalid choice. Please enter 'y' or 'n'.");
}
}
}
return input;
}