void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
double distance = 8.0; // The sensor is 8 inches away from the object
// Calculate height for angles from 0 to 90 degrees in intervals of 10 degrees
for (int angle = 0; angle <= 90; angle += 10) {
double height = calculateHeight(angle, distance);
double maxDist = maxDistance(angle);
Serial.print("Angle: ");
Serial.print(angle);
Serial.print(" degrees, Height: ");
Serial.print(height); //change inches to cm
Serial.print(" inch : ");
Serial.print(height * 2.54); //change inches to cm
Serial.print(" cm | maxDist: ");
if(!isinf(maxDist) && maxDist > 0) Serial.print(maxDist); //change inches to cm
else Serial.print("infinity");
Serial.print(" inch : ");
if(!isinf(maxDist) && maxDist > 0) Serial.print(maxDist * 2.54); //change inches to cm
else Serial.print("infinity");
Serial.println(" cm");
}
}
void loop() {
// put your main code here, to run repeatedly:
}
// Function to convert degrees to radians
double degreesToRadians(double degrees) {
return degrees * M_PI / 180.0;
}
// Function to calculate the height based on the sensor's angle
double calculateHeight(double angleDegrees, double distance) {
double angleRadians = degreesToRadians(angleDegrees); // Convert angle to radians
return distance * sin(angleRadians); // Calculate height
}
double maxDistance(double angleDegrees) {
double wallDistance = 8.0;
double angleRadians = degreesToRadians(angleDegrees); // Convert angle to radians
return wallDistance / cos(angleRadians); // Calculate max distance
}