Uno to Mega Issues

I’ve had to change from Uno to Mega due to memory limitations.

Everything was working on the Uno, but changing to the Mega nothing works.

I was using an Adafruit OLED with built in sd-card.

I have since found out pin numbers were different on the Mega and have updated them (51,52,53)

So this test code now works, and displays stuff on the OLED. Heres part of it:

/***************************************************
  This is a example sketch demonstrating the graphics
  capabilities of the SSD1331 library  for the 0.96"
  16-bit Color OLED with SSD1331 driver chip

  Pick one up today in the adafruit shop!
  ------> http://www.adafruit.com/products/684

  These displays use SPI to communicate, 4 or 5 pins are required to
  interface
  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.
  BSD license, all text above must be included in any redistribution
 ****************************************************/

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1331.h>
#include <SPI.h>


// You can use any (4 or) 5 pins
#define sclk 52
#define mosi 51
#define cs   53
#define rst  9
#define dc   8


// Color definitions
#define	BLACK           0x0000
#define	BLUE            0x001F
#define	RED             0xF800
#define	GREEN           0x07E0
#define CYAN            0x07FF
#define MAGENTA         0xF81F
#define YELLOW          0xFFE0
#define WHITE           0xFFFF

// Option 1: use any pins but a little slower
//Adafruit_SSD1331 display = Adafruit_SSD1331(cs, dc, mosi, sclk, rst);

// Option 2: must use the hardware SPI pins
// (for UNO thats sclk = 13 and sid = 11) and pin 10 must be
// an output. This is much faster - also required if you want
// to use the microSD card (see the image drawing example)
Adafruit_SSD1331 display = Adafruit_SSD1331(&SPI, cs, dc, rst);

float p = 3.1415926;

void setup(void) {
  Serial.begin(9600);
  Serial.print("hello!");
  display.begin();

  Serial.println("init");
  uint16_t time = millis();
  display.fillScreen(BLACK);
  time = millis() - time;

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

  lcdTestPattern();
  delay(1000);

  display.fillScreen(BLACK);
  display.setCursor(0,0);
  display.print("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa");
  delay(1000);

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

  //a single pixel
  display.drawPixel(display.width()/2, display.height()/2, GREEN);
  delay(500);

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

  // optimized lines
  testfastlines(RED, BLUE);
  delay(500);

  testdrawrects(GREEN);
  delay(1000);

  testfillrects(YELLOW, MAGENTA);
  delay(1000);

  display.fillScreen(BLACK);
  testfillcircles(10, BLUE);
  testdrawcircles(10, WHITE);
  delay(1000);

  testroundrects();
  delay(500);

  testtriangles();
  delay(500);

  Serial.println("done");
  delay(1000);
}

see post two…

However, I still can’t get the SD-Card to work, just says failed each time. Here is the code which I’ve updated the pin number (53). Though the first code comments says something about having to use PIN 10?

// Adafruit_ImageReader test for Adafruit ST7735 TFT Breakout for Arduino.
// Demonstrates loading images from SD card or flash memory to the screen,
// to RAM, and how to query image file dimensions.
// Requires three BMP files in root directory of SD card:
// rgbwheel.bmp, miniwoof.bmp and wales.bmp.
// As written, this uses the microcontroller's SPI interface for the screen
// (not 'bitbang') and must be wired to specific pins (e.g. for Arduino Uno,
// MOSI = pin 11, MISO = 12, SCK = 13). Other pins are configurable below.

#include <Adafruit_GFX.h>         // Core graphics library
#include <Adafruit_SSD1331.h>      // Hardware-specific library
#include <SdFat.h>                // SD card & FAT filesystem library
#include <Adafruit_SPIFlash.h>    // SPI / QSPI flash library
#include <Adafruit_ImageReader.h> // Image-reading functions

// Comment out the next line to load from SPI/QSPI flash instead of SD card:
#define USE_SD_CARD

// Color definitions
#define BLACK           0x0000
#define BLUE            0x001F
#define RED             0xF800
#define GREEN           0x07E0
#define CYAN            0x07FF
#define MAGENTA         0xF81F
#define YELLOW          0xFFE0
#define WHITE           0xFFFF

// TFT display and SD card share the hardware SPI interface, using
// 'select' pins for each to identify the active device on the bus.

#define TFT_CS  53 // TFT select pin
#define TFT_RST  9 // Or set to -1 and connect to Arduino RESET pin
#define TFT_DC   8 // TFT display/command pin
#define SD_CS    4 // SD card select pin




#if defined(USE_SD_CARD)
  SdFat                SD;         // SD card filesystem
  Adafruit_ImageReader reader(SD); // Image-reader object, pass in SD filesys
#else
  // SPI or QSPI flash filesystem (i.e. CIRCUITPY drive)
  #if defined(__SAMD51__) || defined(NRF52840_XXAA)
    Adafruit_FlashTransport_QSPI flashTransport(PIN_QSPI_SCK, PIN_QSPI_CS,
      PIN_QSPI_IO0, PIN_QSPI_IO1, PIN_QSPI_IO2, PIN_QSPI_IO3);
  #else
    #if (SPI_INTERFACES_COUNT == 1)
      Adafruit_FlashTransport_SPI flashTransport(SS, &SPI);
    #else
      Adafruit_FlashTransport_SPI flashTransport(SS1, &SPI1);
    #endif
  #endif
  Adafruit_SPIFlash    flash(&flashTransport);
  FatFileSystem        filesys;
  Adafruit_ImageReader reader(filesys); // Image-reader, pass in flash filesys
#endif


Adafruit_SSD1331 tft = Adafruit_SSD1331(&SPI, TFT_CS, TFT_DC, TFT_RST);
Adafruit_Image       img;        // An image loaded into RAM
int32_t              width  = 0, // BMP image dimensions
                     height = 0;

void setup(void) {

  ImageReturnCode stat; // Status from image-reading functions

  Serial.begin(9600);
#if !defined(ESP32)
  while(!Serial);       // Wait for Serial Monitor before continuing
#endif



  tft.begin(); // Initialize screen

  // The Adafruit_ImageReader constructor call (above, before setup())
  // accepts an uninitialized SdFat or FatFileSystem object. This MUST
  // BE INITIALIZED before using any of the image reader functions!
  Serial.print(F("Initializing filesystem..."));
#if defined(USE_SD_CARD)
  // SD card is pretty straightforward, a single call...
  if(!SD.begin(SD_CS, SD_SCK_MHZ(10))) { // Breakouts require 10 MHz limit due to longer wires
    Serial.println(F("SD begin() failed"));
    for(;;); // Fatal error, do not continue
  }
#else
  // SPI or QSPI flash requires two steps, one to access the bare flash
  // memory itself, then the second to access the filesystem within...
  if(!flash.begin()) {
    Serial.println(F("flash begin() failed"));
    for(;;);
  }
  if(!filesys.begin(&flash)) {
    Serial.println(F("filesys begin() failed"));
    for(;;);
  }
#endif
  Serial.println(F("OK!"));

  // Fill screen blue. Not a required step, this just shows that we're
  // successfully communicating with the screen.
  tft.fillScreen(BLUE);

  // Load full-screen BMP file 'daffodil.bmp' at position (0,0) (top left).
  // Notice the 'reader' object performs this, with 'tft' as an argument.
  Serial.print(F("Loading daffodil.bmp to screen..."));
  stat = reader.drawBMP("/daffodil.bmp", tft, 0, 0);
  reader.printStatus(stat);   // How'd we do?

  // Query the dimensions of image 'miniwoof.bmp' WITHOUT loading to screen:
  Serial.print(F("Querying miniwoof.bmp image size..."));
  stat = reader.bmpDimensions("/miniwoof.bmp", &width, &height);
  reader.printStatus(stat);   // How'd we do?
  if(stat == IMAGE_SUCCESS) { // If it worked, print image size...
    Serial.print(F("Image dimensions: "));
    Serial.print(width);
    Serial.write('x');
    Serial.println(height);
  }

  // Load small BMP 'wales.bmp' into a GFX canvas in RAM. This should fail
  // gracefully on Arduino Uno and other small devices, meaning the image
  // will not load, but this won't make the program stop or crash, it just
  // continues on without it. Should work on Arduino Mega, Zero, etc.
  Serial.print(F("Loading wales.bmp to canvas..."));
  stat = reader.loadBMP("/wales.bmp", img);
  reader.printStatus(stat); // How'd we do?

  delay(2000); // Pause 2 seconds before moving on to loop()
}

void loop() {
  for(int r=0; r<4; r++) { // For each of 4 rotations...
    tft.setRotation(r);    // Set rotation
    tft.fillScreen(0);     // and clear screen

    // Load 4 copies of the 'miniwoof.bmp' image to the screen, some
    // partially off screen edges to demonstrate clipping. Globals
    // 'width' and 'height' were set by bmpDimensions() call in setup().
    for(int i=0; i<4; i++) {
      reader.drawBMP("/miniwoof.bmp", tft,
        (tft.width()  * i / 3) - (width  / 2),
        (tft.height() * i / 3) - (height / 2));
    }

    delay(1000); // Pause 1 sec.

    // Draw 50 Welsh dragon flags in random positions. This has no effect
    // on memory-constrained boards like the Arduino Uno, where the image
    // failed to load due to insufficient RAM, but it's NOT fatal.
    for(int i=0; i<50; i++) {
      // Rather than reader.drawBMP() (which works from SD card),
      // a different function is used for RAM-resident images:
      img.draw(tft,                                    // Pass in tft object
        (int16_t)random(-img.width() , tft.width()) ,  // Horiz pos.
        (int16_t)random(-img.height(), tft.height())); // Vert pos
      // Reiterating a prior point: img.draw() does nothing and returns
      // if the image failed to load. It's unfortunate but not disastrous.
    }

    delay(2000); // Pause 2 sec.
  }
}

Can anyone see whats wrong?
Any help would be much appreciated, thanks in advance.
lewis

On an Uno, the SPI pins are:
13 - SCK
12 - MISO
11 - MOSI
10 - Slave Select (can use other pins, but this SS pin must be an output whether you use it or not).

On a Mega, the SPI pins are:
53 - Slave Select (can use other pins, but this SS pin must be an output whether you use it or not).
52 - SCK
51 - MOSI
50 - MISO

You can have multiple SPI devices connected to SCK, MOSI, MISO, each must have its own slave select/chip select tho.

"// Option 2: must use the hardware SPI pins
// (for UNO thats sclk = 13 and sid = 11) and pin 10 must be
// an output."
Pin 10/Uno and 53/Mega is Slave Select, it must be an output, otherwise if low the Uno/Mega
can become the SPI slave, and expect the clock and data to originate from the other device.

If you are using a shield designed to plug onto an Uno and plug it onto a Mega instead, then you can connect the 4 pins above with jumpers and leave 10,11,12,13 as inputs (which is the default state after a reset) and use the original pin declarations.

Thanks Crossroads.

This is the OLED/SD-Card I’m using:

So the following code which doesn’t work (SD Card fails), do I need to set TFT_CS 53 as an output? I’m a bit of a novice!

// Adafruit_ImageReader test for Adafruit ST7735 TFT Breakout for Arduino.
// Demonstrates loading images from SD card or flash memory to the screen,
// to RAM, and how to query image file dimensions.
// Requires three BMP files in root directory of SD card:
// rgbwheel.bmp, miniwoof.bmp and wales.bmp.
// As written, this uses the microcontroller's SPI interface for the screen
// (not 'bitbang') and must be wired to specific pins (e.g. for Arduino Uno,
// MOSI = pin 11, MISO = 12, SCK = 13). Other pins are configurable below.

#include <Adafruit_GFX.h>         // Core graphics library
#include <Adafruit_SSD1331.h>      // Hardware-specific library
#include <SdFat.h>                // SD card & FAT filesystem library
#include <Adafruit_SPIFlash.h>    // SPI / QSPI flash library
#include <Adafruit_ImageReader.h> // Image-reading functions

// Comment out the next line to load from SPI/QSPI flash instead of SD card:
#define USE_SD_CARD

// Color definitions
#define BLACK           0x0000
#define BLUE            0x001F
#define RED             0xF800
#define GREEN           0x07E0
#define CYAN            0x07FF
#define MAGENTA         0xF81F
#define YELLOW          0xFFE0
#define WHITE           0xFFFF

// TFT display and SD card share the hardware SPI interface, using
// 'select' pins for each to identify the active device on the bus.

#define TFT_CS  53 // TFT select pin
#define TFT_RST  9 // Or set to -1 and connect to Arduino RESET pin
#define TFT_DC   8 // TFT display/command pin
#define SD_CS    4 // SD card select pin




#if defined(USE_SD_CARD)
  SdFat                SD;         // SD card filesystem
  Adafruit_ImageReader reader(SD); // Image-reader object, pass in SD filesys
#else
  // SPI or QSPI flash filesystem (i.e. CIRCUITPY drive)
  #if defined(__SAMD51__) || defined(NRF52840_XXAA)
    Adafruit_FlashTransport_QSPI flashTransport(PIN_QSPI_SCK, PIN_QSPI_CS,
      PIN_QSPI_IO0, PIN_QSPI_IO1, PIN_QSPI_IO2, PIN_QSPI_IO3);
  #else
    #if (SPI_INTERFACES_COUNT == 1)
      Adafruit_FlashTransport_SPI flashTransport(SS, &SPI);
    #else
      Adafruit_FlashTransport_SPI flashTransport(SS1, &SPI1);
    #endif
  #endif
  Adafruit_SPIFlash    flash(&flashTransport);
  FatFileSystem        filesys;
  Adafruit_ImageReader reader(filesys); // Image-reader, pass in flash filesys
#endif


Adafruit_SSD1331 tft = Adafruit_SSD1331(&SPI, TFT_CS, TFT_DC, TFT_RST);
Adafruit_Image       img;        // An image loaded into RAM
int32_t              width  = 0, // BMP image dimensions
                     height = 0;

void setup(void) {

  ImageReturnCode stat; // Status from image-reading functions

  Serial.begin(9600);
#if !defined(ESP32)
  while(!Serial);       // Wait for Serial Monitor before continuing
#endif



  tft.begin(); // Initialize screen

  // The Adafruit_ImageReader constructor call (above, before setup())
  // accepts an uninitialized SdFat or FatFileSystem object. This MUST
  // BE INITIALIZED before using any of the image reader functions!
  Serial.print(F("Initializing filesystem..."));
#if defined(USE_SD_CARD)
  // SD card is pretty straightforward, a single call...
  if(!SD.begin(SD_CS, SD_SCK_MHZ(10))) { // Breakouts require 10 MHz limit due to longer wires
    Serial.println(F("SD begin() failed"));
    for(;;); // Fatal error, do not continue
  }
#else
  // SPI or QSPI flash requires two steps, one to access the bare flash
  // memory itself, then the second to access the filesystem within...
  if(!flash.begin()) {
    Serial.println(F("flash begin() failed"));
    for(;;);
  }
  if(!filesys.begin(&flash)) {
    Serial.println(F("filesys begin() failed"));
    for(;;);
  }
#endif
  Serial.println(F("OK!"));

  // Fill screen blue. Not a required step, this just shows that we're
  // successfully communicating with the screen.
  tft.fillScreen(BLUE);

  // Load full-screen BMP file 'daffodil.bmp' at position (0,0) (top left).
  // Notice the 'reader' object performs this, with 'tft' as an argument.
  Serial.print(F("Loading daffodil.bmp to screen..."));
  stat = reader.drawBMP("/daffodil.bmp", tft, 0, 0);
  reader.printStatus(stat);   // How'd we do?

  // Query the dimensions of image 'miniwoof.bmp' WITHOUT loading to screen:
  Serial.print(F("Querying miniwoof.bmp image size..."));
  stat = reader.bmpDimensions("/miniwoof.bmp", &width, &height);
  reader.printStatus(stat);   // How'd we do?
  if(stat == IMAGE_SUCCESS) { // If it worked, print image size...
    Serial.print(F("Image dimensions: "));
    Serial.print(width);
    Serial.write('x');
    Serial.println(height);
  }

  // Load small BMP 'wales.bmp' into a GFX canvas in RAM. This should fail
  // gracefully on Arduino Uno and other small devices, meaning the image
  // will not load, but this won't make the program stop or crash, it just
  // continues on without it. Should work on Arduino Mega, Zero, etc.
  Serial.print(F("Loading wales.bmp to canvas..."));
  stat = reader.loadBMP("/wales.bmp", img);
  reader.printStatus(stat); // How'd we do?

  delay(2000); // Pause 2 seconds before moving on to loop()
}

void loop() {
  for(int r=0; r<4; r++) { // For each of 4 rotations...
    tft.setRotation(r);    // Set rotation
    tft.fillScreen(0);     // and clear screen

    // Load 4 copies of the 'miniwoof.bmp' image to the screen, some
    // partially off screen edges to demonstrate clipping. Globals
    // 'width' and 'height' were set by bmpDimensions() call in setup().
    for(int i=0; i<4; i++) {
      reader.drawBMP("/miniwoof.bmp", tft,
        (tft.width()  * i / 3) - (width  / 2),
        (tft.height() * i / 3) - (height / 2));
    }

    delay(1000); // Pause 1 sec.

    // Draw 50 Welsh dragon flags in random positions. This has no effect
    // on memory-constrained boards like the Arduino Uno, where the image
    // failed to load due to insufficient RAM, but it's NOT fatal.
    for(int i=0; i<50; i++) {
      // Rather than reader.drawBMP() (which works from SD card),
      // a different function is used for RAM-resident images:
      img.draw(tft,                                    // Pass in tft object
        (int16_t)random(-img.width() , tft.width()) ,  // Horiz pos.
        (int16_t)random(-img.height(), tft.height())); // Vert pos
      // Reiterating a prior point: img.draw() does nothing and returns
      // if the image failed to load. It's unfortunate but not disastrous.
    }

    delay(2000); // Pause 2 sec.
  }
}

Yes, you could add this to setup()

pinMode (53, OUTPUT);
pinMode (4, OUTPUT);

to cover both the SD card and the TFT module.

Thanks CrossRoads, I'll give that a go!

The Adafruit examples don’t have the SDCS pin (SD Card, Card Select) connected. It’s the third pin on the OLED after Ground and +3.3V and should, according to your sketch, be connected to Pin 4 (both on UNO and MEGA). For a 5V Arduino, it will have to go through the 5V->3.3V level shifter.

Thanks JohnWasser. My posts probably aren't clear. The first code example was just using the OLED (no SD-Card).

The second code example was using SD Card as well, and does have Pin 4 connected.

Thanks for the help.

Ok everyone thanks for the help. The OLED and SD-Card now work.

My next issue is the DFPlayer, again worked before on the UNO but now this fails to initialise.

Here is the first part of the code:

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1331.h>
#include <SPI.h>
#include <SdFat.h>                // SD card & FAT filesystem library
#include <Adafruit_SPIFlash.h>    // SPI / QSPI flash library
#include <Adafruit_ImageReader.h> // Image-reading functions

#include "SoftwareSerial.h" //DFPLAYER
#include "DFRobotDFPlayerMini.h"  //DFPLAYER


// You can use any (4 or) 5 pins
#define sclk 52 //13 SCK (CK) //12 SO //52
#define mosi 51 //11 SI //51
#define cs   53 //10 OCS (OC) //53
#define rst  9 //R
#define dc   8 //DC
#define SD_CS    4 //SC SD card select pin

// Rotary Encoder Inputs
#define CLK 6
#define DT 5
#define SW 3 //SWITCH BUTTON


// Color definitions
#define	BLACK           0x0000

SdFat                SD;         // SD card filesystem
Adafruit_ImageReader reader(SD); // Image-reader object, pass in SD filesys

//DFPlayer Pins
SoftwareSerial mySoftwareSerial(19, 18); // RX, TX

// Create the Player object
DFRobotDFPlayerMini player;

// Option 1: use any pins but a little slower
//Adafruit_SSD1331 display = Adafruit_SSD1331(cs, dc, mosi, sclk, rst);

// Option 2: must use the hardware SPI pins
// (for UNO thats sclk = 13 and sid = 11) and pin 10 must be
// an output. This is much faster - also required if you want
// to use the microSD card (see the image drawing example)
Adafruit_SSD1331 display = Adafruit_SSD1331(&SPI, cs, dc, rst);


int button = 7;
int press = 0;
int toggle = 0;
int firstTime = true;

//ROTARY VARIABLES
int currentStateCLK;
int lastStateCLK;
String currentDir ="";

void setup(void) {
  pinMode(button, INPUT);
  digitalWrite(7, HIGH);

  // Set encoder pins as inputs
  pinMode(CLK,INPUT);
  pinMode(DT,INPUT);
  pinMode(SW, INPUT_PULLUP);

  
  // Init USB serial port for debugging
  Serial.begin(9600);
  
  // Init serial port for DFPlayer Mini
  mySoftwareSerial.begin(9600);

  if (player.begin(mySoftwareSerial)) {
    Serial.println("DFPLayer OK");
    // Set volume to maximum (0 to 30).
    player.volume(30);
    } else {
    Serial.println("Connecting to DFPlayer Mini failed!");
  }

  // Read the initial state of CLK
  lastStateCLK = digitalRead(CLK);

  // SD card is pretty straightforward, a single call...
  if(!SD.begin(SD_CS, SD_SCK_MHZ(10))) { // Breakouts require 10 MHz limit due to longer wires
    Serial.println(F("SD begin() failed"));
    for(;;); // Fatal error, do not continue
  }

  char bmpFile[14]; //buffer for file name
  //store repetitive file names in PROGMEM to prevent redundent copies
  static const char bmpIntro00[] PROGMEM = "/Intro_00.bmp";
  static const char bmpIntro01[] PROGMEM = "/Intro_01.bmp";
  static const char bmpIntro02[] PROGMEM = "/Intro_02.bmp";
  static const char bmpIntro03[] PROGMEM = "/Intro_03.bmp";
  
  display.begin();
  display.fillScreen(BLACK);
  //SHOW INITIALISING SCREEN
  //reader.drawBMP("/Intro_00.bmp", display, 0, 0);
  strcpy_P(bmpFile, bmpIntro00);
  reader.drawBMP(bmpFile, display, 0, 0);
  delay(50);
  //reader.drawBMP("/Intro_01.bmp", display, 0, 0);
  strcpy_P(bmpFile, bmpIntro01);
  reader.drawBMP(bmpFile, display, 0, 0);
  delay(50);
  //reader.drawBMP("/Intro_02.bmp", display, 0, 0);
  strcpy_P(bmpFile, bmpIntro02);
  reader.drawBMP(bmpFile, display, 0, 0);
  delay(50);
  //reader.drawBMP("/Intro_03.bmp", display, 0, 0);
  strcpy_P(bmpFile, bmpIntro03);
  reader.drawBMP(bmpFile, display, 0, 0);
  delay(50);
  //reader.drawBMP("/Intro_00.bmp", display, 0, 0);
  strcpy_P(bmpFile, bmpIntro00);
  reader.drawBMP(bmpFile, display, 0, 0);
  delay(50);
  //reader.drawBMP("/Intro_01.bmp", display, 0, 0);
  strcpy_P(bmpFile, bmpIntro01);
  reader.drawBMP(bmpFile, display, 0, 0);
  delay(50);
  //reader.drawBMP("/Intro_02.bmp", display, 0, 0);
  strcpy_P(bmpFile, bmpIntro02);
  reader.drawBMP(bmpFile, display, 0, 0);
  delay(50);
  //reader.drawBMP("/Intro_03.bmp", display, 0, 0);
  strcpy_P(bmpFile, bmpIntro03);
  reader.drawBMP(bmpFile, display, 0, 0);
  delay(50);
  //reader.drawBMP("/Intro_00.bmp", display, 0, 0);
  strcpy_P(bmpFile, bmpIntro00);
  reader.drawBMP(bmpFile, display, 0, 0);
  delay(50);
  //reader.drawBMP("/Intro_01.bmp", display, 0, 0);
  strcpy_P(bmpFile, bmpIntro01);
  reader.drawBMP(bmpFile, display, 0, 0);
  delay(50);
  //reader.drawBMP("/Intro_02.bmp", display, 0, 0);
  strcpy_P(bmpFile, bmpIntro02);
  reader.drawBMP(bmpFile, display, 0, 0);
  delay(50);
  //reader.drawBMP("/Intro_03.bmp", display, 0, 0);
  strcpy_P(bmpFile, bmpIntro03);
  reader.drawBMP(bmpFile, display, 0, 0);
  delay(50);
  //reader.drawBMP("/Intro_00.bmp", display, 0, 0);
  strcpy_P(bmpFile, bmpIntro00);
  reader.drawBMP(bmpFile, display, 0, 0);
  delay(50);
  //reader.drawBMP("/Intro_01.bmp", display, 0, 0);
  strcpy_P(bmpFile, bmpIntro01);
  reader.drawBMP(bmpFile, display, 0, 0);
  delay(50);
  //reader.drawBMP("/Intro_02.bmp", display, 0, 0);
  strcpy_P(bmpFile, bmpIntro02);
  reader.drawBMP(bmpFile, display, 0, 0);
  delay(50);
  //reader.drawBMP("/Intro_03.bmp", display, 0, 0);
  strcpy_P(bmpFile, bmpIntro03);
  reader.drawBMP(bmpFile, display, 0, 0);
  delay(50);
  //reader.drawBMP("/Intro_00.bmp", display, 0, 0);
  strcpy_P(bmpFile, bmpIntro00);
  reader.drawBMP(bmpFile, display, 0, 0);
  delay(50);
  //reader.drawBMP("/Intro_01.bmp", display, 0, 0);
  strcpy_P(bmpFile, bmpIntro01);
  reader.drawBMP(bmpFile, display, 0, 0);;
  delay(50);
  //reader.drawBMP("/Intro_02.bmp", display, 0, 0);
  strcpy_P(bmpFile, bmpIntro02);
  reader.drawBMP(bmpFile, display, 0, 0);
  delay(50);
  //reader.drawBMP("/Intro_03.bmp", display, 0, 0);
  strcpy_P(bmpFile, bmpIntro03);
  reader.drawBMP(bmpFile, display, 0, 0);

  //PLAY ZF-1 ANIM
  display.fillScreen(BLACK);
   reader.drawBMP("/Zf1_00.bmp", display, 0, 0);
   delay(0);
   reader.drawBMP("/Zf1_01.bmp", display, 0, 0);
   delay(0);
   reader.drawBMP("/Zf1_02.bmp", display, 0, 0);
   delay(0);
   reader.drawBMP("/Zf1_03.bmp", display, 0, 0);
   delay(0);
   reader.drawBMP("/Zf1_04.bmp", display, 0, 0);
   delay(0);
   reader.drawBMP("/Zf1_05.bmp", display, 0, 0);
   delay(0);
   reader.drawBMP("/Zf1_06.bmp", display, 0, 0);
   delay(0);
   reader.drawBMP("/Zf1_07.bmp", display, 0, 0);
   delay(0);
   reader.drawBMP("/Zf1_08.bmp", display, 0, 0);
   delay(0);
   reader.drawBMP("/Zf1_09.bmp", display, 0, 0);
   delay(0);
   reader.drawBMP("/Zf1_10.bmp", display, 0, 0);
   delay(1500);
   reader.drawBMP("/Zf1_11.bmp", display, 0, 0);
}

void loop() {
  //ButtonPressCode
    press = digitalRead(button);
    if (press == LOW){
      playSound();
    }
  
  // Read the current state of CLK
  currentStateCLK = digitalRead(CLK);

  // If last and current state of CLK are different, then pulse occurred
  // React to only 1 state change to avoid double count
  if (currentStateCLK != lastStateCLK  && currentStateCLK == 1){

    // If the DT state is different than the CLK state then
    // the encoder is rotating CCW so decrement
    if (digitalRead(DT) != currentStateCLK) {
      if(firstTime == true){
        firstTime = false;
      }else{
        if(toggle != -5){
          toggle --;
        }else{
          toggle = 0;
        }
      }
      currentDir ="CCW";
      //RUN IMAGE FUNCTION
      showImage();
    } else {
      if(firstTime == true){
        firstTime = false;
      }else{
        if(toggle != 5){
      // Encoder is rotating CW so increment
        toggle ++;
        }else{
          toggle = 0;
        }
      }
      currentDir ="CW";
      //RUN IMAGE FUNCTION
      showImage();
    }
  }

  // Remember last CLK state
  lastStateCLK = currentStateCLK;
}

and end of code:

void showImage(){

    if(toggle == 0){
          //SHOW MACHINE GUN IMAGE
          reader.drawBMP("/01_bullets.bmp", display, 0, 0);
    }
       
    if((toggle == 1) || (toggle == -5)){
          //SHOW ROCKET IMAGE
          reader.drawBMP("/02_rocket.bmp", display, 0, 0);
    }
       
    if((toggle == 2) || (toggle == -4)){
          //SHOW ARROW IMAGE
          reader.drawBMP("/03_arrow.bmp", display, 0, 0);
    }

    if((toggle == 3) || (toggle == -3)){
          //SHOW NET IMAGE
          reader.drawBMP("/04_net.bmp", display, 0, 0);
    }

    if((toggle == 4) || (toggle == -2)){
          //SHOW FLAME IMAGE
          reader.drawBMP("/05_flame.bmp", display, 0, 0);
    }

    if((toggle == 5) || (toggle == -1)){
          //SHOW ICE IMAGE
          reader.drawBMP("/06_ice.bmp", display, 0, 0);
    }
}

void playSound(){
   if((toggle == 0) && (firstTime != true)){
      //Play the "0001.mp3" in the "mp3" folder on the SD card
      player.playMp3Folder(4);
      delay(500);
   }

   if((toggle == 1) || (toggle == -5)){
    display.setCursor(0,10);
    display.print("Sound 1");
    delay(500);
   }

   if((toggle == 2) || (toggle == -4)){
    display.setCursor(0,10);
    display.print("Sound 2");
    delay(500);
   }

   if((toggle == 3) || (toggle == -3)){
    display.setCursor(0,10);
    display.print("Sound 3");
    delay(500);
   }

   if((toggle == 4) || (toggle == -2)){
    display.setCursor(0,10);
    display.print("Sound 4");
    delay(500);
   }
   
   if((toggle == 5) || (toggle == -1)){
    display.setCursor(0,10);
    display.print("Sound 5");
    delay(500);
  }
}

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