Exemplo n.º 1
0
void setup() {
// start the spi bus
#ifdef hal_spi
  SPI.begin();
#endif
#ifdef hal_tft
  lcd.initR(INITR_BLACKTAB);   // initialize a ST7735S chip, black tab
  lcd.fillScreen(ST7735_BLACK);
#endif
  // initialize serial port
  Serial.begin(9600);
  // leonardo - wait for connection
  while(!Serial) { }
#ifdef TwoWire_h
  // start the i2C bus
  Wire.begin();
#endif
#ifdef hal_raster
  Raster.start();
#endif
#ifdef hal_compass
  compass.start();
#endif
  // start the droid filesystem (now we have serial)
  droid.fs.start();
  // center the joystick
#ifdef hal_tft
  droid.joystick[0].center = 1024 - analogRead(A1);
  droid.joystick[1].center = analogRead(A2);
#endif
  // droid.joystick_center[0] = 1024 - analogRead(A1);
  // droid.joystick_center[1] = analogRead(A2);
}
Exemplo n.º 2
0
void init()
{
	spiffs_mount(); // Mount file system, in order to work with files

	Serial.begin(SERIAL_BAUD_RATE); // 115200 by default
	Serial.systemDebugOutput(true); // Allow debug output to serial

	//WifiStation.config(WIFI_SSID, WIFI_PWD);
	WifiStation.enable(false);
//	WifiAccessPoint.enable(false);

	debugf("Display start");
	startTime = millis();

	// Use this initializer if you're using a 1.8" TFT
	// tft.initR(INITR_BLACKTAB);   // initialize a ST7735S chip, black tab

	// Use this initializer (uncomment) if you're using a 1.44" TFT
	tft.initR(INITR_144GREENTAB);   // initialize a ST7735S chip, black tab

	tft.fillScreen(ST7735_BLACK);
	startTime = millis() - startTime;

	debugf("Initialized in %d ms\n", startTime);

	DemoScreenTimer.initializeMs(500, screen1).start(FALSE);

}
Exemplo n.º 3
0
void setup() {
    Serial.begin(9600);
    Serial1.begin(9600);
    tft.initR(INITR_REDTAB);
    randomSeed(analogRead(4));
    joystickXCentre = analogRead(JOYSTICK_MOVE_X_PIN) - 512;
    joystickYCentre = analogRead(JOYSTICK_MOVE_Y_PIN) - 512;
    pinMode(JOYSTICK_BUTTON_PIN, INPUT_PULLUP);
    pinMode(COUNTDOWN_START_RED_1, OUTPUT);
    pinMode(COUNTDOWN_START_RED_2, OUTPUT);
    pinMode(COUNTDOWN_MID_RED_1, OUTPUT);
    pinMode(COUNTDOWN_MID_RED_2, OUTPUT);
    pinMode(COUNTDOWN_GREEN, OUTPUT);
    tft.setRotation(1); //because our screen is nonstandard rotation
    player1.score = 0;
    player2.score = 0;
    if (!SD.begin(SD_CS)) {
        Serial.println("SD Init Failed");
        return;
    }
    if (!card.init(SPI_HALF_SPEED, SD_CS)) {
        Serial.println("Raw SD Init failed");
        while (1);
    }
    tft.fillScreen(ST7735_BLACK);
}
Exemplo n.º 4
0
void setup(void) {
  Serial.begin(9600);
  pinMode(JOYSTICK_BUTTON, INPUT);
  pinMode(RATING_LED_0, OUTPUT);
  pinMode(RATING_LED_1, OUTPUT);
  pinMode(RATING_LED_2, OUTPUT);
  pinMode(RATING_LED_3, OUTPUT);
  pinMode(RATING_LED_4, OUTPUT);

  digitalWrite(JOYSTICK_BUTTON, HIGH);

  tft.initR(INITR_REDTAB);

  Serial.print("Initializing SD card...");
  if (!SD.begin(SD_CS)) {
    Serial.println("failed!");
    return;
  }
  Serial.println("OK!");

  drawMap();

  // test out reading blocks from the SD card
  if (!card.init(SPI_HALF_SPEED, SD_CS)) {
      Serial.println("Raw SD Initialization has failed");
      while (1) {};  // Just wait, stuff exploded.
  }

  verticalMidpoint = getVertical();
}
Exemplo n.º 5
0
void setup(void) {
    Serial.begin(9600);

    // If your TFT's plastic wrap has a Red Tab, use the following:
    tft.initR(INITR_REDTAB);   // initialize a ST7735R chip, red tab
    // If your TFT's plastic wrap has a Green Tab, use the following:
    //tft.initR(INITR_GREENTAB); // initialize a ST7735R chip, green tab

    // how much memory have we got left at this point?
    Serial.print("Avail mem (bytes):");
    Serial.println(AVAIL_MEM);

    Serial.print("Initializing SD card...");
    if (!SD.begin(SD_CS)) {
      Serial.println("failed!");
      return;
    }
    Serial.println("OK!");

    // clear to yellow
    tft.fillScreen(tft.Color565(0xff, 0xff, 0x00));

    lcd_image_draw(&map_image, &tft, 0, 0, 0, 0, 128, 128);

    // how much memory have we got left at this point?
    Serial.print("Avail mem (bytes):");
    Serial.println(AVAIL_MEM);

    // test out reading blocks from the SD card
    if (!card.init(SPI_HALF_SPEED, SD_CS)) {
        Serial.println("Raw SD Initialization has failed");
        while (1) {};  // Just wait, stuff exploded.
        }

    // how much memory have we got left at this point?
    Serial.print("Avail mem (bytes):");
    Serial.println(AVAIL_MEM);
 
    uint32_t block_num = 4000000;
    uint32_t start = millis();
    for (int l=0; l<135; l++) {
        card.readBlock( block_num, (uint8_t *) block_buf);
        // Serial.println(block_buf[1].name);
        }
    uint32_t stop = millis();
    //Serial.println(stop - start);
    
    //dump_block((uint8_t *) block_buf, BLOCK_LEN);
    // Serial.println(block_buf[1].name);

    for(int i = 0; i < 1066; i++) {
      printRest(i);
    }
}
Exemplo n.º 6
0
void initialize_screen() {

    tft.initR(INITR_REDTAB);

    tft.setRotation(0);

    tft.setCursor(0, 0);
    tft.setTextColor(0x0000);
    tft.setTextSize(1);
    tft.fillScreen(BLUE);    
}
Exemplo n.º 7
0
void setup()  
{
  Serial.begin(9600);

  menuState = TESTKEYPAD_MENU;

  tft.initR(INITR_REDTAB);

  tft.setTextSize(1);
  tft.fillScreen(ST7735_BLACK);
  tft.setCursor(0, 0);
  tft.setTextColor(ST7735_WHITE,ST7735_BLACK);
  sensNum = 0;
}
Exemplo n.º 8
0
/** ========================= Main function ==========================*/
int main (){
    init();
    Serial.begin(9600);
   
    randomSeed(analogRead(3)); // randomSeed() initializes the pseudo-random number generator
   
    pinMode(SEL, INPUT);
    digitalWrite(SEL, HIGH);
    tft.initR(INITR_BLACKTAB); // initialize a ST7735R chip
   
    initializeGrid();
    displayGrid();
    gamePlay();
  
    return 0;
}
void setup() {
    Serial.begin(9600);
    tft.initR(INITR_BLACKTAB);

    /*
      Setup SD Card
    */
    Serial.print("init'ing SD card..");
    if (!SD.begin(SD_CS)) {
        Serial.println("failed");
        return;
    }
    Serial.println("OK!");

    /*
      Hello World
    */
    // Create a layer
    abLayer layer5 = abLayer("bgyellow.lcd");
    
    // Add some sprites
    abSprite* h = layer5.addSprite("h.lcd", 0, 0);
    abSprite* e = layer5.addSprite("e.lcd", 15, 0);
    abSprite* l_1 = layer5.addSprite("l.lcd", 30, 0);
    abSprite* l_2 = layer5.addSprite("l.lcd", 45, 0);
    abSprite* o = layer5.addSprite("o.lcd", 60, 0);

    layer5.draw(&tft);
    while (true) {
      h->moveDown(12);
      e->moveDown(9);
      l_1->moveDown(6);
      l_2->moveDown(3);
      o->moveDown(0);
      layer5.draw(&tft);

      h->moveUp(12);
      e->moveUp(9);
      l_1->moveUp(6);
      l_2->moveUp(3);
      o->moveUp(0);
      layer5.draw(&tft);
    }

}
Exemplo n.º 10
0
void setup() {
	Serial.begin(9600);

	// initialise display
	tft.initR(INITR_BLACKTAB);
	tft.fillScreen(ST7735_BLACK);

	// initialise pins
	pinMode(leftPin, INPUT);
	pinMode(rightPin, INPUT);
	pinMode(turnPin, INPUT);
	
	// display splashscreen
	tft.setTextColor(ST7735_WHITE);
	tft.setTextWrap(true);
	
	tft.setCursor(34, 20);
	tft.println("WELCOME TO");
	
	tft.setCursor(45, 30);
	tft.println("~TETRIS");
	
	tft.setCursor(16, 50);
	tft.println("PRESS THE MIDDLE");
	
	tft.setCursor(18, 60);
	tft.println("BUTTON TO START");

	tft.setCursor(57, 110);
	tft.println("BY:");

	tft.setCursor(33, 120);
	tft.println("NICHOLAS LI");

	tft.setCursor(56, 130);
	tft.println("AND");

	tft.setCursor(24, 140);
	tft.println("ARJUN KALBURGI");

	// only continue if turnPin is pressed
	while(digitalRead(turnPin) == LOW) {}

	tft.fillScreen(ST7735_BLACK);
}
Exemplo n.º 11
0
int main() {
  // Initialize arduino, serial communication, lcd screen, buttons (and
  // internal pull-up resistors), LED pins, and SD card.
  init();
  Serial.begin(9600);
  Serial3.begin(9600);
  while(Serial3.available()) {Serial3.read();} // Clean the Serial line
  tft.initR(INITR_BLACKTAB);

  pinMode(SEL, INPUT);
  digitalWrite(SEL, HIGH);
  pinMode(Button1, INPUT);
  digitalWrite(Button1, HIGH);
  for (int i = 0; i < 10; ++i) {
    pinMode(i + 22, OUTPUT);
  }
  int click;    // This will be used to continue from the Title Screen

  Serial.print("Initializing SD card...");
  if (!SD.begin(SD_CS)) {
    Serial.println("failed!");
  }
  Serial.println("OK!");

  // do the auto calibration of the cursor (found in Cursor.cpp)
  Initialize_Cursor();


  while(true) {
    // draw the title screen, and set the turn pin to neutral (blue)
    lcd_image_draw(&Images[title], &tft, 0, 0, 0, 0, 128, 160 );
    digitalWrite(bluePin, HIGH);
    while (true) { // leave the title page with a joystick button press.
      click = digitalRead(SEL);
      if (!click) {break;}
    }
    // draw a black screen
    tft.fillScreen(0);
    // draw the border image (note that the numbers (0-9) and
    // letters (A-J) are just for appearance and aren't actually used by
    // the program).
    lcd_image_draw(&Images[border], &tft, 0, 0, 0, 0, 128, 128 );

    // initialize My_Ocean so that each value is 0 (water)
    for( int i=0; i<100; i++ ) {
      My_Ocean[i] = 0;
    }
    // initialize Their_Ocean so that each value is 1 (miss). This needs to
    // be nonzero for the first drawing of the screen (occurs on line 47
    // of Place_Ships.cpp) and will be set to 0 after.
    for( int i=0; i<100; i++ ) {
      Their_Ocean[i] = 1;
    }
    //==================== Initialization complete =================

    // Allows a player to place their ships
    My_Ships = Place_Ships(My_Ocean, Their_Ocean);
    // set Their_Ocean so that each value is 0 (water).
    for( int i=0; i<100; i++ ) {
      Their_Ocean[i] = 0;
    }
    // One arduino should read high, and the other should read low
    // (see wiring instructions for more detail). This determines who
    // gets the first turn.
    bool Turn = digitalRead(13);
    // Initialize the number of ships remaining for each player to 5.
    // can be set to 1 for a "debug" mode to see the victory/reset more
    // quickly.
    int Mine_Alive = 5;
    int Their_Alive = 5;

    // Turn on all the LED's initially (because all 10 ships should be
    // alive).
    for (int i = 0; i < 10; i++) {
      digitalWrite(22+i , HIGH);
    }

    // Connect to opponent. One arduino writes 'R' for ready, then waits
    // to receive a response. The other waits to read 'R' first, then
    // writes it.
    Write_Message("Connecting...");
    if( Turn == 1 ) {
      Serial3.write('R');
      while( Serial3.read() !='R' ) {}
    }
    else {
      while( Serial3.read() !='R' ) {}
      Serial3.write('R');
    }

    //==================== Ready to communicate/play ===============

    // Loop until either player has no ships left.
    while((Mine_Alive != 0) && (Their_Alive != 0)) {

      // My turn
      if( Turn == 1 ) {
        // draw a red square in the top left corner (because we will
        // view what we know of Their_Ocean).
        tft.fillRect(0, 0, 8, 8, 0xF800);
        // Make the LED green because it is our turn.
        digitalWrite(redPin, LOW);
        digitalWrite(greenPin, HIGH);
        digitalWrite(bluePin, LOW);
        // Draw Their_Ocean
        Draw_Screen(Their_Ocean, My_Ocean);
        // Indicate our turn at the bottom of the screen
        Write_Message("Your Turn");
        // Call Fire. It will return one (decrementing Their_Alive)
        // if it sinks a ship.
        Their_Alive = Their_Alive - Fire(Their_Ocean);
        // change turns
        Turn = 0;
      }
      else if( Turn == 0 ) {
        // draw a green square in the top left corner (because we will
        // view our own ships (My_Ocean).
        tft.fillRect(0, 0, 8, 8, 0x07E0);
        // Make the LED red because it is their turn.
        digitalWrite(redPin, HIGH);
        digitalWrite(greenPin, LOW);
        digitalWrite(bluePin, LOW);
        // draw My_Ocean
        Draw_Screen(My_Ocean, Their_Ocean);
        // Indicate their turn at the bottom of the screen
        Write_Message("Opponent's turn");
        // Call Get_Shot_At. It will return one (and will decrement
        // Mine_Alive) if a ship is sunk.
        Mine_Alive = Mine_Alive-Get_Shot_At(My_Ocean, My_Ships);
        // change turns
        Turn = 1;
      }
      // update the LEDs based on the number of ships left alive. The
      // Green LEDS represent the number of ships you have alive while
      // the Red ones do the same for the other players ships.
      for (int i = 5; i > 0; i--) {
        if (Their_Alive >= i) {
          digitalWrite(21 + 2*i , HIGH);
        }
        else {
          digitalWrite(21 + 2*i ,  LOW);
        }
      }
      for (int i = 5; i > 0; i--) {
        if (Mine_Alive >= i) {
          digitalWrite(20 + 2*i , HIGH);
        }
        else {
          digitalWrite(20 + 2*i , LOW);
        }
      }
    }

    // The delays help everything to sync up with the sound effects.
    delay(2000);
    if( Mine_Alive > 0 ) {
      // The number sent to the 'Victory_Cascade()' is to indicate which
      // set of LEDS are to be cascaded based on which player is the
      // winner of the game. The spaces in the message are in place to
      // avoid the word in the messages being cut off
      Write_Message("You won! Please hold joystick button to   play again.");
      Sound_Victory();
      Victory_Cascade(22);
    }
    else {
    delay(3000);
      Write_Message("You lost! Please holdjoystick button to  play again.");
      Victory_Cascade(23);
    }
  }
  Serial3.end();
  Serial.end();
  return 0;           // End of function
}
Exemplo n.º 12
0
void setup(void) {
	Serial.begin(9600);
  
	// set buttonPin to INPUT and 
	// turn on internal pull up resistor 
	pinMode(BUTTJOY, INPUT);
	digitalWrite(BUTTJOY, HIGH);
  
	// establish interrupts on joystick button falling (when the button is initially pushed)
	// NOTE: BUTTJOY has been set up with a debounced circut
	attachInterrupt(BUTTJOY, buttonPress, FALLING);
  
	// Define the cursor radius to be 2 pixles
	cursor.r = CURSOR_RADIUS;
  
	// Read initial joystick position
	iniJoy.x = analogRead(HORZJOY);
	iniJoy.y = analogRead(VERTJOY);
  
	// If your TFT's plastic wrap has a Red Tab, use the following:
	tft.initR(INITR_REDTAB);   // initialize a ST7735R chip, red tab
	// If your TFT's plastic wrap has a Green Tab, use the following:
	//tft.initR(INITR_GREENTAB); // initialize a ST7735R chip, green tab

	#if DEBUG
	Serial.print("Initializing SD card...");
	#endif
	if (!SD.begin(SD_CS))
	{
		Serial.println("failed!");
		return;
	}
	#if DEBUG
	Serial.println("OK!");
	#endif
  
	// test out reading blocks from the SD card
	if (!card.init(SPI_HALF_SPEED, SD_CS)) {
		#if DEBUG
		Serial.println("Raw SD Initialization has failed");
		#endif
		while (1) {};  // Just wait, stuff exploded.
	}
	
	// Centers map on pre-selected coord
	m_map.x = m_map.x - tft.width()/2;
	m_map.y = m_map.y - tft.height()/2;
	
	// Places cursor in the center of the screen
	cursor.position.x = (int) tft.width()/2.;
	cursor.position.y = (int) tft.height()/2.;

	// clear to blue
	tft.fillScreen(tft.Color565(137, 207, 240));
	
	// Added for debug purposes
	#if DEBUG
	Serial.print("TFT Height: ");
	Serial.print( tft.height() );
	Serial.print(", TFT Width: ");
	Serial.println( tft.width() );
	#endif

	// Draw initial screen
	lcd_image_draw(&map_image, &tft, &m_map, &c_zero,  tft.width(), tft.height());
	// Draw Cursor on map
	drawCursor(&tft, &cursor);
}
Exemplo n.º 13
0
void setup(void) {
    Serial.begin(BAUD);
    Serial.print("Hello! professorbunsen, I presume? ");

    // Use this initializer if you're using a 1.8" TFT
    //tft.initR(INITR_BLACKTAB);   // initialize a ST7735S chip, black tab

    // Use this initializer (uncomment) if you're using a 1.44" TFT
    tft.initR(INITR_144GREENTAB);   // initialize a ST7735S chip, black tab

    Serial.println("Initialized");

    uint16_t time = millis();
    tft.fillScreen(ST7735_BLACK);
    time = millis() - time;

    Serial.println(time, DEC);
    delay(500);

    // large block of text
    tft.fillScreen(ST7735_BLACK);
    testdrawtext("Als Gregor Samsa eines Morgens aus unruhigen Träumen erwachte, fand er sich in seinem Bett zu einem ungeheueren Ungeziefer verwandelt. Er lag auf seinem panzerartig harten Rücken und sah, wenn er den Kopf ein wenig hob, seinen gewölbten, braunen, von bogenförmigen Versteifungen geteilten Bauch, auf dessen Höhe sich die Bettdecke, zum gänzlichen Niedergleiten bereit, kaum noch erhalten konnte. Seine vielen, im Vergleich zu seinem sonstigen Umfang kläglich dünnen Beine flimmerten ihm hilflos vor den Augen. Was ist mit mir geschehen? dachte er. Es war kein Traum.", ST7735_WHITE);
    delay(1000);

    // tft print function!
    tftPrintTest();
    delay(4000);

    // a single pixel
    tft.drawPixel(tft.width()/2, tft.height()/2, ST7735_GREEN);
    delay(500);

    // line draw test
    testlines(ST7735_YELLOW);
    delay(500);

    // optimized lines
    testfastlines(ST7735_RED, ST7735_BLUE);
    delay(500);

    testdrawrects(ST7735_GREEN);
    delay(500);

    testfillrects(ST7735_YELLOW, ST7735_MAGENTA);
    delay(500);

    tft.fillScreen(ST7735_BLACK);
    testfillcircles(10, ST7735_BLUE);
    testdrawcircles(10, ST7735_WHITE);
    delay(500);

    testroundrects();
    delay(500);

    testtriangles();
    delay(500);

    mediabuttons();
    delay(500);

    Serial.println("done");
    delay(1000);
}
Exemplo n.º 14
0
void init_display(void) {
    tft.initR(INITR_BLACKTAB);
}
Exemplo n.º 15
0
void setup(void) {

  pinMode(BUTTON,INPUT);
  digitalWrite(BUTTON,HIGH);

  tft.initR(INITR_BLACKTAB);
  tft.fillScreen(ST7735_BLACK);

  int16_t OFFSET = 30;
  int16_t hOld = 0;
  int16_t vOld = 0;
  int16_t original_v = analogRead(VERT);
  int16_t original_h = analogRead(HOR);
  int16_t v = 143;
  int16_t h = 64;

  while (1){
    //  v = OFFSET_V(analogRead(VERT), v, original_v);
    h = OFFSET_H(analogRead(HOR), h, original_h);

    if(hOld != h){
      for(int y = -1; y < 2; y++){
	for(int x = -2; x < 4; x++){
	  //Erase dot at old cursor postion

	  tft.drawPixel(hOld+x, vOld+y, ST7735_BLACK);
	}
      }
      delay(10);
    }

    for(int y = -1; y < 2; y++){
      for(int x = -2; x < 4; x++){

	//Draw dot a new cursor postion

	tft.drawPixel(h+x,v+y,ST7735_WHITE);
      }
    }

    if (digitalRead(BUTTON) == LOW){
      Serial.begin(9600);
      Serial.print("OK");
      int blah = -2;
      int bullet = v;

	//Draw dot a new cursor postion
      for(int blah = -2; bullet + blah > -2; blah--){

	tft.drawPixel(h, bullet + blah + 1, ST7735_BLACK);
	tft.drawPixel(h, bullet + blah, ST7735_WHITE); 
	tft.drawPixel(h, bullet + blah-1, ST7735_WHITE); 
	tft.drawPixel(h, bullet + blah-2, ST7735_WHITE); 
      }     
    }

    //Update the cursor position

    vOld = v;
    hOld = h;
  }
}
Exemplo n.º 16
0
void setup_display(void) {
  // initialize the LCD display
  tft.initR(INITR_BLACKTAB);   // initialize a S6D02A1S chip, black tab
  tft.setRotation(1);
  tft.fillScreen(BLACK);
}
Exemplo n.º 17
0
int main() {
  // SETUP
  init();
  Serial.begin(9600);
  tft.initR(INITR_BLACKTAB);  // initialize screen
  
  // Setting the joystick button and LEDs
  pinMode(JOYSTICK_BUTTON, INPUT);
  digitalWrite(JOYSTICK_BUTTON, HIGH);
  
  // Initialize the SD card
  Serial.print("Initializing SD card...");
  if (!SD.begin(SD_CS)) {
    Serial.println("failed!");
    while(true) {} // something is wrong
  } 
  else {Serial.println("OK!");}
  
  // More initialization
  Serial.print("Initializing Raw SD card...");
  if (!card.init(SPI_HALF_SPEED, SD_CS)) {
    Serial.println("failed!");
    while(true) {} // something is wrong
  } 
  else {Serial.println("OK!");}
  
  // Create states for different modes
  // C1 for Mode 1 - MENU screen
  // C2 for Mode 2 - Snake Game
  // C3 for Mode 3 - GAME OVER screen
  // C4 for Mode 4 - Choose level
  // C5 for Mode 5 - PAUSE screen
  typedef enum {C1 = 1, C2, C3, C4, C5, ERR} State;
  State state = C1;
  int select, snakelength;
  
  while (state!=ERR) {
    if  (state == C1) {
      /// ====== MODE 1 - MENU ====== ///
      Serial.println("Currently in Mode 1");
      snakelength = 1;
      init_vert = analogRead(JOYSTICK_VERT); 
      init_horiz = analogRead(JOYSTICK_HORIZ);
      
      // preparations for the game - to not overlap with the pause menu
      q = q_create(720);
      i = 64; // x component
      j = 80; // y component
      q_add(q,i,j); // load into the queue
      random_x = food_x(); // load x coordinate of food piece
      random_y = food_y(); // load y coordinate of food piece
      pausedirection = 0; // set paused direction to 0
      // reset grid to 0
      for (int a = 0; a < 24; a++) {
        for (int b = 0; b < 30; b++) {
        grid[a][b] = 0;
        }
      }
      
      // display main menu
      snake();
      tft.setTextSize(2);
      
      while(true) {
        // alternate highlighting of START
        unsigned long time = millis()%1000;
        int a = time%1000;
        if ((a<17)) {
        tft.setCursor(34, 83);
        tft.fillRoundRect(30,80,65,20,5,WHITE);
        tft.setTextColor(RED);
        tft.print("START");
        }
        else if ((a>500) && (a<520)) {
        tft.setCursor(34, 83);
        tft.fillRoundRect(30,80,65,20,5,RED);
        tft.setTextColor(WHITE);
        tft.print("START");
        }
        // Read the Joystick - HIGH if not pressed, LOW otherwise
        select = digitalRead(JOYSTICK_BUTTON);     
        if (select == LOW) {
          break;
        }
      }
      state = C4; 
    }
    
    else if (state == C2) {
      /// ====== MODE 2 - SNAKE GAME ====== ///
      Serial.println("Currently in Mode 2");
      delay(50);
      soundsetup(); //setting up sound pin
      // print the background
      tft.fillScreen(DARKRED);
      tft.fillRect(4,5,120,150,DARKGRN);
      
      // print the snake
      int x,y;
      x = q_frontx(q);
      y = q_fronty(q);
      tft.fillRect(x,y,5,5, WHITE);
      
      //Bringing the food in, outside while loop first.
      tft.fillRect(random_x, random_y, 5, 5, YELLOW);
      
      // do auto calibration
      int px, py;
      int lastmove;
      
      // read beginning direction chosen by user
      if (pausedirection == 0) {
        direction = read_direction();
      }
      else {
        direction = pausedirection;
      }
      lastmove = direction;
      
      while (true) {
        
        // to direct movement 
        // (without going in reverse direction of previous movement)
        
        // up
        if (direction == 1) {
          if (lastmove == 2) {
            direction = 2;
            j = j-5;
          }
          else {
            j = j+5;
        }
        q_add(q,i,j);
        }
        // down
        else if (direction == 2) {
          if (lastmove == 1) {
            direction = 1;
            j = j+5;
          }
          else {
            j = j-5;
          }
        q_add(q,i,j);
        }
        // right
        else if (direction == 3) {
          if (lastmove == 4) {
            direction = 4;
            i = i-5;
          }
          else {
            i = i+5;
          }
        q_add(q,i,j);
        }
        // left
        else if (direction == 4) {
          if (lastmove == 3) {
            direction = 3;
            i = i+5;
          }
          else {
            i = i-5;
          }
        q_add(q,i,j);
        }
        
        // if the direction is changed, store the new direction & last move
        int new_direc = read_direction();
        if ((new_direc != direction) && (new_direc != 0)) {
          lastmove = direction;
          direction = new_direc;
        }
        
        // if the snake hits a piece of food, the food vanishes and gets replaced 
        if ((i == random_x) && (j == random_y)) {
          // snake grows by 4 squares, except for the first time
          // this allows for it to end up as a max of 720 in the queue
          if (snakelength == 1) {
            q_add(q,i,j);
            q_add(q,i,j);
            q_add(q,i,j);
            snakelength += 3;
          }
          else {
            q_add(q,i,j);
            q_add(q,i,j);
            q_add(q,i,j);
            q_add(q,i,j);
            snakelength += 4;
          }
      if (snakelength < 720) {
        random_x = food_x();
        random_y = food_y();
      
        // if the snake is already there, find a new spot for the food
        while (grid[random_x/5][random_y/5-1] == 1) {
          random_x = food_x();
          random_y = food_y();
        }
        // print the new food
        tft.fillRect(random_x, random_y, 5, 5, YELLOW);
          }
        }
        
        // if the snake runs over itself
        if ((snakelength > 1) && (grid[i/5][j/5-1] == 1)) {
          delay(450); // pause when snake runs into itself
          int m = 0;
          soundLoop();
          while(m < 6000) {
            int rand_x = dissolve_x();
            int rand_y = dissolve_y();
            tft.fillRect(rand_x, rand_y, 5, 5, BLACK);
            m++;
          }
          state = C3;
          break;
        }
        
        px = q_frontx(q);
        py = q_fronty(q);
        // reprint the snake if there is movement
        if ((i != px) || (j != py)) {
          tft.fillRect(i,j,5,5, WHITE);
          grid[i/5][j/5-1] = 1;          // snake body is in grid
          tft.fillRect(px,py,5,5,DARKGRN);
          grid[px/5][py/5-1] = 0;        // snake body is no longer in grid
          q_remove(q);                   // take away from the queue
          delay(speed);                  // controls the speed of the snake
        }
       
        // if any of the borders are hit
        if ((i < 4)||(j < 5)||(i > 119)||(j > 150)) {
          delay(450); // pause when border is hit
          // dissolve the screen
          int m = 0;
          soundLoop();
          while(m < 6000) {
            int rand_x = dissolve_x();
            int rand_y = dissolve_y();
            tft.fillRect(rand_x, rand_y, 5, 5, BLACK);
            m++;
          }
          //~ delay(250);
          state = C3; 
          break;
        }
        
        // Read the Joystick - HIGH if not pressed, LOW otherwise
        select = digitalRead(JOYSTICK_BUTTON);     
        if (select == LOW) {
          state = C5;
          break;
        }
      }
    }
    
    else if (state == C3) {
      /// ====== MODE 3 - GAME OVER ====== ///
      Serial.println("Currently in Mode 3");
      q_destroy(q); // clear the queue
      tft.fillScreen(BLACK);
      tft.fillRoundRect(5,20,118,25,5,RED);
      tft.setCursor(10, 25); 
      tft.setTextColor(BLACK);
      tft.setTextSize(2);
      tft.setTextWrap(true);
      tft.print("GAME OVER");
      tft.print("\n"); 
      
      tft.setCursor(10, 55);
      tft.setTextColor(RED);
      tft.setTextSize(1.5);
      if (snakelength >= 720) {
        snakelength = 720;
        tft.print("YOU WON! CONGRATZ");
      }
      else {
        tft.print("      Oh no!         You hit something!");
      }
      
      tft.setCursor(10, 80);
      tft.setTextColor(WHITE);
      tft.setTextSize(1);
      tft.print("Length of Snake:");
      tft.print(snakelength);
      tft.setCursor(10, 100);
      tft.print("Press the joystick   to return to main    menu");
      
      // Read the Joystick - HIGH if not pressed, LOW otherwise
      while (true) {
        select = digitalRead(JOYSTICK_BUTTON);     
        if (select == LOW) {
          break;
        }
      }
      state = C1;
    }
    
    else if (state == C4) {
      /// ====== MODE 4 - CHOOSE LEVEL ====== ///
      Serial.println("Currently in Mode 4");
      // printing
      // snake display
      snake();
      // difficulty levels
      tft.setTextSize(2);  
      tft.setTextColor(WHITE);
      easy(RED);
      tft.setTextColor(RED);
      medium(WHITE);
      hard(WHITE);
      
      int selection = 1;
      int oldselection;
      while(true) {
        // read direction from the user for updating selection
        oldselection = selection;
        vertical = analogRead(JOYSTICK_VERT);      // will be 0-1023
        delay(100);
        
        // scroll down
        if (vertical > init_vert + 200) {
        selection++;
          if (selection > 3) {
            selection = 0;
          }
        } 
        // scroll up
        else if (vertical < init_vert - 200) {
          selection--;
          if (selection < 0) {
            selection = 3;
          }
        }
        
        if (selection != oldselection) {
          update(selection);
        }
        
        // Read the Joystick - HIGH if not pressed, LOW otherwise
        select = digitalRead(JOYSTICK_BUTTON);     
        if (select == LOW) {
          Serial.print("made selection: ");
          Serial.println(selection);
          if (selection == 1) {speed = 225;}
          else if (selection == 2) {speed = 150;}
          else if (selection == 3) {speed = 75;}
          break;
        }
      }
      state = C2;
    }
    
    else if (state == C5) {
      /// ====== MODE 5 - PAUSE MENU ====== ///
      Serial.println("Currently in Mode 5");
      pausedirection = direction;
      
      // printing snake and pause
      snake();
      tft.setTextSize(2);
      tft.setCursor(34, 73); 
      tft.fillRoundRect(30,70,65,20,5,WHITE);
      tft.setTextColor(RED);
      tft.print("Pause");
      
      while(true) {
        // Read the Joystick - HIGH if not pressed, LOW otherwise
        select = digitalRead(JOYSTICK_BUTTON);     
        if (select == LOW) {
          break;
        }
      }
      // reset grid to 0
      for (int a = 0; a < 24; a++) {
        for (int b = 0; b < 30; b++) {
        grid[a][b] = 0;
        }
      }
      state = C2; 
    }
    //if not any of this:
    else { 
      Serial.println("There has been an error");
      state = ERR; 
    }
  }
    
  Serial.end();
  return 0;
}