Series: Tried Using Touch Panel TFT Display GT-SP with Arduino

Setting the range of motion for a servo motor connected to an Arduino

Setting the range of motion for a servo motor connected to an Arduino

Hello everyone, I’m @kissaten, a beginner in electronics. In this series, I’m explaining the process of connecting a 7-inch touch display (GT-SP series “GTWV070S3A00P”) to an Arduino and working on various projects.

In the previous episode, we used a decorated slider on the GT-SP to control a servo motor connected to the Arduino. This time, we will fine-tune the slider’s range of motion on the GT-SP using more precise controls over the objects placed on it.

Reading this article will be best understood if you are familiar with the topics discussed previously, such as “Controlling a Servo Motor Connected to Arduino from a Touch Screen” and “Display values obtained from Arduino in a highly visible design“. We will apply the decorative techniques introduced before, so if you haven’t seen the previous articles, I highly recommend checking them out.

Creating Screens in GT Design Studio

In this session, we’ll be using the servo motor featured in our previous article. As a new feature, we’ve set up a separate page and created a range of motion for the servo motor using “Edit Object”. We’ve also prepared corresponding programming.

We’ve displayed the “minimum angle” and “maximum angle” on either side of the slider and added buttons for page navigation. When moving to a page, we send the name of the destination page as a prefix. This allows the Arduino to recognize the page and perform the appropriate actions for that page.

Additionally, we’ve made it easier for the Arduino to understand what actions are being performed by prefixing operation names to strings when moving the slider or setting ranges. This prefix is sent simultaneously.

Specifically,

Page Navigation Feature:

  • We’ve added a function that sends the name of the destination page when a page navigation button is pressed.

Operation Identification Feature:

  • Before performing any operation, we send a string with the operation name prefixed to allow the host to identify which action is being taken.

Error Display Feature:

  • We’ve added a new POPUP page to display when invalid values are entered.

For detailed designs of each feature, please refer to the previous article.

Additionally, we have set a common design for the buttons as follows.

From the BUTTON properties, set the OFF Text Color (when the button is not pressed) to 0x00FFFFFF and the OFF Back Color to 0x005F5F5F. For the Color ON (when the button is pressed), set the ON Text Color to 0x00FFFFFF and the ON Back Color to 0x003F3F3F.

In addition, we will set the font for the button text (Text0) based on the guidelines from the previous article.

Introducing Three New Pages

In this update, we are introducing three distinct pages. The first page is dedicated to slider operations. On this page, we have placed a slider, and objects that display the “minimum angle,” “maximum angle,” and the current angle of the servo motor.

The second page is designed for setting the “minimum angle” and “maximum angle” of the slider. Both the first and second pages include buttons for switching between pages.

The third page features an error message popup for the slider. This popup appears when the “minimum angle” or “maximum angle” settings on the second page fall outside the slider’s designated range (outside 0 degrees to 180 degrees).

Page Switching Events

Buttons for navigating between pages (“BUTTON_0” and “BUTTON_2”) are provided on “PAGE_0” and “PAGE_1”, respectively. During navigation, the Arduino is informed of the current page via “DATA_TX_FIX”, which transmits the name of each page.

The photo shows the Event Edit window for ‘BUTTON_0’ on PAGE_0. In the DATA_TX_FIX’s VAL field, ‘PAGE_1’ is entered as TEXT for ‘BUTTON_0’. Similarly, ‘PAGE_0’ is set for ‘BUTTON_2’ located on PAGE_1. By incorporating these prefixes, the program is able to adjust its operations accordingly.

Resizing the Third Page

The third page, which is the error page used as a popup, requires size adjustments.

In the properties of the third page (PAGE_2), change the General > Size X and Size Y values. For this update, set Size X to ‘500’ and Size Y to ‘300’.

Description of Objects on Each Page:

About PAGE_0

PAGE_0 is the main screen displaying the slider. The difference from the previous project is the addition of text objects “TEXT_2” and “TEXT_3” on either side of the slider, which display the “minimum angle” and “maximum angle”, respectively. Additionally, a button object “BUTTON_0” has been placed at the bottom to switch to the settings page for adjusting angles.

Changes have also been made to the slider object’s event settings; it is now configured to send ‘RAD’ as a prefix via DATA_TX_FIX.

About PAGE_1

This page is designed for setting the “minimum angle” and “maximum angle” of the servo motor.

Two edit objects are provided for numeric input. In the properties of these edit objects, set Style Keypad > Pad Mode to “TENKEY”. This allows you to switch from the default alphanumeric keyboard to a numeric-only input pad.

Additionally, as default values for the “minimum angle” and “maximum angle”, set ‘0’ in the Text 0 > Text 0 field of the edit object “EDIT_0”, and ‘180’ in the Text 0 > Text 0 field of the edit object “EDIT_1”.

The design settings for the EDIT objects are set as follows.

 

For this iteration, we have chosen a design based on black, as shown in the image. We set various colors under the properties of the EDIT objects: Color, Color KeyPad, and Color DISABLE.

As for the event settings, “BUTTON_1” is configured to use ‘RAD_SET’ as a prefix in DATA_TX_FIX. This setup allows the numerical values entered into the edit objects (stored in Text 0 > Text 0) to be transmitted via DATA_TX.

About PAGE_2

PAGE_2 is a popup page designed to display an error when the angle range does not fall within 0-180 degrees, or when the minimum angle exceeds the maximum angle.

For the button object labeled “Close” (“BUTTON_3”), a POPUP event is assigned. From the Event Edit’s ACTION section, select “POPUP_SELECT”. Once a NEW ACTION is added, choose “<POPUP_CLOSE>”.

Creating a Project for Writing to a Touchscreen

The program prepared for this occasion includes elements that determine the GT-SP’s page, allowing for corresponding processing. On “PAGE_0,” we have precise control over the servo motor’s movements, while “PAGE_1” allows for specialized operations like setting the slider’s range. Additionally, actions are implemented to indicate errors when values outside the slider’s range are set.

Specifically,

Page Management:

  • Added a New Variable to Indicate the Current Page
  • When a string that begins with “PAGE” is received, it is recognized as a command to change pages, and the variable indicating the current page is updated accordingly.
  • When a string that does not start with “PAGE” is received, processes specific to the current page are executed.

Function Addition:

  • A function to read strings has been added.

Processing for Each Page:

  • The processPAGE_0 function handles operations on “PAGE_0,” primarily relating to the servo motor’s movements. The range for the slider is specified by RAD_min and RAD_max.
  • The processPAGE_1 function processes operations on “PAGE_1.” Here, when a signal starting with “RAD_SET” is received, the subsequent MIN and MAX values are stored in variables.

Error Checking and Display:

  • Checks whether MIN and MAX values are within the appropriate range. If the values are correct, the MIN and MAX values displayed next to the slider bar on PAGE_0 are updated.
  • If the values are found to be incorrect, an error dialog popup window is displayed.

Page Control Features:

  • A function for page control has been added.

 

Decoding the Program

In the main loop, data starting with “PAGE” is recognized as a page transition, and other data triggers processes specific to the current page.

In processPAGE_0, the servo motor’s angle is adjusted. Specifically, data beginning with “RAD” is received, and its value is mapped to the servo motor’s operational range to change the angle. In processPAGE_1, the operational range of the servo motor (minimum and maximum angles) is set. Data beginning with “RAD_SET” is used to read the minimum and maximum values, which are then checked to ensure they are within the range before updating the variables.

Execution Results

This project is an enhancement of the previous project, now utilizing the GT-SP display to allow users to directly adjust the operational range of the servo motor. This improvement enables users to easily set the necessary operational ranges for specific applications, which holds significant importance. The flexible range setting feature is applicable to a variety of applications, opening up new possibilities for projects using Arduino and GT-SP.

Moreover, the use of prefixes to differentiate operations suggests promising potential for future program development.

In our next session, we will tackle a project to control the lighting status of LEDs from the display.