#define lBtn 2
#define rBtn 3
#define lLed A2
#define rLed A3
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define spk 4
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int x0, y0, x, y, r;
int xl0, yl0;
int xl, yl, w, h;
int dx, dy, dxl;
int startMidi[6] = {262, 294, 330, 349, 392, 0}; //C4, D4, E4, F4, G4(Do, Re, Mi, Fa, So)
int endMidi[6] = {392, 349, 330, 294, 262, 0}; //G4, F4, E4, D4, C4
void game_init() {
display.clearDisplay();
x=SCREEN_WIDTH/2; y=SCREEN_HEIGHT/2; r=3;
dx = 2; dy = 1;
display.fillCircle(x, y, r, WHITE);
x0 = x; y0 = y;
w = 24;
h = 3;
xl = SCREEN_WIDTH/2 - w/2;
yl = SCREEN_HEIGHT - h - 5;
display.fillRect(xl, yl, w, h, WHITE);
xl0 = xl; yl0 = yl; dxl = 1;
display.display();
midi_play(startMidi);
}
void midi_play(int *midi) {
for(int i=0; midi[i]!=0; i++) {
tone(spk, midi[i], 100);
delay(100);
}
}
void setup() {
// put your setup code here, to run once:
pinMode(lBtn, INPUT_PULLUP);
pinMode(rBtn, INPUT_PULLUP);
pinMode(lLed, OUTPUT);
pinMode(rLed, OUTPUT);
digitalWrite(lLed, LOW);
digitalWrite(rLed, LOW);
pinMode(spk, OUTPUT);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while(true);
}
game_init();
}
void loop() {
// put your main code here, to run repeatedly:
delay(30);
if(digitalRead(lBtn)==LOW) {
digitalWrite(lLed, HIGH);
xl -= dxl;
if(xl<0) xl = 0;
display.fillRect(xl0, yl0, w, h, BLACK);
display.fillRect(xl, yl, w, h, WHITE);
xl0 = xl; yl0 = yl;
}
else
digitalWrite(lLed, LOW);
if(digitalRead(rBtn)==LOW){
digitalWrite(rLed, HIGH);
xl += dxl;
if(xl>SCREEN_WIDTH-w) xl = SCREEN_WIDTH - w;
display.fillRect(xl0, yl0, w, h, BLACK);
display.fillRect(xl, yl, w, h, WHITE);
xl0 = xl; yl0 = yl;
}
else
digitalWrite(rLed, LOW);
display.display();
}