Skip to content

Commit fdf7842

Browse files
committed
Add Comment and Wire.begin()
- Add Wire.begin() to the setup() in the examples since it was removed from the .cpp file. - Adding comments to clarify on when to use certain functions for SPI and I2C
1 parent c768ea4 commit fdf7842

File tree

5 files changed

+203
-145
lines changed

5 files changed

+203
-145
lines changed

examples/MicroOLED_Clock/MicroOLED_Clock.ino

+49-34
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
/****************************************************************
2-
* MicroOLED_Clock.ino
3-
* Analog Clock demo using SFE_MicroOLED Library
4-
* Jim Lindblom @ SparkFun Electronics
5-
* Original Creation Date: October 27, 2014
6-
*
7-
* This sketch uses the MicroOLED library to draw a 3-D projected
8-
* cube, and rotate it along all three axes.
9-
*
10-
* Development environment specifics:
11-
* Arduino 1.0.5
12-
* Arduino Pro 3.3V
13-
* Micro OLED Breakout v1.0
14-
*
15-
* This code is beerware; if you see me (or any other SparkFun employee) at the
16-
* local, and you've found our code helpful, please buy us a round!
17-
*
18-
* Distributed as-is; no warranty is given.
2+
MicroOLED_Clock.ino
3+
Analog Clock demo using SFE_MicroOLED Library
4+
Jim Lindblom @ SparkFun Electronics
5+
Original Creation Date: October 27, 2014
6+
7+
This sketch uses the MicroOLED library to draw a 3-D projected
8+
cube, and rotate it along all three axes.
9+
10+
Development environment specifics:
11+
Arduino 1.0.5
12+
Arduino Pro 3.3V
13+
Micro OLED Breakout v1.0
14+
15+
This code is beerware; if you see me (or any other SparkFun employee) at the
16+
local, and you've found our code helpful, please buy us a round!
17+
18+
Distributed as-is; no warranty is given.
1919
***************************************************************/
2020
#include <Wire.h> // Include Wire if you're using I2C
2121
#include <SPI.h> // Include SPI if you're using SPI
@@ -27,13 +27,19 @@
2727
#define PIN_RESET 9 // Connect RST to pin 9 (SPI & I2C)
2828
#define PIN_DC 8 // Connect DC to pin 8 (SPI only)
2929
#define PIN_CS 10 // Connect CS to pin 10 (SPI only)
30-
#define DC_JUMPER 0 // DC jumper setting(I2C only)
30+
#define DC_JUMPER 0 // Set to either 0 (SPI, default) or 1 (I2C) based on jumper, matching the value of the DC Jumper
31+
// Also connect pin 13 to SCK and pin 11 to MOSI
3132

3233
//////////////////////////////////
3334
// MicroOLED Object Declaration //
3435
//////////////////////////////////
35-
MicroOLED oled(PIN_RESET, PIN_DC, PIN_CS); // SPI Example
36-
//MicroOLED oled(PIN_RESET, DC_JUMPER); // I2C Example
36+
// Declare a MicroOLED object. The parameters include:
37+
// 1 - Reset pin: Any digital pin
38+
// 2 - D/C pin: Any digital pin (SPI mode only)
39+
// 3 - CS pin: Any digital pin (SPI mode only, 10 recommended)
40+
41+
MicroOLED oled(PIN_RESET, PIN_DC, PIN_CS); //Example SPI declaration, comment out if using I2C
42+
//MicroOLED oled(PIN_RESET, DC_JUMPER); //Example I2C declaration, uncomment if using I2C
3743

3844
// Use these variables to set the initial time
3945
int hours = 11;
@@ -67,27 +73,36 @@ void initClockVariables()
6773
POS_12_X = MIDDLE_X - oled.getFontWidth();
6874
POS_12_Y = MIDDLE_Y - CLOCK_RADIUS + 2;
6975
POS_3_X = MIDDLE_X + CLOCK_RADIUS - oled.getFontWidth() - 1;
70-
POS_3_Y = MIDDLE_Y - oled.getFontHeight()/2;
71-
POS_6_X = MIDDLE_X - oled.getFontWidth()/2;
76+
POS_3_Y = MIDDLE_Y - oled.getFontHeight() / 2;
77+
POS_6_X = MIDDLE_X - oled.getFontWidth() / 2;
7278
POS_6_Y = MIDDLE_Y + CLOCK_RADIUS - oled.getFontHeight() - 1;
7379
POS_9_X = MIDDLE_X - CLOCK_RADIUS + oled.getFontWidth() - 2;
74-
POS_9_Y = MIDDLE_Y - oled.getFontHeight()/2;
75-
80+
POS_9_Y = MIDDLE_Y - oled.getFontHeight() / 2;
81+
7682
// Calculate clock arm lengths
7783
S_LENGTH = CLOCK_RADIUS - 2;
7884
M_LENGTH = S_LENGTH * 0.7;
7985
H_LENGTH = S_LENGTH * 0.5;
8086
}
8187

88+
// I2C is great, but will result in a much slower update rate. The
89+
// slower framerate may be a worthwhile tradeoff, if you need more
90+
// pins, though.
91+
8292
void setup()
8393
{
94+
delay(100);
95+
//Wire.begin(); //set up I2C bus, uncomment if you are using I2C
96+
// These three lines of code are all you need to initialize the
97+
// OLED and print the splash screen.
98+
8499
oled.begin(); // Initialize the OLED
85100
oled.clear(PAGE); // Clear the display's internal memory
86101
oled.clear(ALL); // Clear the library's display buffer
87102
oled.display(); // Display what's in the buffer (splashscreen)
88-
103+
89104
initClockVariables();
90-
105+
91106
oled.clear(ALL);
92107
drawFace();
93108
drawArms(hours, minutes, seconds);
@@ -96,14 +111,14 @@ void setup()
96111

97112
void loop()
98113
{
99-
114+
100115
// Check if we need to update seconds, minutes, hours:
101116
if (lastDraw + CLOCK_SPEED < millis())
102117
{
103118
lastDraw = millis();
104119
// Add a second, update minutes/hours if necessary:
105120
updateTime();
106-
121+
107122
// Draw the clock:
108123
oled.clear(PAGE); // Clear the buffer
109124
drawFace(); // Draw the face to the buffer
@@ -138,7 +153,7 @@ void drawArms(int h, int m, int s)
138153
{
139154
double midHours; // this will be used to slightly adjust the hour hand
140155
static int hx, hy, mx, my, sx, sy;
141-
156+
142157
// Adjust time to shift display 90 degrees ccw
143158
// this will turn the clock the same direction as text:
144159
h -= 3;
@@ -150,21 +165,21 @@ void drawArms(int h, int m, int s)
150165
m += 60;
151166
if (s < 0)
152167
s += 60;
153-
168+
154169
// Calculate and draw new lines:
155170
s = map(s, 0, 60, 0, 360); // map the 0-60, to "360 degrees"
156171
sx = S_LENGTH * cos(PI * ((float)s) / 180); // woo trig!
157172
sy = S_LENGTH * sin(PI * ((float)s) / 180); // woo trig!
158173
// draw the second hand:
159174
oled.line(MIDDLE_X, MIDDLE_Y, MIDDLE_X + sx, MIDDLE_Y + sy);
160-
175+
161176
m = map(m, 0, 60, 0, 360); // map the 0-60, to "360 degrees"
162177
mx = M_LENGTH * cos(PI * ((float)m) / 180); // woo trig!
163178
my = M_LENGTH * sin(PI * ((float)m) / 180); // woo trig!
164179
// draw the minute hand
165180
oled.line(MIDDLE_X, MIDDLE_Y, MIDDLE_X + mx, MIDDLE_Y + my);
166-
167-
midHours = minutes/12; // midHours is used to set the hours hand to middling levels between whole hours
181+
182+
midHours = minutes / 12; // midHours is used to set the hours hand to middling levels between whole hours
168183
h *= 5; // Get hours and midhours to the same scale
169184
h += midHours; // add hours and midhours
170185
h = map(h, 0, 60, 0, 360); // map the 0-60, to "360 degrees"
@@ -179,7 +194,7 @@ void drawFace()
179194
{
180195
// Draw the clock border
181196
oled.circle(MIDDLE_X, MIDDLE_Y, CLOCK_RADIUS);
182-
197+
183198
// Draw the clock numbers
184199
oled.setFontType(0); // set font type 0, please see declaration in SFE_MicroOLED.cpp
185200
oled.setCursor(POS_12_X, POS_12_Y); // points cursor to x=27 y=0
+73-55
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
/******************************************************************************
2-
* MicroOLED_Cube.ino
3-
* Rotating a 3-D Cube on the MicroOLED Breakout
4-
* Jim Lindblom @ SparkFun Electronics
5-
* Original Creation Date: October 27, 2014
6-
*
7-
* This sketch uses the MicroOLED library to draw a 3-D projected
8-
* cube, and rotate it along all three axes.
9-
*
10-
* Development environment specifics:
11-
* Arduino 1.0.5
12-
* Arduino Pro 3.3V
13-
* Micro OLED Breakout v1.0
14-
*
15-
* This code is beerware; if you see me (or any other SparkFun employee) at the
16-
* local, and you've found our code helpful, please buy us a round!
17-
*
18-
* Distributed as-is; no warranty is given.
2+
MicroOLED_Cube.ino
3+
Rotating a 3-D Cube on the MicroOLED Breakout
4+
Jim Lindblom @ SparkFun Electronics
5+
Original Creation Date: October 27, 2014
6+
7+
This sketch uses the MicroOLED library to draw a 3-D projected
8+
cube, and rotate it along all three axes.
9+
10+
Development environment specifics:
11+
Arduino 1.0.5
12+
Arduino Pro 3.3V
13+
Micro OLED Breakout v1.0
14+
15+
This code is beerware; if you see me (or any other SparkFun employee) at the
16+
local, and you've found our code helpful, please buy us a round!
17+
18+
Distributed as-is; no warranty is given.
1919
******************************************************************************/
2020
#include <Wire.h> // Include Wire if you're using I2C
2121
#include <SPI.h> // Include SPI if you're using SPI
@@ -27,43 +27,61 @@
2727
#define PIN_RESET 9 // Connect RST to pin 9
2828
#define PIN_DC 8 // Connect DC to pin 8
2929
#define PIN_CS 10 // Connect CS to pin 10
30-
#define DC_JUMPER 0
30+
#define DC_JUMPER 0 // Set to either 0 (SPI, default) or 1 (I2C) based on jumper, matching the value of the DC Jumper
31+
// Also connect pin 13 to SCK and pin 11 to MOSI
3132

3233
//////////////////////////////////
3334
// MicroOLED Object Declaration //
3435
//////////////////////////////////
35-
MicroOLED oled(PIN_RESET, PIN_DC, PIN_CS); // SPI declaration
36-
//MicroOLED oled(PIN_RESET, DC_JUMPER); // I2C declaration
36+
// Declare a MicroOLED object. The parameters include:
37+
// 1 - Reset pin: Any digital pin
38+
// 2 - D/C pin: Any digital pin (SPI mode only)
39+
// 3 - CS pin: Any digital pin (SPI mode only, 10 recommended)
40+
41+
MicroOLED oled(PIN_RESET, PIN_DC, PIN_CS); //Example SPI declaration, comment out if using I2C
42+
//MicroOLED oled(PIN_RESET, DC_JUMPER); //Example I2C declaration, uncomment if using I2C
3743

3844
int SCREEN_WIDTH = oled.getLCDWidth();
3945
int SCREEN_HEIGHT = oled.getLCDHeight();
4046

4147
float d = 3;
42-
float px[] = {
43-
-d, d, d, -d, -d, d, d, -d };
44-
float py[] = {
45-
-d, -d, d, d, -d, -d, d, d };
46-
float pz[] = {
47-
-d, -d, -d, -d, d, d, d, d };
48+
float px[] = {
49+
-d, d, d, -d, -d, d, d, -d
50+
};
51+
float py[] = {
52+
-d, -d, d, d, -d, -d, d, d
53+
};
54+
float pz[] = {
55+
-d, -d, -d, -d, d, d, d, d
56+
};
4857

4958
float p2x[] = {
50-
0,0,0,0,0,0,0,0};
59+
0, 0, 0, 0, 0, 0, 0, 0
60+
};
5161
float p2y[] = {
52-
0,0,0,0,0,0,0,0};
62+
0, 0, 0, 0, 0, 0, 0, 0
63+
};
5364

5465
float r[] = {
55-
0,0,0};
66+
0, 0, 0
67+
};
5668

5769
#define SHAPE_SIZE 600
5870
// Define how fast the cube rotates. Smaller numbers are faster.
5971
// This is the number of ms between draws.
6072
#define ROTATION_SPEED 0
6173

74+
// I2C is great, but will result in a much slower update rate. The
75+
// slower framerate may be a worthwhile tradeoff, if you need more
76+
// pins, though.
6277
void setup()
6378
{
79+
delay(100);
80+
//Wire.begin(); //set up I2C bus, uncomment if you are using I2C
81+
6482
oled.begin();
6583
oled.clear(ALL);
66-
oled.display();
84+
oled.display();
6785
}
6886

6987
void loop()
@@ -74,41 +92,41 @@ void loop()
7492

7593
void drawCube()
7694
{
77-
r[0]=r[0]+PI/180.0; // Add a degree
78-
r[1]=r[1]+PI/180.0; // Add a degree
79-
r[2]=r[2]+PI/180.0; // Add a degree
80-
if (r[0] >= 360.0*PI/180.0) r[0] = 0;
81-
if (r[1] >= 360.0*PI/180.0) r[1] = 0;
82-
if (r[2] >= 360.0*PI/180.0) r[2] = 0;
83-
84-
for (int i=0;i<8;i++)
95+
r[0] = r[0] + PI / 180.0; // Add a degree
96+
r[1] = r[1] + PI / 180.0; // Add a degree
97+
r[2] = r[2] + PI / 180.0; // Add a degree
98+
if (r[0] >= 360.0 * PI / 180.0) r[0] = 0;
99+
if (r[1] >= 360.0 * PI / 180.0) r[1] = 0;
100+
if (r[2] >= 360.0 * PI / 180.0) r[2] = 0;
101+
102+
for (int i = 0; i < 8; i++)
85103
{
86104
float px2 = px[i];
87-
float py2 = cos(r[0])*py[i] - sin(r[0])*pz[i];
88-
float pz2 = sin(r[0])*py[i] + cos(r[0])*pz[i];
105+
float py2 = cos(r[0]) * py[i] - sin(r[0]) * pz[i];
106+
float pz2 = sin(r[0]) * py[i] + cos(r[0]) * pz[i];
89107

90-
float px3 = cos(r[1])*px2 + sin(r[1])*pz2;
108+
float px3 = cos(r[1]) * px2 + sin(r[1]) * pz2;
91109
float py3 = py2;
92-
float pz3 = -sin(r[1])*px2 + cos(r[1])*pz2;
110+
float pz3 = -sin(r[1]) * px2 + cos(r[1]) * pz2;
93111

94-
float ax = cos(r[2])*px3 - sin(r[2])*py3;
95-
float ay = sin(r[2])*px3 + cos(r[2])*py3;
96-
float az = pz3-150;
112+
float ax = cos(r[2]) * px3 - sin(r[2]) * py3;
113+
float ay = sin(r[2]) * px3 + cos(r[2]) * py3;
114+
float az = pz3 - 150;
97115

98-
p2x[i] = SCREEN_WIDTH/2+ax*SHAPE_SIZE/az;
99-
p2y[i] = SCREEN_HEIGHT/2+ay*SHAPE_SIZE/az;
116+
p2x[i] = SCREEN_WIDTH / 2 + ax * SHAPE_SIZE / az;
117+
p2y[i] = SCREEN_HEIGHT / 2 + ay * SHAPE_SIZE / az;
100118
}
101119

102120
oled.clear(PAGE);
103-
for (int i=0;i<3;i++)
121+
for (int i = 0; i < 3; i++)
104122
{
105-
oled.line(p2x[i],p2y[i],p2x[i+1],p2y[i+1]);
106-
oled.line(p2x[i+4],p2y[i+4],p2x[i+5],p2y[i+5]);
107-
oled.line(p2x[i],p2y[i],p2x[i+4],p2y[i+4]);
108-
}
109-
oled.line(p2x[3],p2y[3],p2x[0],p2y[0]);
110-
oled.line(p2x[7],p2y[7],p2x[4],p2y[4]);
111-
oled.line(p2x[3],p2y[3],p2x[7],p2y[7]);
123+
oled.line(p2x[i], p2y[i], p2x[i + 1], p2y[i + 1]);
124+
oled.line(p2x[i + 4], p2y[i + 4], p2x[i + 5], p2y[i + 5]);
125+
oled.line(p2x[i], p2y[i], p2x[i + 4], p2y[i + 4]);
126+
}
127+
oled.line(p2x[3], p2y[3], p2x[0], p2y[0]);
128+
oled.line(p2x[7], p2y[7], p2x[4], p2y[4]);
129+
oled.line(p2x[3], p2y[3], p2x[7], p2y[7]);
112130
oled.display();
113131
}
114132

0 commit comments

Comments
 (0)