#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// ========== Configuration Constants ==========
#define I2C_ADDR 0x27 // I2C address of the LCD (common: 0x27 or 0x3F)
#define LCD_COLS 16 // LCD number of columns
#define LCD_ROWS 2 // LCD number of rows
#define BENCH_LOOPS 5000 // Number of iterations for the benchmark
#define DISPLAY_MS 5000 // How long to show the result on LCD (ms)
#define SERIAL_BAUD 115200 // Serial monitor baud rate
const char* myName = "Shahad"; // Name to display on the first line
// Create LCD object
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLS, LCD_ROWS);
// ========== Function Prototypes ==========
unsigned long runBenchmark(double &result); // Runs the computation and returns duration
// ========== Setup ==========
void setup() {
// Initialize Serial communication (for debugging and backup output)
Serial.begin(SERIAL_BAUD);
// Wait for serial port to connect (only needed for boards with native USB)
while (!Serial) { ; }
// Initialize the LCD
lcd.init(); // Initialize the LCD hardware
lcd.backlight(); // Turn on backlight
// Show a startup message while preparing
lcd.setCursor(0, 0);
lcd.print(myName);
lcd.setCursor(0, 1);
lcd.print("Preparing...");
delay(500); // Brief pause to let the message be seen
}
// ========== Main Loop ==========
void loop() {
double sum; // Will hold the benchmark result
unsigned long duration; // Will hold the computation time
// Run the benchmark and get the duration and sum
duration = runBenchmark(sum);
// Output to Serial monitor (useful for logging or if LCD is not available)
Serial.print("Benchmark result (sum) = ");
Serial.println(sum, 6); // Print with 6 decimal places
Serial.print("Duration (ms) = ");
Serial.println(duration);
// Update the LCD with the name and measured time
lcd.clear(); // Clear previous content
lcd.setCursor(0, 0);
lcd.print(myName); // First line: name
lcd.setCursor(0, 1);
lcd.print("Time: ");
lcd.print(duration);
lcd.print(" ms"); // Second line: e.g., "Time: 1234 ms"
// Wait so the result can be read (adjust DISPLAY_MS as needed)
delay(DISPLAY_MS);
}
// ========== Benchmark Function ==========
unsigned long runBenchmark(double &result) {
unsigned long start = millis(); // Record start time
double sum = 0.0;
for (int i = 1; i <= BENCH_LOOPS; i++) {
// Note: On Arduino, double is the same as float (4 bytes).
// The computation is deliberately heavy to produce a measurable time.
sum += sqrt((double)i) * sin((double)i);
}
unsigned long elapsed = millis() - start; // Compute elapsed time
result = sum; // Store the sum via reference
return elapsed;
}