(Unsolved) Using a Switch to run 2 seprate programs

I am trying to use a slide switch to to run 2 different programs.

This is the code for program 1:

#include <Gamer.h>

Gamer gamer;

int isPlaying;

// Tweaking variables
int snakeGrowthFactor = 1;
boolean passThroughWalls = true;
int snakeSpeed = 180;

/* These two arrays store the snake's X/Y coordinates!
  Our snake has a maximum length of 64. If it were any
  bigger, it wouldn't fit in our screen! */
int snakeX[64];
int snakeY[64];
int snakeDirection;

// Stores the snake's length. This is also the score!
int snakeLength;

int appleX;
int appleY;

// Splash screen image
byte splashScreen[8] = {
  B00000000,
  B01111110,
  B01000000,
  B01111110,
  B00000010,
  B00000010,
  B01011110,
  B00000000
};

void setup() {
  gamer.begin();
  randomSeed(gamer.ldrValue());
}

void loop() {
  if (isPlaying) {
    gamer.clear();
    updateApple();
    drawApple();
    updateDirection();
    moveSnake();
    detectCollision();
    drawSnake();
    gamer.updateDisplay();
    delay(snakeSpeed);
  }
  else {
    showSplashScreen();
  }
}

/* ---------------------------------------------------------------
  Displays the game's splash screen. Press start and the game will
  start.
*/
void showSplashScreen() {
  if (gamer.isPressed(START)) {
    // begin!
    snakeDirection = DOWN;
    snakeLength = 1;
    snakeX[0] = 0;
    snakeY[0] = 0;
    generateApple();
    isPlaying = true;
  }
  else {
    gamer.printImage(splashScreen);
  }
}

/* ---------------------------------------------------------------
  It's game over baby! Shows the score, and takes us back
  to the splash screen.
*/
void gameOver() {
  gamer.printString("Game over");
  delay(100);
  gamer.printString("Score");
  delay(500);
  gamer.showScore(snakeLength);
  delay(500);
  isPlaying = false;
}

And this is Program 2:

// Include Gamer library.
#include <Gamer.h>

// Create a copy of the Gamer library.
Gamer gamer;

// General
int score;
boolean isPlaying;

// Wall - related
int wallThickness = 2;
int gapSize = 3;
int currentWallPosition;
int gapPosition;

// Bird - related
int flyingSpeed = 150;
int birdX = 2;
int birdY;
int gravity = 1;

// Sound - related
int gameStartSongLength = 3;
int gameStartNotes[] = {160, 140, 120};
int gameOverSongLength = 4;
int gameOverNotes[] = {120, 140, 160, 190};
int wallNote = 200;

// Splash screen image
byte splashScreen[8] = {B11101110,
                        B10001001,
                        B10001001,
                        B11101111,
                        B10001001,
                        B10001001,
                        B10001110,
                        B00000000};


void setup() {
  gamer.begin();
  randomSeed(gamer.ldrValue());
}

void loop() {
  if(isPlaying) {
    gamer.clear();
    moveWall();
    drawWall();
    updateBird();
    detectCollision();
    recordScore();
    drawBird();
    gamer.updateDisplay();
    delay(flyingSpeed);
  }
  else {
    showSplashScreen();
  }
}

/* ---------------------------------------------------------------
 Updates the bird's position. If you press UP, it will move up.
 Otherwise, gravity will bring it down :)
 */
void updateBird() {
  /* 
   If the UP button is pressed, move the bird up. Otherwise, 
   move it down with gravity. Remember, the X axis' 0 is on the top
   of the screen. Therefore, when we move the bird up, we REDUCE birdY. 
   When gravity brings it down, away from the axis' origin, 
   we INCREASE birdY.
   */
  if(gamer.isPressed(UP)) {
    birdY--;
  }
  else {
    birdY = birdY + gravity;
  }

  // Detect if the bird is on the floor.
  if(birdY == 7) gameOver();
}

/* ---------------------------------------------------------------
 Draws the bird. Wherever it might be!
 */
void drawBird() {
  // Make sure the bird isn't off the screen.
  birdY = constrain(birdY, 0, 7);
  // Display the bird dot.
  gamer.display[birdX][birdY] = 1;
}

/* ---------------------------------------------------------------
 Moves the walls from right to left on the screen, with a constant
 speed. 
 */
void moveWall() {
  // If the wall is at the end of the screen, get a new wall going!
  if(currentWallPosition == 0 - wallThickness) {
    generateWall();
  }
  // Otherwise, move the wall. 
  else {
    currentWallPosition--;
  }
}

/* ---------------------------------------------------------------
 Places a new wall on the edge of the screen, ready to be moved.
 */
void generateWall() {
  // Set the wall to the right of the screen.
  currentWallPosition = 8;
  // Get a random gap in the wall.
  gapPosition = random(1, 7-gapSize);
}

/* ---------------------------------------------------------------
 Draws the walls at their current position
 */
void drawWall() {
  // Draw multiple walls, if we need to.
  for(int j=0; j<wallThickness; j++) {
    if(currentWallPosition+j >= 0 && currentWallPosition+j <= 7) {
      for(int i=0; i<8; i++) {
        // Draw the wall, but miss out the gap. 
        if(i > gapPosition + gapSize - 1 || i < gapPosition) {
          gamer.display[currentWallPosition+j][i] = 1;
        }
      }
    }
  }
}

/* ---------------------------------------------------------------
 Checks if the bird is on a wall. If there's a pixel on the 
 same position as the bird, it's game over!
 */
void detectCollision() {
  if(gamer.display[birdX][birdY] == 1) {
    gameOver();
  }
}

/* ---------------------------------------------------------------
 Keeps track of the score. If the bird is flying through a wall, 
 add one to the score, and make a sound!
 */
void recordScore() {
  if(birdX == currentWallPosition + wallThickness) {
    score++;
    gamer.playTone(wallNote);
  }
  else gamer.stopTone();
}

/* ---------------------------------------------------------------
 Shows an image when the game isn't being played.
 */
void showSplashScreen() {
  if(gamer.isPressed(START)) {
    
    // Play a tune before the game starts.
    for(int i=0; i<gameStartSongLength; i++) {
      gamer.playTone(gameStartNotes[i]);
      delay(100);
    }
    
    // Stop the sound!
    gamer.stopTone();
    
    isPlaying = true;
    generateWall();
    birdY = 2;
    score = 0;
  }
  else {
    gamer.printImage(splashScreen);
  }
}

/* ---------------------------------------------------------------
 Shows Game over, followed by the score. 
 */
void gameOver() {
  
  // Play the Game Over tune.
  for(int i=0; i<gameOverSongLength; i++) {
    gamer.playTone(gameOverNotes[i]);
    delay(100);
  }
  
  // Turn off sound.
  gamer.stopTone();
  
  // Display Game Over followed by the score.
  gamer.printString("Game over");
  delay(100);
  gamer.printString("Score");
  delay(500);
  gamer.showScore(score);
  delay(500);
  isPlaying = false;
}

How would I make this work?
Any help is appreciated :slight_smile:

What have you tried exactly? And what kind of slide switch? SPST? SPDT?

You need to combine them into a single sketch, probably as two functions, calling one or the other based on the state of the switch.
Memory management may be an issue.

INTP:
What have you tried exactly? And what kind of slide switch? SPST? SPDT?

I am using a SPDT

AWOL:
You need to combine them into a single sketch, probably as two functions, calling one or the other based on the state of the switch.
Memory management may be an issue.

what sort of code would I need create the functions?

The code that you already have, sort of code.

I imagine the sort of code you need is the simple sort for the switch and the function sort for functions.

You only need the switch to work as SPST really, GND--switch as on/off--board pin set as input pullup.
If pin high, do prog 1, if pin low, do prog 2.

AWOL:
The code that you already have, sort of code.

yes i know that code but what about the functions you said to use, sorry for asking but i am a noob at arduino

What arduino are you using and do you have the individual games working and what is your display device and what libraries are you using?

INTP:
I imagine the sort of code you need is the simple sort for the switch and the function sort for functions.

You only need the switch to work as SPST really, GND--switch as on/off--board pin set as input pullup.
If pin high, do prog 1, if pin low, do prog 2.

Yes, that makes sense but what would the code look like for that?

It would look, I imagine, a lot like example code in the IDE for reading a simple switch

INTP:
What arduino are you using and do you have the individual games working and what is your display device and what libraries are you using?

I am using the Arduino Uno
the games do work and i have tested them
the display device is a 8x8 LED matrix
I am using the Gamer library but i can use others if needed

If you can't translate "If pin high, do prog 1, if pin low, do prog 2" to code, I think you need to actually learn the basics of code through some of the many tutorials available.
And do it quick, before we all get the impression you want to put in zero effort.

I understand it now thanks for the help :slight_smile:

Qcraft:
the display device is a 8x8 LED matrix

In which case, you may wish to revisit your snake game's storage requirements - you're wasting memory.

If you only need to select the game after a power-up or reset its fairly simple - rename the setup()
functions and loop() functions for each game, and have setup and loop like this:

boolean game1 = false ;
void setup ()
{
  game1 = digitalRead (switchpin) ;
  if (game1)
    game1_setup () ;
  else
    game2_setup () ;
}

void loop ()
{
  if (game1)
    game1_loop () ;
  else
    game2_loop () ;
}

However there may be stuff setup globally at top level (not inside a function) by either/both games,
which may be more tricky.

If you want to be able switch games without reseting then you’ll have to ensure all setup is done in
a setup function, not at top level, so you can switch games and completely set things up correctly.