Skip to content

Instantly share code, notes, and snippets.

@hkstemclub
Last active April 10, 2018 18:30
Show Gist options
  • Save hkstemclub/29ceae2c1e0029fc025ac2ffb537c865 to your computer and use it in GitHub Desktop.
Save hkstemclub/29ceae2c1e0029fc025ac2ffb537c865 to your computer and use it in GitHub Desktop.
/*
Modify version of https://www.youtube.com/watch?v=7ph5ov56cfg
Code will be used to display a bar graph from measured input voltage from pin A0.
The purpose of this program is to test and demonstrate the display performance of the 128 x 64 display
The display is a Balance world Inc 0.96" Inch Yellow and Blue I2c IIC Serial 128x64 Oled LCD Oled LED Module for
Arduino Display 51 Msp420 Stim32 SCR
This could tested with a Mega but should work with an UNO
Kit hkstem.club
Pin connections
Arduino device
A0 Voltage read (10K pot across +5, and ground)
A1
A2
A3
A4 SDA (if no SDA pin)
A5 SCL (if not SCL pin)
1
2
3
4
5
6
7
8
9
10
11
12
13
SDA SDA
SLC SLC
Graphics Libraries
https://github.com/adafruit/Adafruit-GFX-Library
https://github.com/adafruit/Adafruit_SSD1306
Modify Adafruit_SSD1306.h Line #73 to be like
#define SSD1306_128_64
// #define SSD1306_128_32
// #define SSD1306_96_16
display buy:
http://hkstem.club/product/096oled
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
#define ADJ_PIN A0
Adafruit_SSD1306 Display(OLED_RESET);
int r = 0;
int i = 0;
void setup() {
Display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
// i'll follow the license agreement and display the Adafruit logo
// and since they were nice enough to supply the libraries
Display.clearDisplay();
Display.display();
delay (1000);
DrawTitles();
}
void loop() {
// get some dummy data to display
//r = rand() / 220;
r = analogRead(ADJ_PIN);
r = r / 7.98;
Display.setTextSize(2);
// note set the background color or the old text will still display
Display.setTextColor(WHITE, BLACK);
Display.setCursor(0, 33);
Display.println(Format(r * 7.99 / 204.6 , 3, 2));
//draw the bar graph
Display.fillRect(r, 50, 128 - r, 10, BLACK);
Display.fillRect(3, 50, r, 10, WHITE);
for (i = 1; i < 13; i++) {
Display.fillRect(i * 10, 50, 2, 10, BLACK);
}
// now that the display is build, display it...
Display.display();
}
void DrawTitles(void) {
Display.setTextSize(2);
Display.setTextColor(WHITE);
Display.setCursor(0, 0);
Display.println("Voltage");
Display.setTextSize(1);
Display.setTextColor(WHITE);
Display.setCursor(0, 19);
Display.println("hkstem.club");
//Display.println("Random number");
Display.display();
}
String Format(double val, int dec, int dig ) {
// this is my simple way of formatting a number
// data = Format(number, digits, decimals) when needed
int addpad = 0;
char sbuf[20];
String fdata = (dtostrf(val, dec, dig, sbuf));
int slen = fdata.length();
for ( addpad = 1; addpad <= dec + dig - slen; addpad++) {
fdata = " " + fdata;
}
return (fdata);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment