#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
//const float PI = 3.14159;
float radius=3.0;
float areaku=0.0;
// create an OLED display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(9600);
// initialize OLED display with I2C address 0x3C
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
delay(2000); // wait two seconds for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(0, 2); // set position to display (x,y)
oled.println("Program to calculate an area and circumference of metal disc"); // set text
oled.setCursor(0, 35); // set position to display (x,y)
oled.println("IAC2"); // set text
oled.display(); // display on OLED
}
void loop() {
// Wait for user to input the circumference
while (!Serial.available());
// Read the circumference input from the Serial monitor
float circumference = Serial.parseFloat();
// Calculate the radius
float radius = circumference / (2 * PI);
// Calculate the area
float area = PI * radius * radius;
// Display the result on the OLED display
oled.clearDisplay(); // Clear the previous display
oled.setTextSize(1); // Normal 1:1 pixel scale
oled.setTextColor(SSD1306_WHITE); // Draw white text
oled.setCursor(0,0); // Start at top-left corner
oled.print("Circumference: ");
oled.println(circumference);
oled.print("Area: ");
oled.println(area);
oled.display(); // Show the result
// Wait for a moment before clearing the display
delay(5000);
// Clear the display
oled.clearDisplay(); // display on OLED
}