Having trouble trying to execute TVout game code based on a button press

I'm making a cool little CRT clock using an old camcorder viewfinder. I have it displaying my custom "gui" using the TVout library.

I have an extra "mode" button on my keypad that I want to use to bring up a game or something. I found some sketches online and tweaked them to run on my clock. Game of Life, Breakout. No problem.

![|500x374](https://farm1.staticflickr.com/746/20849970916_1f64b38e88_z.jpg" width="640" height="479")

![|500x375](https://farm1.staticflickr.com/748/20253662174_049aae39e2_k.jpg" width="640" height="481")

There is, however, a problem when I try to combine both sketches. I can display my clock code, or play Breakout, but if I try and merge the code, I get absolutely nothing.

I thought it would be easy to define a bool, and toggle its state with a button press. Based on the state, I display either the game, or the clock.

Something like this:

int modeButton = 8;
boolean startGame = false;

void setup() {
  pinMode(modeButton, INPUT);
}

void loop() {
  if (startGame == false) {
    // execute clock code
  }
  if (startGame == true) {
    //execute game code
  }

  //toggle what to display
  if (digitalRead(modeButton == LOW)) {
    startGame = !startGame;
  }
}

Anyone know why this methodology just doesn't work? The screen doesn't even turn on when I put in any kind of code that's executed based on a bool value or button count, etc.

This shouldn't be hard. If you had a few small games, even just two, couldn't you put up a menu where you could pick which game to play?

 if (digitalRead(modeButton == LOW)) {
    startGame = !startGame;
  }
  if (digitalRead(modeButton) == LOW) {
    startGame = !startGame;
  }

for starters.

Good catch, but that's just the simple example I typed up on my lunch break to illustrate the methodology. It's right in my actual code at home.

Still, my clock code runs fine. Breakout runs fine. But when I put them both together and try and run one or the other based on that boolean value, nothing is ever called, and the screen doesn't even turn on. It's like there's no signal at all.

Maybe there's a similar boo-boo in the real code.

that's just the simple example I typed up on my lunch break to illustrate the methodology.

Please don't do that again.

It's tough to say without seeing your game code. Or the rest of it.
I assume that each pass through your main loop it runs through the game code and continues on.
So the game code itself has no delay()s and if (startGame == true) {} can be properly exited.
But I don't know.

You might want to try out something like.

attachInterrupt(0, changeGame, FALLING);

switch (gameMode) {
case 0

//Game 1 Code

break;
case 1

//Game 2 Code

break;
}


void changeGame(){
gameMode= !gameMode;
}

So your button can update the variable regardless of code progress. And such that you could include conditions to break out of the case structure early on a button press.

ApexM0Eng:
It's tough to say without seeing your game code. Or the rest of it.
I assume that each pass through your main loop it runs through the game code and continues on.
So the game code itself has no delay()s and if (startGame == true) {} can be properly exited.
But I don't know.

You might want to try out something like.

attachInterrupt(0, changeGame, FALLING);

switch (gameMode) {
case 0

//Game 1 Code

break;
case 1

//Game 2 Code

break;
}

void changeGame(){
gameMode= !gameMode;
}




So your button can update the variable regardless of code progress. And such that you could include conditions to break out of the case structure early on a button press.

Thanks. I'll have to look into that, but I think interrupts can't be used with the TVout library.

In any case, this seems to work as expected....

void loop () {

  if (startGame == false) {

    TV.select_font(font8x8);
    TV.println("false");
  }
  if (startGame == true) {

    TV.select_font(font8x8);
    TV.println("true");
  }

  //mode button pressed
  if (digitalRead(modeButton) == LOW) {

    //debounce
    delay(100);

    if (startGame == false) {
      startGame = true;
    } else {
      startGame = false;
    }
  }
} //end loop

And I'm not sure why this works, but dumping in a game instead of text makes a difference.

As I suspected. I can't use interrupts. Even if I could, it would require a hardware change to my PCB.

But you might be onto something. This works:

void loop () {
  if (digitalRead(modeButton) == LOW) {
    TV.delay_frame(20);
    changeGame();
  }

  switch (startGame) {
    case 0:
      TV.select_font(font8x8);
      TV.println("FALSE");
      break;

    case 1:
      TV.select_font(font8x8);
      TV.println("TRUE");
      break;
  }
} //end loop

void changeGame() {
  startGame = !startGame;
}