#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#define LCD_ADDRESS 0x27
#define LCD_COL 20
#define LCD_ROW 4
// f(0) as 1 or 0 depends on classical vs combinatorial definition
// of Fibonacci Sequence
unsigned long f1 = 1;
unsigned long f2 = 1;
unsigned int n = 1;
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_ROW, LCD_COL);
unsigned long fibonacci(unsigned long &f1, unsigned long &f2) {
unsigned long fn = f2 + f1;
f1 = f2;
f2 = fn;
return fn;
}
void setup() {
// put your setup code here, to run once:
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Fibonacci Series");
}
void loop() {
// put your main code here, to run repeatedly:
if (n <= LCD_COL) {
// lcd.clear() causes screen flashing
// lcd.print(" ") is an alternative
for (byte i = 0; i < n && i < LCD_COL; i++) {
lcd.setCursor(i, 1);
lcd.print("\xFF");
// for hex codes
// check https://docs.wokwi.com/parts/wokwi-lcd1602#a00-variant
}
lcd.setCursor(0, 2);
switch (n) {
case 1:
lcd.print(f1);
break;
case 2:
lcd.print(f2);
break;
default:
lcd.print(fibonacci(f1, f2));
}
n++;
delay(1000);
} else {
lcd.setCursor(0, 3);
lcd.print("First " + String(LCD_COL) + " Terms");
}
}