#include <TinyWireM.h>
#include <Tiny4kOLED.h>
// ============================================================================
const int dataPin = 1; /* Q7 */
const int clockPin = 3; /* CP */
const int latchPin = 4; /* PL */
bool buttons[7];
int linx = 48;
const int buzz = 1;
int lastTime;
const int spd = 8;
int px = 64;
int py = 32;
int dx = 8;
int dy = 8;
int puntos = 0;
void line(int x, int y, int l, int t) {
oled.setCursor(x, y); //Set position of line
oled.startData();
for (int n=0;n<=l;n++) {
oled.sendData(t); //Maximum 256 combinations
}
oled.endData();
}
void buttonUpdate() {
digitalWrite(latchPin, LOW);
digitalWrite(latchPin, HIGH);
// Step 2: Shift
for (int i = 0; i < 7; i++) {
if (digitalRead(dataPin)) {
buttons[i] = 1;
} else {
buttons[i] = 0;
}
digitalWrite(clockPin, HIGH); // Shift out the next bit
digitalWrite(clockPin, LOW);
}
}
void setup() {
oled.begin(128, 64, sizeof(tiny4koled_init_128x64r), tiny4koled_init_128x64r);
pinMode(dataPin, INPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
oled.setFont(FONT8X16);
}
void loop() {
oled.clear();
oled.setCursor(0,0);
oled.println(String(puntos));
oled.on();
oled.setCursor(px, py/8); //Set position of pixel
oled.startData();
oled.sendData(0x03);
oled.sendData(0x03);
oled.endData();
buttonUpdate();
line(linx, 7, 16, 3);
if (buttons[2]){
linx -= spd;
if (linx<-16) {
linx=120;
}
}
else if (buttons[1]){
linx += spd;
if (linx>127) {
linx=0;
}
}
if (px>=127) {
dx *= -1;
}
if (px<=0) {
dx *= -1;
}
if (py>=63) {
px = 64;
py = 32;
puntos = 0;
linx = 48;
dx = random(8)-random(8);
}
if (py<=0) {
dy *= -1;
}
if (py>=55 && py<63 && px>=linx && px<=linx+16) {
dy *= -1;
dx = random(8)-random(8);
puntos++;
}
px+=dx;
py+=dy;
oled.on();
delay(16);
}