Created
September 23, 2025 08:26
-
-
Save peterhellberg/c9e0dab2bd981164884145603e2f4ece to your computer and use it in GitHub Desktop.
Small example of using the SparkFun MicroView library. Hardware: https://www.sparkfun.com/sparkfun-microview-oled-arduino-module.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /****************************************************************************** | |
| MicroViewBounce.ino | |
| This sketch uses the MicroView library. | |
| ******************************************************************************/ | |
| #include <MicroView.h> | |
| MicroView m; | |
| struct State { | |
| int8_t x; | |
| int8_t y; | |
| int8_t ax; | |
| int8_t ay; | |
| uint8_t f; | |
| bool m; | |
| }; | |
| State s = { | |
| .x = 5, | |
| .y = 5, | |
| .ax = -1, | |
| .ay = 1, | |
| .f = 0, | |
| .m = true, | |
| }; | |
| char buf[32]; | |
| void setup() { | |
| Serial.begin(9600); | |
| m.begin(); | |
| m.clear(ALL); | |
| } | |
| void loop() { | |
| input(); | |
| update(); | |
| if (s.f % 2 == 0) draw(); | |
| delay(16); | |
| } | |
| void input() { | |
| if (Serial.available() > 0) { | |
| char c = Serial.read(); | |
| switch(c) { | |
| case ' ': | |
| s.m = !s.m; | |
| break; | |
| case 'w': | |
| s.ay -= 2; | |
| break; | |
| case 's': | |
| s.ay += 2; | |
| break; | |
| case 'a': | |
| s.ax -= 2; | |
| break; | |
| case 'd': | |
| s.ax += 2; | |
| break; | |
| } | |
| } | |
| } | |
| void update() { | |
| if (s.ax < 0) s.ax = -1; | |
| if (s.ax > 0) s.ax = 1; | |
| if (s.ay < 0) s.ay = -1; | |
| if (s.ay > 0) s.ay = 1; | |
| if (s.x < 1 || s.x > 63) s.ax = s.ax * -1; | |
| if (s.y < 1 || s.y > 47) s.ay = s.ay * -1; | |
| if (s.x < 0 || s.x > 64) s.x = 32; | |
| if (s.y < 0 || s.y > 48) s.y = 24; | |
| if (s.m) { | |
| s.x += s.ax; | |
| s.y += s.ay; | |
| sprintf(buf, "%02dx%02d", s.x, s.y); | |
| Serial.println(buf); | |
| } | |
| s.f += 1; | |
| } | |
| void draw() { | |
| m.clear(PAGE, ' '); | |
| m.setCursor(34,1); | |
| m.print(buf); | |
| m.circleFill(s.x, s.y, 6, BLACK, NORM); | |
| m.circle(s.x, s.y, 7, WHITE, NORM); | |
| m.circleFill(s.x, s.y, 3, WHITE, NORM); | |
| m.display(); | |
| } |
Author
peterhellberg
commented
Sep 23, 2025
Author
Slightly expanded with two buttons wired up to Aduino pins 2 and 3.
/******************************************************************************
MicroViewBounce.ino
This sketch uses the MicroView library.
******************************************************************************/
#include <MicroView.h>
MicroView m;
struct State {
int8_t x;
int8_t y;
int8_t ax;
int8_t ay;
uint8_t f;
bool m;
bool b1;
bool b2;
};
State s = {
.x = 5,
.y = 5,
.ax = -1,
.ay = 1,
.f = 0,
.m = true,
.b1 = false,
.b2 = false,
};
char buf[32];
const int button1Pin = 2;
const int button2Pin = 3;
void setup() {
Serial.begin(9600);
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
m.begin();
m.clear(ALL);
}
void loop() {
input();
update();
if (s.f % 2 == 0) draw();
delay(16);
}
void input() {
s.b1 = !digitalRead(button1Pin);
s.b2 = !digitalRead(button2Pin);
if(s.b1) s.ax -= 2;
if(s.b2) s.ax += 2;
if (Serial.available() > 0) {
char c = Serial.read();
switch(c) {
case ' ':
s.m = !s.m;
break;
case 'w':
s.ay -= 2;
break;
case 's':
s.ay += 2;
break;
case 'a':
s.ax -= 2;
break;
case 'd':
s.ax += 2;
break;
}
}
}
void update() {
if (s.ax < 0) s.ax = -1;
if (s.ax > 0) s.ax = 1;
if (s.ay < 0) s.ay = -1;
if (s.ay > 0) s.ay = 1;
if (s.x < 1 || s.x > 63) s.ax = s.ax * -1;
if (s.y < 1 || s.y > 47) s.ay = s.ay * -1;
if (s.x < 0 || s.x > 64) s.x = 32;
if (s.y < 0 || s.y > 48) s.y = 24;
if (s.m) {
s.x += s.ax;
s.y += s.ay;
sprintf(buf, "%02dx%02d", s.x, s.y);
Serial.println(buf);
}
s.f += 1;
}
void draw() {
m.clear(PAGE, ' ');
m.setCursor(34,1);
m.print(buf);
m.circleFill(s.x, s.y, 6, BLACK, NORM);
m.circle(s.x, s.y, 7, WHITE, NORM);
m.circleFill(s.x, s.y, 3, WHITE, NORM);
if (s.b1) m.circle(s.x, s.y, 12, WHITE, NORM);
if (s.b2) m.circle(s.x, s.y, 16, WHITE, NORM);
m.display();
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
