1
1
/* *****************************************************************************
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.
19
19
******************************************************************************/
20
20
#include < Wire.h> // Include Wire if you're using I2C
21
21
#include < SPI.h> // Include SPI if you're using SPI
27
27
#define PIN_RESET 9 // Connect RST to pin 9
28
28
#define PIN_DC 8 // Connect DC to pin 8
29
29
#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
31
32
32
33
// ////////////////////////////////
33
34
// MicroOLED Object Declaration //
34
35
// ////////////////////////////////
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
37
43
38
44
int SCREEN_WIDTH = oled.getLCDWidth();
39
45
int SCREEN_HEIGHT = oled.getLCDHeight();
40
46
41
47
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
+ };
48
57
49
58
float p2x[] = {
50
- 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 };
59
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
60
+ };
51
61
float p2y[] = {
52
- 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 };
62
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
63
+ };
53
64
54
65
float r[] = {
55
- 0 ,0 ,0 };
66
+ 0 , 0 , 0
67
+ };
56
68
57
69
#define SHAPE_SIZE 600
58
70
// Define how fast the cube rotates. Smaller numbers are faster.
59
71
// This is the number of ms between draws.
60
72
#define ROTATION_SPEED 0
61
73
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.
62
77
void setup ()
63
78
{
79
+ delay (100 );
80
+ // Wire.begin(); //set up I2C bus, uncomment if you are using I2C
81
+
64
82
oled.begin ();
65
83
oled.clear (ALL);
66
- oled.display ();
84
+ oled.display ();
67
85
}
68
86
69
87
void loop ()
@@ -74,41 +92,41 @@ void loop()
74
92
75
93
void drawCube ()
76
94
{
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++)
85
103
{
86
104
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];
89
107
90
- float px3 = cos (r[1 ])* px2 + sin (r[1 ])* pz2;
108
+ float px3 = cos (r[1 ]) * px2 + sin (r[1 ]) * pz2;
91
109
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;
93
111
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 ;
97
115
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;
100
118
}
101
119
102
120
oled.clear (PAGE);
103
- for (int i= 0 ;i< 3 ; i++)
121
+ for (int i = 0 ; i < 3 ; i++)
104
122
{
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 ]);
112
130
oled.display ();
113
131
}
114
132
0 commit comments