#include <Adafruit_SSD1306.h>
#include <math.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup()
{
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3c);
display.clearDisplay();
display.setTextSize(1); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
display.setCursor(4, 4);
display.println(F("f(x) = x^2"));
display.drawLine(64, 0, 64, 64, SSD1306_WHITE);
display.drawLine(0, 32, 128, 32, SSD1306_WHITE);
display.display();
// Set the origin (0,0) to the center of the display
int originX = SCREEN_WIDTH / 2;
int originY = SCREEN_HEIGHT / 2;
// Scale factor to fit the parable in the screen
float scale = 0.1;
// Variables to store the previous point
int prevX = -originX;
int prevY = scale * prevX * prevX;
// Draw the parable y = x^2 using lines
for (int x = -originX + 1; x < originX; x++) {
int y = scale * x * x;
if (originY - y >= 0
&& originY - y < SCREEN_HEIGHT
&& originY - prevY >= 0
&& originY - prevY < SCREEN_HEIGHT) {
display.drawLine(originX + prevX, originY - prevY, originX + x, originY - y, SSD1306_WHITE);
}
// Update previous point
prevX = x;
prevY = y;
}
display.display();
}
void loop()
{
}