In this video I show you how you can easily plot any graph or data onto any OLED display. We create a mathematical function which will map the raw data onto the screen. We demonstrate the function with a sin wave, but the function will work with any equation or data. This is the code which we developed in the video.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
#include <SPI.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> float rawXdata[128]; int normXdata[128]; float rawYdata[128]; int normYdata[128]; float xMin = 0; float xMax = 2 * 3.1415; float yMin = -1; float yMax = 1; float Pt = 0; float Pb = 63; float Pl = 0; float Pr = 127; float pi = 3.14159265; int numPoints = 128; int screenWidth = 128; int screenHeight = 64; int oledReset = -1; #define SCREEN_ADDRESS 0x3C Adafruit_SSD1306 oled(screenWidth, screenHeight, &Wire, oledReset); int j=0; void setup() { // put your setup code here, to run once: Serial.begin(115200); oled.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS); oled.clearDisplay(); oled.display(); } void loop() { // put your main code here, to run repeatedly: for (int i = 0; i < numPoints; i = i + 1) { rawXdata[i] = i * (2 * pi / 127); rawYdata[i] = sin(rawXdata[i]); Serial.println(rawYdata[i]); delay(100); } normData(); plotGraph(); oled.display(); } void normData() { for (int i = 0; i < numPoints; i = i + 1) { normYdata[i] = (Pb - Pt) / (yMin - yMax) * (rawYdata[i] - yMax) + Pt; normXdata[i] = (Pr - Pl) / (xMax - xMin) * (rawXdata[i] - xMin) + Pl; } } void plotGraph(){ oled.drawLine(Pl,Pt,Pl,Pb,WHITE); oled.drawLine(Pl,Pb,Pr,Pb,WHITE); for (int i=0;i<numPoints-1;i=i+1){ oled.drawLine(normXdata[i],normYdata[i],normXdata[i+1],normYdata[i+1],WHITE); } } |