Go Down

Topic: OLEDWave - Sine & Cosine Animation on OLED SH1106 (Read 1 time) previous topic - next topic

stievenart

Oct 20, 2019, 05:56 pm Last Edit: Oct 21, 2019, 01:40 pm by stievenart
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: https://youtu.be/BEwXZORL3js

Fritzing diagram:


Code: [Select]
/*

  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
  }
}

TheMemberFormerlyKnownAsAWOL

Code: [Select]
const float Pi = 3.14159265359;
M_PI is already defined for you.
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

stievenart

#2
Oct 21, 2019, 01:38 pm Last Edit: Oct 21, 2019, 01:41 pm by stievenart
Code: [Select]
const float Pi = 3.14159265359;
M_PI is already defined for you.
I have used "M_PI" on previous projects. I just want to show that it is not the only way to use Pi. Besides, It's easier to understand when it is written out longhand 3.14159265359

Juanpablocanguro

For some reason Stievenart's code for this runs very smoothly on my setup, however, when I try your code for the cube, the whole image grows out of the OLED screen.... why is that? Also, can you provide code for your dodecahedron?

Go Up