Calling a function within a switch/case statement

Hello,

I would like to execute a function by sending a case using Labview. The idea is to control a 2.8" TFT LCD (http://www.nuelectronics.com/estore/index.php?main_page=product_info&cPath=1&products_id=19) and to display defined patterns upon calling a case via Labview. To this end I have modified the LIFA (Labview Interface for Arduino) provided by National Instruments and applied it sucessfully, e.g. to make an led blink (=calling functions by sending a case via Labview works). I also sucessfully used the Arduino TFT library (www.nuelectronics.com/download/projects/TFT_lib.zip) to control the LCD. What I can't get to work is the combination of the TFT library and LIFA. All connections are functional, Labview works, the arduino works, the library works and calling self-made functions works, the compiler is not complaining. Would you please have a look at my code and check what I am doing wrong here? Thank you very much!

Bests,
Daniel

LVIFA_Base_TFT_Arduino_Forum.zip (20.6 KB)

TFTlib.zip (46.7 KB)

What I can't get to work is the combination of the TFT library and LIFA.

Before looking at the code, you need to explain this statement.

The code does something.

You expect it to do something.

What those two somethings are is not clear.

calling functions by sending a case via Labview works

You don't "send a case" to the Arduino. You send data to the Arduino that it evaluates in a switch statement, and executes the appropriate case block.

Please excuse the inappropriate terminology, I am very new to C++ and Arduino. And thank you for correcting me - that's the best way for learning new languages (at least from my own experience).

My code is supposed to initialize a TFT LCD and set the pixel color to red. When I use a simple sketch as shown below the LCD is initialized and the pixel color is set to red:

#include <TFT_Graphics.h>

Graphics tft;

void setup()
{
	tft.initialize(LCD_HORIZONTAL);
	tft.ClearScreen(RED);
}


void loop()
{	
	}

When I use the above shown code with the Labview Interface for Arduino, i.e. send data via Labview to Arduino, that evaluates it in a switch statement and executes a case block, the TFT LCD is not initialized and red pixels are not turned on.

This is the data send by Labview: 0x41

This is my case block:

case 0x41: // Data sent by Labview
MyTFT();    // Function called in the case block
break;

This is the function MyTFT () called in the case block:

void MyTFT()   
{
  Graphics tft;                              //Create object of class 'Graphics' in library header file Graphics.h 
  tft.initialize(LCD_HORIZONTAL);   //LCD initialization
  tft.ClearScreen(RED);                 //turn on red pixels 
}

Please find the complete Labview Interface for Arduino sketch attached to the first posting.Hope this is a bit clearer. Thanks again!

When I use the above shown code with the Labview Interface for Arduino, i.e. send data via Labview to Arduino, that evaluates it in a switch statement and executes a case block, the TFT LCD is not initialized and red pixels are not turned on.

How do you know that MyTFT() is even being called?

In that method, you create a local variable, tft, and call two methods on that instance. Then, the function ends and the local instance goes away. This may well undo any changes that the instance made to the hardware.

Why isn't tft still global?

I am mot entirely sure that MyTFT() is called, but what I did is that I used a MyBlink() function (turns on and off and led) in the same case block after MyTFT(). My Blink() was executed and the led blinked upon sending data via labview.

I also used tft as global variable (listed just after the include statements) but I got the same result as in the example provided. I will try it with tft as a global variable again.

Hi PaulS,

I've changed my code and tried it again with tft as a global variable but it still does not work. How do I check if MyTFT() is called? Thanks!

Daniel

Bertbert:
Hi PaulS,

I've changed my code and tried it again with tft as a global variable but it still does not work. How do I check if MyTFT() is called? Thanks!

Daniel

For example you could toggle pin 13 inside MyTFT(). When configured as output, that pin drives the on-board green led.

I just tried that and it works, so it seems that MyTFT() is executed.

Do you have any suggestions on how to proceed from here?

So if MyTFT is called but you don't observe what you expect, then it probably means MyTFT function doesn't work. I'd write a test sketch that would leave all the labview stuff out and just test MyTFT.

Ok, but how do I 'simulate' the data send by Labview to execute the case block?

This sketch works fine (also the uncommended code):

#include <TFT_Graphics.h>

Graphics tft;

void setup()
{
	tft.initialize(LCD_HORIZONTAL);
	tft.ClearScreen(BLACK);
        //tft.FastSolidRect(85, 10, 240, 231, BLACK);
        //tft.DrawCircle(160,120, 100, BLUE, 1);
}


void loop()
{	
//tft.FastSolidRect(85, 10, 240, 231, RED);
//delay(500);
//tft.DrawCircle(160,120, 100, BLUE, 1);
//delay(500);
	}

This one works too:

#include <TFT_Graphics.h>

Graphics tft;

void setup()
{

}


void loop()
{	
MyTFT();
delay(2000);
	}


void MyTFT()                        
{ 
  tft.initialize(LCD_HORIZONTAL);   
  tft.ClearScreen(RED);            
}

Bertbert:
Ok, but how do I 'simulate' the data send by Labview to execute the case block?

Just send the byte labview would send, only don't use labview but a serial terminal. Ascii code 0x41 is 65, i.e. 'A' (capital A) char. You can even use arduino serial monitor...

I followed your suggestions and found that MyTFT() was not executed but I figured out a way that involved ending and re-starting the serial communication to get the custom code running. Thanks heaps for your comments!