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.
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?
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.
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.
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.