
Have you ever looked at any device and thought about how it
actually works? Perhaps it was a remote controlled boat, elevator, drinks vending machine or electronic toy? Or maybe you wanted to create a robot yourself, invent an electronic control for the model of the railway? Or did you suddenly have a desire to organize the receipt and analysis of a long-term weather forecast? How and with what could you start your own project?
Arduino board will help in practice to reveal some secrets of electronics. Created by Massimo Banzi and David Quartille, the Arduino system offers a budget-friendly way to create interactive projects and objects, such as remote-controlled robots, GPS-based route recording systems and electronic games.
In this book, an overview of 65 projects. An example of one project under the cut.
Chapter 10. Touch Screens
In this chapter, you:
')
• Learn how to connect a resistive touchscreen to an Arduino board;
• learn to read the values that the touch screen can return;
• construct a simple switch that is triggered by touch;
• Construct a switch with a dimming function.
Touch screens surround us from all sides: smartphones, tablet computers and handheld gaming devices. So why shouldn't we use the touch screen for user interaction?
Touch ScreensTouchscreens can be very expensive, but we will use an inexpensive model produced by SparkFun (part number LCD-08977 and BOB-09170), originally designed for the Nintendo DS game console.
This touch screen has dimensions of 5 x 7 cm and is shown in Fig. 10.1, where it is mounted on a breadboard.
Please note that the touch screen is connected to a small circuit board in the upper right corner (circled in Figure 10.1). This adapter is used to connect the touch screen with the breadboard and the Arduino; in fig. 10.2 shows an enlarged image of the adapter.
Touchscreen connectionThe touchscreen adapter connects to the Arduino board, as shown in the table below. 10.1. Table 10.1. Connecting the touchscreen adapter to the Arduino board
Project No. 34: Defining the touch area on the touch screenThe resistive touch screen consists of a glass panel and a flexible plastic membrane, between which there are two layers of resistive coating. One resistive coating acts as the X axis, and the other acts as the Y axis. The resistance of the resistive coating changes depending on the touch point, that is, by measuring the voltage on each layer, you can determine the X and Y coordinates of the touch area.
In this project we use the Arduino board to determine the voltage on each layer and convert it to the integer coordinates of the touch point.
Equipment
The following is the equipment you need for this project:
• Touch screen with adapter.
• One trimmer with a rating of 10 kΩ.
• One LCD with a screen size of 16 x 2 characters.
• Several pieces of wire of different lengths.
• One breadboard.
• Arduino board and USB cable
Connect the touch screen in accordance with the table. 10.1 and the liquid crystal indicator, as shown in fig. 7.2.
SketchEnter and load the next sketch. The most important sections of the sketch are provided with comments:
Project 34 - Determination of the touch area on the touch screen
#include <LiquidCrystal.h> LiquidCrystal lcd(4,5,6,7,8,9); int x,y = 0; void setup() { lcd.begin(16,2); lcd.clear(); } int readX() // X { int xr=0; pinMode(A0, INPUT); pinMode(A1, OUTPUT); pinMode(A2, INPUT); pinMode(A3, OUTPUT); digitalWrite(A1, LOW); // A1 digitalWrite(A3, HIGH); // A3 delay(5); xr=analogRead(0); // X return xr; } int readY() // Y { int yr=0; pinMode(A0, OUTPUT); // A0 pinMode(A1, INPUT); // A1 pinMode(A2, OUTPUT); // A2 pinMode(A3, INPUT); // A3 digitalWrite(14, LOW); // A0 digitalWrite(16, HIGH); // A2 delay(5); yr=analogRead(1); // Y return yr; } void loop() { lcd.setCursor(0,0); lcd.print(" x = "); x=readX(); lcd.print(x); y=readY(); lcd.setCursor(0,1); lcd.print(" y = "); lcd.print(y); delay (200); }
The readX () and readY () ( and ) functions read the voltage on the resistive covers of the touch screen using analogRead () and return the resulting value. The sketch continuously calls these two functions to ensure that the coordinates of the touch area of the touch screen are determined in real time and displayed on the LCD screen ( and ). (Delay delay (5) in each function is necessary, it gives time to analog inputs / outputs to change its state.)
Sketch TestingIn the process of testing the sketch, observe changes in the readings on the LCD screen by touching the touch screen, and notice how the X and Y values change depending on the position of the touch point. Also note what values are displayed on the LCD screen when you do not touch the screen (Fig. 10.3).
Remember these values - they will be useful to you in the event that in the sketch you will determine the absence of a touch on the screen.
Touch Screen CalibrationTouching the touchscreen in the corners, as shown in Fig. 10.4, and writing down the obtained values, you actually calibrate it. In the simplest case, the coordinates of the corners of the screen will be quite enough. With these values, you can divide the touch screen into small areas and use them to control.
By calibrating the touch screen and dividing it into small areas, you can use the readX () and readY () functions to determine whether different control areas on the screen are touched and then use if-then instructions to perform certain operations, as shown in project 35.
Project No. 35: Two-zone switchIn this project we will create a simple switch based on the touch screen. To begin with, we divide the screen into two horizontal zones, as shown in fig. 10.5: on the left is the “on” zone, and on the right is the “off” zone.
Comparing the coordinates of the touch point with the boundaries of the zones, the Arduino board will determine the zone in which the touch occurred. After determining the zone, you can set a high or low voltage level on the digital output, but in this sketch we will simply output the zone name to the port monitor.
SketchEnter and load the following sketch:
// Project 35 - Two-zone switch
int x,y = 0; void setup() { Serial.begin(9600); pinMode(10, OUTPUT); } void switchOn() { digitalWrite(10, HIGH); Serial.print("Turned ON at X = "); Serial.print(x); Serial.print(" Y = "); Serial.println(y); delay(200); } void switchOff() { digitalWrite(10, LOW); Serial.print("Turned OFF at X = "); Serial.print(x); Serial.print(" Y = "); Serial.println(y); delay(200); } int readX() // X { int xr=0; pinMode(A0, INPUT); pinMode(A1, OUTPUT); pinMode(A2, INPUT); pinMode(A3, OUTPUT); digitalWrite(A1, LOW); // A1 digitalWrite(A3, HIGH); // A3 delay(5); xr=analogRead(0); return xr; } int readY() // Y { int yr=0; pinMode(A0, OUTPUT); pinMode(A1, INPUT); pinMode(A2, OUTPUT); pinMode(A3, INPUT); digitalWrite(A0, LOW); // A0 digitalWrite(A2, HIGH); // A2 delay(5); yr=analogRead(1); return yr; } void loop() { x=readX(); y=readY(); // ""? if (x<=900 && x>=500) { switchOn(); } // ""? if (x<500 && x>=100) { switchOff(); } }
Operating principleTwo if statements in the void loop () function define the zone in which the touch occurred. If the touch point is in the left zone, then the touch is interpreted as the “turn on” command. If the touch point is in the right zone, the touch is interpreted as a “turn off” command.
NoteThe Y coordinate in this project is ignored, because the touch screen is conventionally divided by a vertical border into two horizontal zones. If we also defined horizontal boundaries, then it would be necessary to check the Y coordinate, as we will see in project 36.
»More information about the book can be found on
the publisher's website.»
Table of Contents»
ExcerptFor Habrozhiteley a 25% discount on the coupon -
Arduino