//Panos Papazoglou, MICRODEV.GR //with interrupts #include #include #include #include #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_DC 8 #define OLED_CS 10 #define OLED_RESET 9 Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &SPI, OLED_DC, OLED_RESET, OLED_CS); int HIGHSCORE=0; int score=0; int cx=63; int cy=5; int dx=1, dy=1; int xmax,ymax; int rad=3; //raketa int rakW=25, rakH=2; int rakX=63; int rakY=63-rakH; void setup() { Serial.begin(9600); if(!oled.begin(SSD1306_SWITCHCAPVCC)) { Serial.println(F("OLED Error")); for(;;); } ymax=SCREEN_HEIGHT; xmax=SCREEN_WIDTH; pinMode(2,INPUT_PULLUP);pinMode(3,INPUT_PULLUP); attachInterrupt(1,left,LOW); attachInterrupt(0,right,LOW); pinMode(5,OUTPUT); digitalWrite(5,HIGH); start(); } void loop() { oled.clearDisplay(); oled.display(); cx+=dx; cy+=dy; if ((cx>xmax-rad) || (cx=rakX) && (cx<=rakX+rakW) && (cy+rad>rakY)) { dy=-dy; score++; if (score>HIGHSCORE) HIGHSCORE=score; } else if (cy+rad>ymax) { cx=63; cy=5; mytext("GAME",3,30,5); mytext("OVER",3,30,28); delay(2000); start(); } oled.fillCircle(cx,cy,rad,WHITE); draw_raketa(); draw_score(); oled.display(); int mydelay=20; int long mt=millis(); while((millis()-mt)SCREEN_WIDTH-rakW-1) rakX=SCREEN_WIDTH-rakW-1; oled.fillRect(rakX,rakY,rakW,rakH,WHITE); } void left() { static unsigned long lastIT = 0; unsigned long IT = millis(); if (IT - lastIT > 70) { rakX-=20; } lastIT = IT; } void right() { static unsigned long lastIT = 0; unsigned long IT = millis(); if (IT - lastIT > 70) { rakX+=20; } lastIT = IT; } void start() { oled.clearDisplay(); oled.display(); displayTITLES(); delay(4000); wait_loop(); score=0; } void displayTITLES() { mytext("MICRODEV.GR",1,30,0); mytext("High Score",2,5,15); mytext(String(HIGHSCORE),2,55,34); mytext("Loading...",1,35,55); } void wait_loop() { for(int i=5;i>=0;i--) { oled.clearDisplay(); oled.display(); mytext(String(i),4,50,15); delay(1000); } } void draw_score() { mytext(String(score),2,100,10); } void mytext(String text, int textsize, int x, int y) { oled.setTextSize(textsize); oled.setTextColor(WHITE); oled.setCursor(x,y); oled.print(text); oled.display(); }