It's my Birthday and i'll share if I want to!

I created a little dial sketch today using the 0.96 Inch I2C Serial 128 x 64 Display.
I’m new to all this so cut me some slack eh?
Just wanted to share!

:slight_smile: see it here…

I had an LDR connected to A0 for the test.

I hope i get this right…

SDA to Nano/Uno - A4
SCL to Nano/Uno -A5
VCC to 5V
GND to GND
LDR1 to A0 - LDR2 to GND - 560 ohm from LDR1 to 5V.

Has a wee 10 second calibration bit… then does the thing…

I like it :slight_smile:

Any questions?
Or code improvements?
Please shout out!

//use hardware SPI
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET  8
Adafruit_SSD1306 display(OLED_RESET);

// Set up our global variables
int sensorValue, sensorMax, sensorMin = 0;   // to hold the analog value
int sensorPin = A0;   // the input pin
int needleWidth = 4;   // the width of our dials needle
int divisions = 10;  // the wee dots
int scaleTickSize = 10;

void setup()   {
  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC);
  display.clearDisplay();

  // sensor calibration
  while (millis() < 10000) {
    digitalWrite(13, true);
    sensorValue = analogRead(sensorPin);
    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(10, 10);
    display.print("CALIBRATE SENSOR!");
    display.setCursor(10, 30);
    display.setTextSize(3);
    display.print(10 - (millis() / 1000));
    display.display();

    // record the maximum sensor value
    if (sensorValue > sensorMax) {
      sensorMax = sensorValue;
    }
    // record the minimum sensor value
    if (sensorValue < sensorMin) {
      sensorMin = sensorValue;
    }
  }

  // signal the end of the calibration period
  digitalWrite(13, LOW);
}

void loop() {
  sensorValue = map(analogRead(sensorPin), sensorMin, sensorMax, 180, 360);
  dial(sensorValue);
}

void dial(int value) {
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(10, 10);
  display.print(sensorValue - 180);
  display.drawRoundRect(0, 0 , SSD1306_LCDWIDTH , SSD1306_LCDHEIGHT  , 10, WHITE);
  display.drawRoundRect(2, 2 , SSD1306_LCDWIDTH - 4 , SSD1306_LCDHEIGHT - 4  , 10, WHITE);
  int r = 55; // needle size
  int x1 = 64; // center of screen
  int y1 = 64; // baseline
  //draw the scale ticks
  for (int n = 180; n < 360; n = n + divisions) {
    display.drawLine(64 + cos(n * (PI / 180)) * (r - scaleTickSize) , 64 + sin(n * (PI / 180)) * (r - scaleTickSize) , 64 + cos(n * (PI / 180)) * r , 64 + sin(n * (PI / 180)) * r , WHITE);
  }

  int x2 = 64 + cos(value * (PI / 180)) * r;
  int y2 = 64 + sin(value * (PI / 180)) * r;
  display.drawLine(x1, y1 - 3, x2, y2, WHITE);
  display.drawLine(x1 + needleWidth, y1 - 3, x2, y2, WHITE);
  display.drawLine(x1 - needleWidth, y1 - 3, x2, y2, WHITE);
  delay(10);
  display.display();
}

looks pretty good, some remarks

  • some variables are constants in fact, so better declare them as such e.g. const int sensorPin = A0;
    (also in the dial function)
    makes the compiler a bit better in optimizing the code

  • by convention digitalWrite(13, true); should be digitalWrite(13, HIGH);

  • you should keep the previous value, if the new one is the same you do not need to redraw
    => less flickering.

with the previous value you can also remove the old dial - and redraw the new, without redrawing the whole display.

  • this loop can be optimized I think
  for (int n = 180; n < 360; n = n + divisions) {
    display.drawLine(64 + cos(n * (PI / 180)) * (r - scaleTickSize) , 64 + sin(n * (PI / 180)) * (r - scaleTickSize) , 64 + cos(n * (PI / 180)) * r , 64 + sin(n * (PI / 180)) * r , WHITE);
  }

prevent some double math, and improve readability what happens.

for (int n = 180; n < 360; n = n + divisions) 
{
  float angle = n * (PI / 180);
  float radius = r - scaleTickSize;
    display.drawLine(64 + cos(angle) * radius , 64 + sin(angle) * radius , 
                            64 + cos(angle) * r , 64 + sin(angle) * r , WHITE);
  }

my 2 cents,

Thanks for the feedback - it's appreciated!

Thanks for sharing and happy birthday to you. :)