Update oled 0.96" i2c screen by timer

hi every one i'm working on a game
in this game balls go hit the walls and come back to racket if you hit the ball by racket you have earned score else ball goes out of screen and you will lose
my problem is i want to update screen by timer interrupt
i have used an led to see if timer working fine
when i'm not puting updatescreen function in timer isr and use it in main loop timer works fine and screen is updating
but when use it in timer interrupt non of led or updating screen are not working and screen goes blank
please someone help me with this
this is the code
It's not perfect, but it should work well so far

#include <Adafruit_SSD1306.h>
#include <TimerOne.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32

#define OLED_RESET     4
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define upKey 2
#define downKey 3

int ballRad = 3;
int Xball = 50;
int Yball = 20;
int old_Xball = -1;
int old_Yball = -1;

int racketLength = 10;
int racketPos = 0;
int old_racketLength = -1;
int old_racketPos = -1;

void setup() {
  Timer1.initialize(16000);
  Timer1.attachInterrupt(blinkLED);
  if (!oled.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  oled.clearDisplay();
  oled.display();
  pinMode(upKey, INPUT_PULLUP);
  pinMode(downKey, INPUT_PULLUP);
  pinMode(13, OUTPUT);
}
void blinkLED(void) {
  digitalWrite(13, !digitalRead(13));
  updateScreen();

}

void updateScreen(void)
{
  if ((racketPos != old_racketPos) || (racketLength != old_racketLength)) {
    for (int x = 0; x < 2; x++) {
      oled.drawPixel(x, racketPos - 1, SSD1306_BLACK);
      oled.drawFastVLine(x, racketPos, racketLength, SSD1306_WHITE);
      oled.drawPixel(x, (racketPos + racketLength + 1), SSD1306_BLACK);
      old_racketPos = racketPos;
      old_racketLength = racketLength;
    }
  }
  if ((Xball != old_Xball) || (Yball != old_Yball)) {
    oled.fillCircle(old_Xball, old_Yball, ballRad, SSD1306_BLACK);
    oled.fillCircle(Xball, Yball, ballRad, SSD1306_WHITE);
    old_Xball = Xball;
    old_Yball = Yball;
  }
  oled.display();
}

void loop() {
  //updateScreen();
  if ((digitalRead(upKey) == 0) && (racketPos > 0)) racketPos--;
  if ((digitalRead(downKey) == 0) && ((racketPos + racketLength) < SCREEN_HEIGHT)) racketPos++;
  delay(30);


}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.