OLEDWave was written to utilize any 128x64 display. This is a very simple example showing how to smoothly generate a Sine and Cosine wave on an OLED Display. Draws Sine and Cosine wave pixel by pixel and starts over. Will continuously draw until power is removed.
See animation at work at: OLEDWave - Arduino Drawing Sine and Cosine Waves on OLED - YouTube
Fritzing diagram:
/*
OLEDWave
OLEDWave was written to utilize any 128x64 display. This is a very simple example showing how to smoothly
generate a Sine and Cosine wave on an OLED Display. This code was written by Greg Stievenart with no claim
to the information provided in this code. Please feel free to copy, modify and share. Freely published
October 19, 2019.
The units in this example are broken down into an imaginary circle with a radius of 30. This number
is multiplied by the Sine and the Cosine of the wave.
EXAMPLE:
60/2 ** ** ** **
* * * * * * * *
* * * * * * * *
0 * ---------- * -------- * ---------- * -------- * ---------- * ---------- * ---------- *
* * * * * *
* * * * * *
-60/2 ** ** **
More information about the Sine fuction: https://en.wikipedia.org/wiki/Sine
This project is posted at: https://forum.arduino.cc/index.php?topic=641852
*/
#include <Wire.h> // requried to run I2C SH1106
#include <SPI.h> // requried to run I2C SH1106
#include <Adafruit_GFX.h> // https://github.com/adafruit/Adafruit-GFX-Library
#include <Adafruit_SH1106.h> // https://github.com/wonho-maker/Adafruit_SH1106
#define OLED_RESET 4
Adafruit_SH1106 display(OLED_RESET); // reset required for SH1106
int hCenter = 32; // horizontal center of 64 / 2 = 32
int Radius = 30; // radius of circle
const float Pi = 3.14159265359; // Pi
void setup(){
display.begin(SH1106_SWITCHCAPVCC, 0x3D); // needed for SH1106 display
}
void loop(){
display.clearDisplay(); // clears display
display.drawRect(0, 0, 120, 64, WHITE); // draws border
display.drawLine(0,30,120,30,WHITE); // draws grid horizontal center
display.drawLine(30,0,30,64,WHITE); // draws grid vertical first
display.drawLine(60,0,60,64,WHITE); // draws grid vertical second
display.drawLine(90,0,90,64,WHITE); // draws grid vertical third
for (int i=0; i<120; i++){ // draws 120 pixels per loop
float Angle = i * 3; // 120 X 3 = 360°
int a = (hCenter + (sin(Angle * (Pi / 180)) * Radius)); // Pi/180 converts degrees to radians
int b = (hCenter + (cos(Angle * (Pi / 180)) * Radius)); // Pi/180 converts degrees to radians
display.drawPixel(i,a,WHITE); // draws each pixel for Sine wave
display.drawPixel(i,b,WHITE); // draws each pixel for Cosine wave
display.display(); // displays new screen information
}
}
OLEDWave.ino (3.3 KB)