Adafruit GFX library changing colors randomly

First, obligatory both new to Arduino and new to the forum so please have some patience with me.

I am using a Arduino Mega 2560 to control an Ada fruit 2.8" TFT LCD display via hardware SPI. the controller for the LCD is an ILI9341. When I run the following code, I am supposed to get a 8x8 checkerboard pattern and for the most part I do. The problem is that part way through drawing some of the rectangles, it switches to a random, undefined color. Here is an example of what I mean:

Here is my code:

#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>

// A couple of pins.
#define TFT_DC 44
#define TFT_CS 45

// My colors for the display.
#define BLACK           0x0000
#define WHITE           0xFFFF
#define BLUE_SQUARE     0x759B
#define GRAY_SQUARE     0xA534


// Define the visual LCD.
// Use hardware SPI and the pin numbers defined above for CS/DC.
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

void setup() {
  // Initialive the visual LCD.
  tft.begin();
  tft.setRotation(0);
  tft.fillScreen(BLACK);
  tft.drawRect(3, 3, 2, 233, WHITE);   // Left border of board.
  tft.drawRect(3, 3, 233, 2, WHITE);   // Top border of board.
  tft.drawRect(235, 3, 2, 233, WHITE); // Right border of board.
  tft.drawRect(3, 235, 234, 2, WHITE); // Bottom border of board.
  init_board(); // This function will initialize the board on the LCD.
}

void loop() {
  // put your main code here, to run repeatedly:

}

void init_board(){ // This function is called in setup but it cleans up the setup loop some.
  // This section of code will initialize the chess board squares and also put the chess pieces on their starting squares.
  // Initialize board squares. First, we initialize some of the vaiables that we will need.
  int cursorX = -1;
  int cursorY = 0;
  int color_init_toggle = 1;
  int COLOR = GRAY_SQUARE;
  int x = 0;
  int y = 0;
  
  // There are 64 squares on a chess board.
  for (byte i = 0; i < 64; i++){
    // We add one the the X position, and if that X position is off the board, we set it X to zero and then add one the the Y axis.
    cursorX = cursorX + 1;
    if (cursorX > 7){
      cursorY++;
      cursorX = 0;
      if ((cursorY % 2) == 0){color_init_toggle = 1;} // This is here so that the board will have the correct checkers pattern. Otherwise, The board comes out as columns.
      else{color_init_toggle = 0;}
      
      }
    // We set the color for the square and then switch the value of the toggle so the next square is the opposite color.
    if (color_init_toggle == 0){
      COLOR = BLUE_SQUARE;
      color_init_toggle = 1;}    
    else{
      COLOR = GRAY_SQUARE;
      color_init_toggle = 0;}

    // After that, we set the location in x and y for the top left of the square and then draw out a solid rectangle.
    x = (4+(cursorX*29)); // Each square is 29 pixels by 29 pixels and there is a border of 4 pixes before you get to the game board.
    y = (4+(cursorY*29));
    // The format here is simple: X pos of top left corner, then the Y pos for the same. the the width of the rect, then the height of the rectange. lastly, the color of the rectangle.
    tft.fillRect(x, y, 29, 29, COLOR);
  }

}

If anyone has any ideas or suggestions, I would greatly appreciate them. Thanks is advance.

You've posted correctly, thanks for that. It would be nice to see the wiring diagram. Do you have anything else connected to the SPI pins? Any shields?

For shields, I have a MEGA Sensor Shield V1.3. It just brings the pin headers into a more usable configuration.

For wiring:

image

There is nothing else connected to the SPI pins. Also, the other pins on the LCD are just left unconnected if that matters.

I ran your sketch on a Uno with a Chinese Red SPI ILI9341 display. (with 3.3V level shifters)
It worked fine.

Note that I use the full-fat constructor

You don't need the RESET argument on genuine Adafruit boards. (because Adafruit provide an onboard pullup for TFT_RST pin)
You don't need level shifters on genuine Adafruit boards. (because Adafruit provide onboard level shifters)

David.

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