Display turns white when running void loop

Hello to everyone!
I am doing a very simple code for displaying some data in my 2.4 tft lcd screen.
I am trying to plot some data, the first would be a reading from a HMC5883L

Theres something strange going on with the code. I can run my setup with no loop and the display performs as expected.
However when i am running the code with the loop, i am getting a white blank display.
Is there any problem between the display and the sensor?

I am posting my code below.

thanks

Fede

#include <Adafruit_GFX.h>
#include <Adafruit_TFTLCD.h>
#include <TouchScreen.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>

Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

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


Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

void setup() {

  mag.begin();
    
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  Serial.begin(9600);
  tft.reset();
  tft.begin(0x9341);

  tft.setRotation(0);
  tft.fillScreen(BLACK);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  
  tft.setCursor(105, 5);
  tft.println("TWD");
  tft.setCursor(90, 30);
  tft.setTextSize(4);
  tft.setTextColor(WHITE);
  tft.println("000");  //REEMPLAZAR CON WIND VALUE
    tft.drawLine(0,70,240,70, WHITE);
  tft.drawLine(0,71,240,71, WHITE);
  
  tft.setTextSize(2);  
  tft.setCursor(105, 80);
  tft.println("AVG");
  tft.setCursor(90, 105);
  tft.setTextSize(4);
  tft.setTextColor(WHITE);
  tft.println("000");  //REEMPLAZAR CON WIND AVG
  tft.drawLine(0,150,240,150, WHITE);
  tft.drawLine(0,151,240,151, WHITE);

  tft.setTextSize(2);  
  tft.setCursor(105, 160);
  tft.println("E.T.");
   

}

void loop() {

 sensors_event_t event; 
    
  mag.getEvent(&event);
  float heading = atan2(event.magnetic.y, event.magnetic.x);
   float declinationAngle = 0.017;
   
  heading += declinationAngle;

    // Correct for when signs are reversed.
  if(heading < 0)
    heading += 2*PI;
    
  // Check for wrap due to addition of declination.
  if(heading > 2*PI)
    heading -= 2*PI;

float headingDegrees = heading * 180/M_PI; 
 
  int ete = millis()/1000;
  
tft.setTextSize(4);  

tft.setCursor(90, 190);
  
  tft.println(heading,0);

  tft.fillRect(80,180,95,50, BLACK);
  



  
  delay(1000);
  
 


  
  

}```

I can run my setup with no loop

No, you can't. The loop() function is required, but it may be empty.

Please post the code that works, and separately, the code that does not.

All of that on a Arduino Uno ?

Working code (displays black screen with some text and lines)

#include <Adafruit_GFX.h>
#include <Adafruit_TFTLCD.h>
#include <TouchScreen.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>

Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

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


Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

void setup() {

  mag.begin();
    
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  Serial.begin(9600);
  tft.reset();
  tft.begin(0x9341);

  tft.setRotation(0);
  tft.fillScreen(BLACK);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  
  tft.setCursor(105, 5);
  tft.println("TWD");
  tft.setCursor(90, 30);
  tft.setTextSize(4);
  tft.setTextColor(WHITE);
  tft.println("000");  //REEMPLAZAR CON WIND VALUE
    tft.drawLine(0,70,240,70, WHITE);
  tft.drawLine(0,71,240,71, WHITE);
  
  tft.setTextSize(2);  
  tft.setCursor(105, 80);
  tft.println("AVG");
  tft.setCursor(90, 105);
  tft.setTextSize(4);
  tft.setTextColor(WHITE);
  tft.println("000");  //REEMPLAZAR CON WIND AVG
  tft.drawLine(0,150,240,150, WHITE);
  tft.drawLine(0,151,240,151, WHITE);

  tft.setTextSize(2);  
  tft.setCursor(105, 160);
  tft.println("E.T.");
   

}

void loop() {

 

}```

NON WORKING CODE:

#include <Adafruit_GFX.h>
#include <Adafruit_TFTLCD.h>
#include <TouchScreen.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>

Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

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


Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

void setup() {

  mag.begin();
    
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  Serial.begin(9600);
  tft.reset();
  tft.begin(0x9341);

  tft.setRotation(0);
  tft.fillScreen(BLACK);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  
  tft.setCursor(105, 5);
  tft.println("TWD");
  tft.setCursor(90, 30);
  tft.setTextSize(4);
  tft.setTextColor(WHITE);
  tft.println("000");  //REEMPLAZAR CON WIND VALUE
    tft.drawLine(0,70,240,70, WHITE);
  tft.drawLine(0,71,240,71, WHITE);
  
  tft.setTextSize(2);  
  tft.setCursor(105, 80);
  tft.println("AVG");
  tft.setCursor(90, 105);
  tft.setTextSize(4);
  tft.setTextColor(WHITE);
  tft.println("000");  //REEMPLAZAR CON WIND AVG
  tft.drawLine(0,150,240,150, WHITE);
  tft.drawLine(0,151,240,151, WHITE);

  tft.setTextSize(2);  
  tft.setCursor(105, 160);
  tft.println("E.T.");
   

}

void loop() {

 sensors_event_t event; 
    
  mag.getEvent(&event);
  float heading = atan2(event.magnetic.y, event.magnetic.x);
   float declinationAngle = 0.017;
   
  heading += declinationAngle;

    // Correct for when signs are reversed.
  if(heading < 0)
    heading += 2*PI;
    
  // Check for wrap due to addition of declination.
  if(heading > 2*PI)
    heading -= 2*PI;

float headingDegrees = heading * 180/M_PI; 
 
  int ete = millis()/1000;
  
tft.setTextSize(4);  

tft.setCursor(90, 190);
  
  tft.println(heading,0);

  tft.fillRect(80,180,95,50, BLACK);
  



  
  delay(1000);
  
 


  
  

}```

Yes sir arduino uno and TFT display with ILI9341 connected without the shield (wired)

Thanks

Fede

I imagine that by adding the extra code, you have run low on SRAM memory, and this line fails to allocate the screen buffer, or something like that.

The linker tells you how much free SRAM you have, before upload.
Which exact display do you have? Post a link to it.

tft.begin() might return success or fail, which you can check and take action (like print a message to the serial monitor).

1 Like

Sketch uses 17430 bytes (54%) of program storage space. Maximum is 32256 bytes.
Global variables use 612 bytes (29%) of dynamic memory, leaving 1436 bytes for local variables. Maximum is 2048 bytes.

Display link:

Cheers

Fede

#define LCD_RESET A4

The magnetometer uses I2C, which also uses A4.

Try changing LCD_RESET to some other pin.

Sounds great. I'll not define lcd reset, anyway i don't need it, and try again.

Which pin do you recommend me to reassign the reset? I tried going for an empty one but still getting white screen

Cheers

Fede

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