Screen keeps randomly freezing

Im trying to make pong/ breakout on an arduino uno, more specificly the inventor.io hero board, from scratch. I've *mostly got the base mechanics working, but the oled display keeps freezing up randomly. and the ball that i've made doesn't want to bounce on the left side of the screen.

here's the code:



#include <U8glib.h>

//rotary encoder
#define CLK2 2
#define DT2 3

int counter = 20;  //keep track of steps
int CLKstate;
int lastCLKstate;
//display
U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);  //set this to whatever your using

int ballX = 64;
int ballY = 32;
int ballDir = 0;
int ballSpeed = 3;

void draw(void) {

  u8g.drawBox(8, counter, 5, 25);   //player
  u8g.drawBox(ballX, ballY, 5, 5);  //the ball
}

void setup(void) {  //stuff for the rotary encoder
  pinMode(CLK2, INPUT);
  pinMode(DT2, INPUT);

  lastCLKstate = digitalRead(CLK2);

  attachInterrupt(digitalPinToInterrupt(CLK2), updateEncoder, CHANGE);
  attachInterrupt(digitalPinToInterrupt(DT2), updateEncoder, CHANGE);
}

void moveBall() {
  if (ballDir == 0) {  // if moving to the right
    if (ballX >= 123) {
      ballDir = 4;
    } else {
      ballX += ballSpeed;
    }

  } else if (ballDir == 4) {  // if moving to the left
    if (ballDir <= 0) {
      ballDir == 0;
    } else {
      ballX -= ballSpeed;
    }
  }
}

void updateEncoder() {
  //read the state of clk
  CLKstate = digitalRead(CLK2);

  if (CLKstate != lastCLKstate && CLKstate == 1) {

    if (digitalRead(DT2) != CLKstate) {
      counter++;  //clockwise
    } else {
      counter--;  //counterclockwise
    }
  }
  lastCLKstate = CLKstate;
}

void loop(void) {
  moveBall();
  // picture loop
  u8g.firstPage();
  do {
    draw();
  } while (u8g.nextPage());
}

i've connected a oled display to ground and 5v and sck to A5 and sda to A4. I also have a rotary encoder with power and gnd at power and ground, and CLK and DT at pins 2 and 3 respectivly. nothing else, Pls help!

I have not seen the "freeze" yet.

I noticed that the "ball" goes through the paddle, and when the ball hits the wall, the ball wraps around to the other side. You should add the width of the ball to your "if it hits the wall" routine so that the edge of the ball is detected.

should ballX in the if and remove one of the extra = in line below, then ball bounces back and forth..

simmed here..

have fun.. ~q

Can you describe this? Paddle movement? Ball location? Anything you notice?

the paddle stops moving even when you try to, and same with the ball

play the sim i posted..
i did a few more changes.. :slight_smile:

have fun.. ~q

1 Like

Thanks for your bug fix and the simmed pong game! i tryed it out on the hardware and it works well, but there are some bugs, like the paddle not obeying you, and the paddle just teleporting. but overall, great game! can i borrow code from it if i give you creedit?

You're welcome..
and it's actually your code, i just fixed it up a bit and yes, still needs a bit of work..
be it harm none, do as ye wish..
~q

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