Arduino Giga Display Shield drawPixel() Not Working

With the sketch below, nothing is drawing on the screen despite the touch points printing.

#include "Arduino_GigaDisplay_GFX.h"
#include "Arduino_GigaDisplayTouch.h"

GigaDisplay_GFX display;
Arduino_GigaDisplayTouch touchDetector;

#define WHITE 0xffff
#define BLACK 0x0000

void setup() {
  delay(3000);
  display.begin();
  display.fillScreen(WHITE);

  Serial.begin(115200);
  touchDetector.begin();
}

void loop() {
  uint8_t contacts;
  GDTpoint_t points[5];
  int avgX = 0;
  int avgY = 0;

  contacts = touchDetector.getTouchPoints(points);
  if (contacts > 0) {  //Check if at least one touch occurs on the screen
    //Print the coordinates of all simultaneous contacts detected
    for (uint8_t i = 0; i < contacts; i++) {
      Serial.print(points[i].x);
      Serial.print(" ");
      Serial.println(points[i].y);

      avgX += points[i].x;
      avgY += points[i].y;
    }

    avgX /= 5;
    avgY /= 5;

    display.drawPixel(avgX, avgY, BLACK);
  }
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

Please post your full sketch, using code tags when you do

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

It is also helpful to post error messages in code tags as it makes it easier to scroll through them and copy them for examination

Hi,

I’m not sure why it is formatted the way it is. There aren’t any spaces in the sketch but I can’t seem to merge everything into one code grouping on the forum.

Did you try this

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Hi,

Yes, I auto-formatted in the IDE. When pasting, it skips each line, and when applying code tags, it separates each individual line. Trying to delete lines in between doesn’t work either.

Did you use "Copy for Forum" to copy the code from the IDE ?

That worked, apologies.

No problem. You got there in the end so please remember it for next time you post code

Does the code prints the point coordinates to the Serial Monitor?

Yes

Hi @mfusco. The drawPixel function only adds the pixel data to the graphics buffer. The display won't be updated until you call the endWrite() function.

So you can make your sketch work as expected by adding a call to the startWrite function before, and a call to the endWrite() function after:

    display.startWrite();
    display.drawPixel(avgX, avgY, BLACK);
    display.endWrite();

The reason the drawPixel function doesn't do this internally is for the sake of performance in some applications. The drawPixel function is often a helper function which is called many times while constructing graphics objects. In that use case, it is more efficient to store the graphic data only in memory until it has been completely generated, then use a single display update instead of a separate update for drawing each individual pixel.