#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_ILI9341.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)
#define TFT_DC 9
#define TFT_CS 10
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
const float Pi = 3.14159265359;
int Radius = SCREEN_HEIGHT / 2;
float toRadians(float degrees) {
return degrees * (Pi / 180);
}
int findYcoord(float Angle, float offset, int screenX, int screenY) {
return floor(screenY / 2 + (sin(toRadians(Angle + offset)) * (screenY/2)));
}
void drawSineILI9341(float offset) {
float Angle;
int screenX = 320;
int screenY = 240;
int prevX = 0, prevY = findYcoord(Angle, offset, screenX, screenY);
//tft.fillScreen(ILI9341_BLACK);
for (float x=0; x<=screenX; x++) {
Angle = x*1.125;
int y = findYcoord(Angle, offset, screenX, screenY);
tft.drawLine(prevX, prevY, x, y, ILI9341_WHITE);
prevX = x;
prevY = y;
}
display.display();
}
void drawSine(float offset) {
float Angle;
float prevX = 0.0f, prevY = findYcoord(Angle, offset, SCREEN_WIDTH, SCREEN_HEIGHT);
display.clearDisplay();
for (float x=0; x<=SCREEN_WIDTH; x++) {
Angle = x * 2.8125;
int y = findYcoord(Angle, offset, SCREEN_WIDTH, SCREEN_HEIGHT);
display.drawLine(prevX, prevY, x, y, WHITE);
prevX = x;
prevY = y;
}
display.display();
}
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
pinMode(A0, INPUT);
tft.begin();
tft.setRotation(1);
display.display();
drawSine(0.0f);
}
float offset, prevOffset;
void loop() {
int potentiometer = analogRead(A0);
prevOffset = offset;
offset = potentiometer * 0.3515625f;
if(prevOffset != offset) {
drawSine(offset);
}
}