Arduino TFT LCDタッチスクリーンチュートリアル – ステップバイステップガイド
このArduinoタッチスクリーンチュートリアルでは、TFT LCDタッチスクリーンをArduinoで使用する方法を学びます。以下の動画をご覧いただくか、書面によるチュートリアルをお読みください。 このチュートリアルでは3つの例を用意しました。最初の例は超音波センサーを使った距離測定です。センサーからの出力、つまり距離が画面に表示され、タッチスクリーンで単位(センチメートルまたはインチ)を選択できます。 次の例は、3つのRGBスライダーを使ったRGB LEDの制御です。例えば青のスライダーを動かすと、LEDが青く光り、最大値に近づくにつれて明るくなります。スライダーは0〜255の範囲で動き、それらを組み合わせることでRGB LEDに任意の色を設定できますが、LED自体の色再現性には限界があることをご了承ください。 3つ目の例はゲームです。実はスマートフォン向けの人気ゲーム「Flappy Bird」のレプリカです。プッシュボタンまたはタッチスクリーン自体を使ってプレイできます。 それでは、これらの例を順に、コードの背景にある仕組みをステップバイステップで解説していきます。 例として、3.2インチTFTタッチスクリーンとTFT LCD Arduino Megaシールドの組み合わせを使用します。シールドが必要なのは、TFTタッチスクリーンが3.3Vで動作するのに対し、Arduino Megaの出力は5Vだからです。最初の例ではHC-SR04超音波センサー、2つ目の例では3つの抵抗付きRGB LED、ゲーム例ではプッシュボタンを使用します。また、ArduinoボードとTFTシールドの間に挿入できるよう、ピンヘッダーをはんだ付けし、1本を曲げたカスタムピンヘッダーも自作しました。 以下のサイトから部品を入手できます: 回路図はこちらです。GNDピン、デジタルピン8〜13、およびピン14を使用します。5VピンはTFTスクリーンで使用済みのため、コードのsetup()でピン13を即座にHIGHに設定し、VCCとして使用します。 コードがやや長いため、理解しやすいようセクションごとに分けて説明し、最後に完全なソースコードを掲載します。 Henning Karlsen氏作成のUTFTライブラリとURTouchライブラリを使用します。彼の素晴らしい功績に感謝します。これらのライブラリにより、TFTスクリーンを非常に簡単に使用でき、多くの異なるサイズ、シールド、コントローラーに対応しています。ライブラリは彼のウェブサイトRinkyDinkElectronics.comからダウンロードでき、デモ例や詳細なドキュメントも公開されています。 ライブラリをインクルードした後、UTFTオブジェクトとURTouchオブジェクトを作成します。これらのオブジェクトのパラメータはTFTスクリーンとシールドのモデルによって異なり、ライブラリのドキュメントで確認できます。 次に、ライブラリ付属のフォントを定義し、プログラムに必要な変数を定義します。setup()ではスクリーンとタッチの初期化、接続されたセンサー、LED、ボタンのピンモード設定、そして最初にdrawHomeScreen()カスタム関数を呼び出してプログラムのホーム画面を描画します。 では、プログラムのホーム画面を作成する方法を説明します。setBackColor()関数でテキスト表示領域の背景色を黒に設定します。次に色を白に設定し、ビッグフォントを選択、print()関数で"Arduino TFT Tutorial"という文字列を画面中央、Y軸10ピクセル下に表示します。次に色を赤に設定し、テキスト下に赤い線を引きます。その後、色を白に戻し、"by HowToMechatronics.com"をスモールフォントで、"Select Example"をビッグフォントで表示します。 次は距離センサーのボタンです。まず色を設定し、fillRoundRect()関数で角丸長方形を塗りつぶして描画します。次に色を白に戻し、drawRoundRect()関数で同じ位置に塗りつぶしなしの角丸長方形を描画することで、ボタンにフレームがあるような見た目にします。ボタンの上にビッグフォントでテキストを表示し、背景色をボタンの塗りつぶし色と同じにします。他の2つのボタンも同じ手順で作成します。 次に、ボタンを機能させ、押されたときに適切な例に遷移するようにします。setup()でcurrentPage変数に文字'0'を設定し、ホーム画面にいることを示します。これが真であり、かつ画面が押された場合、このif文が真になり、画面が押されたX座標とY座標を取得します。それが最初のボタンの領域であれば、drawDistanceSensor()カスタム関数を呼び出して距離センサー例をアクティブにします。また、currentPage変数に文字'1'を設定し、最初の例にいることを示します。drawFrame()カスタム関数は、ボタンが押されたときにハイライトするために使用します。他の2つのボタンも同様の手順です。 drawDistanceSensor()カスタム関数は、ボタンが押されたときに1回だけ呼び出され、ホーム画面の場合と同様にこの例のすべてのグラフィックを描画します。一方、getDistance()カスタム関数は、センサーで測定された最新の距離を表示するために繰り返し呼び出す必要があります。 こちらがその関数で、超音波センサーを使って距離を計算し、SevenSegNumフォントで緑色の値をセンチメートルまたはインチで表示します。超音波センサーの仕組みについて詳しく知りたい場合は、その専用チュートリアルをご覧ください。loop()セクションでは、単位選択ボタンや戻るボタンが押されたときの処理がわかります。 次はRGB LED制御の例です。2番目のボタンが押されると、drawLedControl()カスタム関数が1回だけ呼び出されてこの例のグラフィックが描画され、setLedColor()カスタム関数が繰り返し呼び出されます。この関数では、タッチスクリーンを使って3つのスライダーの値を0〜255に設定します。if文で各スライダーの領域を限定し、スライダーのX値を取得します。各スライダーのX座標値は38〜310ピクセルですが、これらを0〜255の値にマッピングし、LEDを光らせるためのPWM信号として使用します。RGB LEDの仕組みについて詳しく知りたい場合は、その専用チュートリアルをご覧ください。このカスタム関数の残りのコードはスライダーの描画用です。loop()セクションには、LEDを消灯させる戻るボタンの処理のみがあります。 次はArduinoゲームの例ですが、少し複雑なので次のチュートリアルで詳しく解説します。 コードを動作させてコンパイルするには、Arduinoスケッチと同じディレクトリに追加の「.c」ファイルを含める必要があります。このファイルは3つ目のゲーム例用で、鳥のビットマップデータです。この部分の仕組みについて詳しくは、専用のチュートリアルをご覧ください。こちらからダウンロードできます: プログラムの完全なソースコードはこちら:概要



このArduinoタッチスクリーンに必要なパーツ


Arduinoタッチスクリーンコード
#include <UTFT.h>
#include <URTouch.h>
//==== オブジェクト作成
UTFT myGLCD(SSD1289,38,39,40,41); // パラメータは使用するディスプレイ/シールドモデルに合わせて調整
URTouch myTouch( 6, 5, 4, 3, 2);
//==== 変数定義
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];
extern unsigned int bird01[0x41A];
int x, y;
char currentPage, selectedUnit;
// 超音波センサー
const int VCC = 13;
const int trigPin = 11;
const int echoPin = 12;
long duration;
int distanceInch, distanceCm;
// RGB LED
const int redLed = 10;
const int greenLed = 9;
const int blueLed = 8;
int xR=38;
int xG=38;
int xB=38;
// Floppy Bird
int xP = 319;
int yP = 100;
int yB = 30;
int fallRateInt = 0;
float fallRate =0;
int score=0;
const int button = 14;
int buttonState = 0;
void setup() {
// 初期設定
myGLCD.InitLCD();
myGLCD.clrScr();
myTouch.InitTouch();
myTouch.setPrecision(PREC_MEDIUM);
// ピンモード定義
pinMode(VCC, OUTPUT); // VCC
pinMode(trigPin, OUTPUT); // trigPinを出力に設定
pinMode(echoPin, INPUT); // echoPinを入力に設定
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(button, INPUT);
digitalWrite(VCC, HIGH); // +5V - ピン13をVCCとして使用
drawHomeScreen(); // ホーム画面を描画
currentPage = '0'; // ホーム画面にいることを示す
selectedUnit = '0'; // 最初の例で選択される単位(cmまたはinch)を示す
}コード言語: Arduino (arduino)// drawHomeScreen - カスタム関数
void drawHomeScreen() {
// タイトル
myGLCD.setBackColor(0,0,0); // テキスト表示領域の背景色を黒に設定
myGLCD.setColor(255, 255, 255); // 色を白に設定
myGLCD.setFont(BigFont); // フォントをビッグに設定
myGLCD.print("Arduino TFT Tutorial", CENTER, 10); // 文字列を画面に表示
myGLCD.setColor(255, 0, 0); // 色を赤に設定
myGLCD.drawLine(0,32,319,32); // 赤い線を描画
myGLCD.setColor(255, 255, 255); // 色を白に設定
myGLCD.setFont(SmallFont); // フォントをスモールに設定
myGLCD.print("by HowToMechatronics.com", CENTER, 41); // 文字列を表示
myGLCD.setFont(BigFont);
myGLCD.print("Select Example", CENTER, 64);
// ボタン - 距離センサー
myGLCD.setColor(16, 167, 103); // 緑色を設定
myGLCD.fillRoundRect (35, 90, 285, 130); // 塗りつぶし角丸長方形を描画
myGLCD.setColor(255, 255, 255); // 色を白に設定
myGLCD.drawRoundRect (35, 90, 285, 130); // 塗りつぶしなしの角丸長方形を描画(フレーム効果)
myGLCD.setFont(BigFont); // フォントをビッグに設定
myGLCD.setBackColor(16, 167, 103); // テキスト表示領域の背景色を緑に設定(ボタンと同じ色)
myGLCD.print("DISTANCE SENSOR", CENTER, 102); // 文字列を表示
// ボタン - RGB LED制御
myGLCD.setColor(16, 167, 103);
myGLCD.fillRoundRect (35, 140, 285, 180);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (35, 140, 285, 180);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(16, 167, 103);
myGLCD.print("RGB LED CONTROL", CENTER, 152);
// ボタン - Birduino
myGLCD.setColor(16, 167, 103);
myGLCD.fillRoundRect (35, 190, 285, 230);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (35, 190, 285, 230);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(16, 167, 103);
myGLCD.print("BIRDUINO GAME", CENTER, 202);
}コード言語: Arduino (arduino)//========== loopセクション ========
void loop() {
// ホーム画面
if (currentPage == '0') {
if (myTouch.dataAvailable()) {
myTouch.read();
x=myTouch.getX(); // 押されたX座標
y=myTouch.getY(); // 押されたY座標
// 距離センサーボタンが押された場合
if ((x>=35) && (x<=285) && (y>=90) && (y<=130)) {
drawFrame(35, 90, 285, 130); // カスタム関数 - 押されたときにボタンをハイライト
currentPage = '1'; // 最初の例にいることを示す
myGLCD.clrScr(); // 画面をクリア
drawDistanceSensor(); // 1回だけ呼び出される(次のループでは上記if文が偽になるため)。この関数で最初の例のグラフィックを描画。
}
// RGB LED制御ボタンが押された場合
if ((x>=35) && (x<=285) && (y>=140) && (y<=180)) {
drawFrame(35, 140, 285, 180);
currentPage = '2';
myGLCD.clrScr();
drawLedControl();
}
// Birduinoゲームボタンが押された場合
if ((x>=35) && (x<=285) && (y>=190) && (y<=230)) {
drawFrame(35, 190, 285, 230);
currentPage = '3';
myGLCD.clrScr();
myGLCD.setColor(114, 198, 206);
myGLCD.fillRect(0,0,319,239);
drawGround();
drawPilars(xP,yP);
drawBird(30);
delay(1000);
}
}
}
// 距離センサー例
if (currentPage == '1') {
getDistance(); // センサーから距離を取得し、最初の例にいる間は繰り返し呼び出して最新の結果を表示
if (myTouch.dataAvailable()) {
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
// センチメートルボタンが押された場合
if ((x>=10) && (x<=135) &&(y>=90) && (y<=163)) {
selectedUnit = '0';
}
// インチボタンが押された場合
if ((x>=10) && (x<=135) &&(y>=173) && (y<=201)) {
selectedUnit = '1';
}
// 戻るボタンが押された場合
if ((x>=10) && (x<=60) &&(y>=10) && (y<=36)) {
drawFrame(10, 10, 60, 36);
currentPage = '0'; // ホーム画面に戻ることを示す
myGLCD.clrScr();
drawHomeScreen(); // ホーム画面を描画
}
}
}
// RGB LED制御
if (currentPage == '2') {
setLedColor();
if (myTouch.dataAvailable()) {
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
// 戻るボタン
if ((x>=10) && (x<=60) &&(y>=10) && (y<=36)) {
drawFrame(10, 10, 60, 36);
currentPage = '0';
myGLCD.clrScr();
drawHomeScreen();
// LEDを消灯
analogWrite(redLed, 0);
analogWrite(greenLed, 0);
analogWrite(blueLed, 0);
}
}
}
//==== ゲーム例のコードセクションは次のチュートリアルで解説 ====
// Birduinoゲーム
if (currentPage == '3') {
//delay(1);
xP=xP-3;
drawPilars(xP, yP);
yB+=fallRateInt;
fallRate=fallRate+0.4;
fallRateInt= int(fallRate);
if (yB>=220) {
yB=220;
}
if(yB>=180 || yB<=0){
restartGame();
}
if((xP<=85) && (xP>=30) && (yB<=yP-2)){
restartGame();
}
if((xP<=85) && (xP>=30) && (yB>=yP+60)){
restartGame();
}
drawBird(yB);
if (xP<=-51){
xP=319;
yP = rand() % 100+20;
score++;
}
if (myTouch.dataAvailable()) {
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
if ((x>=0) && (x<=319) &&(y>=50) && (y<=239)) {
fallRate=-5;
}
}
buttonState = digitalRead(button);
if (buttonState == HIGH) {
fallRate=-5;
}
}
if (myTouch.dataAvailable()) {
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
if ((x>=10) && (x<=60) &&(y>=10) && (y<=36)) {
drawFrame(10, 10, 60, 36);
currentPage = '0';
myGLCD.clrScr();
drawHomeScreen();
analogWrite(redLed, 0);
analogWrite(greenLed, 0);
analogWrite(blueLed, 0);
}
}
}コード言語: Arduino (arduino)//===== getDistance() - カスタム関数
void getDistance() {
// trigPinをクリア
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// trigPinを10マイクロ秒HIGHに設定
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// echoPinを読み取り、音波の往復時間をマイクロ秒で取得
duration = pulseIn(echoPin, HIGH);
// 距離を計算
distanceCm= duration*0.034/2;
distanceInch= distanceCm/2.53;
// センチメートルで距離を表示
if (selectedUnit == '0' && distanceCm <=400) {
myGLCD.setFont(SevenSegNumFont);
myGLCD.setColor(0, 255, 0);
myGLCD.setBackColor(0, 0, 0);
myGLCD.printNumI(distanceCm,130, 145, 3,'0');
myGLCD.setFont(BigFont);
myGLCD.print("cm ", 235, 178);
}
// インチで距離を表示
if (selectedUnit == '1' && distanceCm <=160) {
myGLCD.setFont(SevenSegNumFont);
myGLCD.setColor(0, 255, 0);
myGLCD.setBackColor(0, 0, 0);
myGLCD.printNumI(distanceInch,130, 145, 3,'0');
myGLCD.setFont(BigFont);
myGLCD.print("inch", 235, 178);
}
delay(10);
}コード言語: Arduino (arduino)//============= setLedColor() - カスタム関数
void setLedColor() {
if (myTouch.dataAvailable()) {
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
// 赤色スライダーの領域
if( (y>=130) && (y<=156)) {
xR=x; // 押されたX値を変数xRに格納
if (xR<=38) { // スライダー領域を38ピクセル以上に制限
xR=38;
}
if (xR>=303){ // スライダー領域を310ピクセル以下に制限
xR=303;
}
}
// 緑色スライダーの領域
if( (y>=170) && (y<=196)) {
xG=x;
if (xG<=38) {
xG=38;
}
if (xG>=303){
xG=303;
}
}
// 青色スライダーの領域
if( (y>=210) && (y<=236)) {
xB=x;
if (xB<=38) {
xB=38;
}
if (xB>=303){
xB=303;
}
}
}
// X軸の値を38→0、310→255にマッピング(LED点灯には0〜255の値が必要)
int xRC = map(xR,38,310,0,255);
int xGC = map(xG,38,310,0,255);
int xBC = map(xB,38,310,0,255);
// LEDのピンにPWM信号を送信
analogWrite(redLed, xRC);
analogWrite(greenLed, xGC);
analogWrite(blueLed, xBC);
// 最新の色の組み合わせで長方形を描画
myGLCD.setColor(xRC, xGC, xBC);
myGLCD.fillRoundRect(175, 87, 310, 119);
// 位置指示子を描画
myGLCD.setColor(255, 255, 255);
myGLCD.fillRect(xR,139,(xR+4),147); // 位置指示子
myGLCD.setColor(xRC, 0, 0);
myGLCD.fillRect(31, 139, (xR-1), 147);
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect((xR+5), 139, 309, 147);
myGLCD.setColor(255, 255, 255);
myGLCD.fillRect(xG,179,(xG+4),187);
myGLCD.setColor(0, xGC, 0);
myGLCD.fillRect(31, 179, (xG-1), 187);
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect((xG+5), 179, 309, 187);
myGLCD.setColor(255, 255, 255);
myGLCD.fillRect(xB,219,(xB+4),227);
myGLCD.setColor(0, 0, xBC);
myGLCD.fillRect(31, 219, (xB-1), 227);
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect((xB+5), 219, 309, 227);
}コード言語: Arduino (arduino)プログラムの完全なソースコード
Arduino TFTチュートリアル ファイル
1 file(s) 6.07 KB Download
/* Arduino TFT Tutorial
* Program made by Dejan Nedelkovski,
* www.HowToMechatronics.com */
/* This program uses the UTFT and URTouch libraries
* made by Henning Karlsen.
* You can find and download them at:
* www.RinkyDinkElectronics.com */
#include <UTFT.h>
#include <URTouch.h>
//==== Creating Objects
UTFT myGLCD(SSD1289,38,39,40,41); //Parameters should be adjusted to your Display/Schield model
URTouch myTouch( 6, 5, 4, 3, 2);
//==== Defining Variables
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];
extern unsigned int bird01[0x41A];
int x, y;
char currentPage, selectedUnit;
//Ultrasonic Sensor
const int VCC = 13;
const int trigPin = 11;
const int echoPin = 12;
long duration;
int distanceInch, distanceCm;
// RGB LEDs
const int redLed = 10;
const int greenLed = 9;
const int blueLed = 8;
int xR=38;
int xG=38;
int xB=38;
// Floppy Bird
int xP = 319;
int yP = 100;
int yB = 30;
int fallRateInt = 0;
float fallRate =0;
int score=0;
const int button = 14;
int buttonState = 0;
void setup() {
// Initial setup
myGLCD.InitLCD();
myGLCD.clrScr();
myTouch.InitTouch();
myTouch.setPrecision(PREC_MEDIUM);
// Defining Pin Modes
pinMode(VCC, OUTPUT); // VCC
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(button, INPUT);
digitalWrite(VCC, HIGH); // +5V - Pin 13 as VCC
drawHomeScreen(); // Draws the Home Screen
currentPage = '0'; // Indicates that we are at Home Screen
selectedUnit = '0'; // Indicates the selected unit for the first example, cms or inches
}
void loop() {
// Home Screen
if (currentPage == '0') {
if (myTouch.dataAvailable()) {
myTouch.read();
x=myTouch.getX(); // X coordinate where the screen has been pressed
y=myTouch.getY(); // Y coordinates where the screen has been pressed
// If we press the Distance Sensor Button
if ((x>=35) && (x<=285) && (y>=90) && (y<=130)) {
drawFrame(35, 90, 285, 130); // Custom Function -Highlighs the buttons when it's pressed
currentPage = '1'; // Indicates that we are the first example
myGLCD.clrScr(); // Clears the screen
drawDistanceSensor(); // It is called only once, because in the next iteration of the loop, this above if statement will be false so this funtion won't be called. This function will draw the graphics of the first example.
}
// If we press the RGB LED Control Button
if ((x>=35) && (x<=285) && (y>=140) && (y<=180)) {
drawFrame(35, 140, 285, 180);
currentPage = '2';
myGLCD.clrScr();
drawLedControl();
}
// If we press the Birduino Game Button
if ((x>=35) && (x<=285) && (y>=190) && (y<=230)) {
drawFrame(35, 190, 285, 230);
currentPage = '3';
myGLCD.clrScr();
myGLCD.setColor(114, 198, 206);
myGLCD.fillRect(0,0,319,239);
drawGround();
drawPilars(xP,yP);
drawBird(30);
delay(1000);
}
}
}
// Distance Sensor Example
if (currentPage == '1') {
getDistance(); // Gets distance from the sensor and this function is repeatedly called while we are at the first example in order to print the lasest results from the distance sensor
if (myTouch.dataAvailable()) {
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
// If we press the Centimeters Button
if ((x>=10) && (x<=135) &&(y>=90) && (y<=163)) {
selectedUnit = '0';
}
// If we press the Inches Button
if ((x>=10) && (x<=135) &&(y>=173) && (y<=201)) {
selectedUnit = '1';
}
// If we press the Back Button
if ((x>=10) && (x<=60) &&(y>=10) && (y<=36)) {
drawFrame(10, 10, 60, 36);
currentPage = '0'; // Indicates we are at home screen
myGLCD.clrScr();
drawHomeScreen(); // Draws the home screen
}
}
}
// RGB LED Control
if (currentPage == '2') {
setLedColor();
if (myTouch.dataAvailable()) {
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
//Back button
if ((x>=10) && (x<=60) &&(y>=10) && (y<=36)) {
drawFrame(10, 10, 60, 36);
currentPage = '0';
myGLCD.clrScr();
drawHomeScreen();
// Turns the LED off
analogWrite(redLed, 0);
analogWrite(greenLed, 0);
analogWrite(blueLed, 0);
}
}
}
//==== This section of the code, for the game example, is explained in my next tutorial
// Birduino Game
if (currentPage == '3') {
//delay(1);
xP=xP-3;
drawPilars(xP, yP);
yB+=fallRateInt;
fallRate=fallRate+0.4;
fallRateInt= int(fallRate);
if (yB>=220) {
yB=220;
}
if(yB>=180 || yB<=0){
restartGame();
}
if((xP<=85) && (xP>=30) && (yB<=yP-2)){
restartGame();
}
if((xP<=85) && (xP>=30) && (yB>=yP+60)){
restartGame();
}
drawBird(yB);
if (xP<=-51){
xP=319;
yP = rand() % 100+20;
score++;
}
if (myTouch.dataAvailable()) {
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
if ((x>=0) && (x<=319) &&(y>=50) && (y<=239)) {
fallRate=-5;
}
}
buttonState = digitalRead(button);
if (buttonState == HIGH) {
fallRate=-5;
}
}
if (myTouch.dataAvailable()) {
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
if ((x>=10) && (x<=60) &&(y>=10) && (y<=36)) {
drawFrame(10, 10, 60, 36);
currentPage = '0';
myGLCD.clrScr();
drawHomeScreen();
analogWrite(redLed, 0);
analogWrite(greenLed, 0);
analogWrite(blueLed, 0);
}
}
}
// ====== Custom Funtions ======
// drawHomeScreen - Custom Function
void drawHomeScreen() {
// Title
myGLCD.setBackColor(0,0,0); // Sets the background color of the area where the text will be printed to black
myGLCD.setColor(255, 255, 255); // Sets color to white
myGLCD.setFont(BigFont); // Sets font to big
myGLCD.print("Arduino TFT Tutorial", CENTER, 10); // Prints the string on the screen
myGLCD.setColor(255, 0, 0); // Sets color to red
myGLCD.drawLine(0,32,319,32); // Draws the red line
myGLCD.setColor(255, 255, 255); // Sets color to white
myGLCD.setFont(SmallFont); // Sets the font to small
myGLCD.print("by HowToMechatronics.com", CENTER, 41); // Prints the string
myGLCD.setFont(BigFont);
myGLCD.print("Select Example", CENTER, 64);
// Button - Distance Sensor
myGLCD.setColor(16, 167, 103); // Sets green color
myGLCD.fillRoundRect (35, 90, 285, 130); // Draws filled rounded rectangle
myGLCD.setColor(255, 255, 255); // Sets color to white
myGLCD.drawRoundRect (35, 90, 285, 130); // Draws rounded rectangle without a fill, so the overall appearance of the button looks like it has a frame
myGLCD.setFont(BigFont); // Sets the font to big
myGLCD.setBackColor(16, 167, 103); // Sets the background color of the area where the text will be printed to green, same as the button
myGLCD.print("DISTANCE SENSOR", CENTER, 102); // Prints the string
// Button - RGB LED Control
myGLCD.setColor(16, 167, 103);
myGLCD.fillRoundRect (35, 140, 285, 180);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (35, 140, 285, 180);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(16, 167, 103);
myGLCD.print("RGB LED CONTROL", CENTER, 152);
// Button - Birduino
myGLCD.setColor(16, 167, 103);
myGLCD.fillRoundRect (35, 190, 285, 230);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (35, 190, 285, 230);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(16, 167, 103);
myGLCD.print("BIRDUINO GAME", CENTER, 202);
}
// Highlights the button when pressed
void drawFrame(int x1, int y1, int x2, int y2) {
myGLCD.setColor(255, 0, 0);
myGLCD.drawRoundRect (x1, y1, x2, y2);
while (myTouch.dataAvailable())
myTouch.read();
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (x1, y1, x2, y2);
}
//====================================================
void drawDistanceSensor() {
myGLCD.setColor(100, 155, 203);
myGLCD.fillRoundRect (10, 10, 60, 36);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10, 10, 60, 36);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(100, 155, 203);
myGLCD.print("<-", 18, 15);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setFont(SmallFont);
myGLCD.print("Back to Main Menu", 70, 18);
myGLCD.setFont(BigFont);
myGLCD.print("Ultrasonic Sensor", CENTER, 50);
myGLCD.print("HC-SR04", CENTER, 76);
myGLCD.setColor(255, 0, 0);
myGLCD.drawLine(0,100,319,100);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setColor(255, 255, 255);
myGLCD.setFont(SmallFont);
myGLCD.print("Select Unit", 10, 114);
myGLCD.setFont(BigFont);
myGLCD.print("Distance:", 130, 120);
myGLCD.setColor(223, 77, 55);
myGLCD.fillRoundRect (10, 135, 90, 163);
myGLCD.setColor(225, 255, 255);
myGLCD.drawRoundRect (10, 135, 90, 163);
myGLCD.setBackColor(223, 77, 55);
myGLCD.setColor(255, 255, 255);
myGLCD.print("cm", 33, 140);
myGLCD.setColor(223, 77, 55);
myGLCD.fillRoundRect (10, 173, 90, 201);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10, 173, 90, 201);
myGLCD.setBackColor(223, 77, 55);
myGLCD.setColor(255, 255, 255);
myGLCD.print("inch", 17, 180);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setFont(SmallFont);
myGLCD.print("Source code at: HowToMechatronics.com", CENTER, 220);
}
//====================================================
//===== getDistance - Custom Function
void getDistance() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distanceCm= duration*0.034/2;
distanceInch= distanceCm/2.53;
// Prints the distance in centimeters
if (selectedUnit == '0' && distanceCm <=400) {
myGLCD.setFont(SevenSegNumFont);
myGLCD.setColor(0, 255, 0);
myGLCD.setBackColor(0, 0, 0);
myGLCD.printNumI(distanceCm,130, 145, 3,'0');
myGLCD.setFont(BigFont);
myGLCD.print("cm ", 235, 178);
}
// Prints the distance in inches
if (selectedUnit == '1' && distanceCm <=160) {
myGLCD.setFont(SevenSegNumFont);
myGLCD.setColor(0, 255, 0);
myGLCD.setBackColor(0, 0, 0);
myGLCD.printNumI(distanceInch,130, 145, 3,'0');
myGLCD.setFont(BigFont);
myGLCD.print("inch", 235, 178);
}
delay(10);
}
//====================================================
void drawLedControl() {
myGLCD.setColor(100, 155, 203);
myGLCD.fillRoundRect (10, 10, 60, 36);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10, 10, 60, 36);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(100, 155, 203);
myGLCD.print("<-", 18, 15);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setFont(SmallFont);
myGLCD.print("Back to Main Menu", 70, 18);
myGLCD.setFont(BigFont);
myGLCD.print("RGB LED Control", CENTER, 50);
myGLCD.print("LED Color:", 10, 95);
myGLCD.print("R", 10, 135);
myGLCD.print("G", 10, 175);
myGLCD.print("B", 10, 215);
myGLCD.setColor(255, 0, 0);
myGLCD.drawLine(0,75,319,75);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRect(30, 138, 310, 148); // R - Slider
myGLCD.drawRect(30, 178, 310, 188);
myGLCD.drawRect(30, 218, 310, 228);
}
//====================================================
//============= setLedColor() - Custom Funtion
void setLedColor() {
if (myTouch.dataAvailable()) {
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
// Area of the Red color slider
if( (y>=130) && (y<=156)) {
xR=x; // Stores the X value where the screen has been pressed in to variable xR
if (xR<=38) { // Confines the area of the slider to be above 38 pixels
xR=38;
}
if (xR>=303){ /// Confines the area of the slider to be under 310 pixels
xR=303;
}
}
// Area of the Green color slider
if( (y>=170) && (y<=196)) {
xG=x;
if (xG<=38) {
xG=38;
}
if (xG>=303){
xG=303;
}
}
// Area of the Blue color slider
if( (y>=210) && (y<=236)) {
xB=x;
if (xB<=38) {
xB=38;
}
if (xB>=303){
xB=303;
}
}
}
// Maps the values of the X - Axis from 38 to 0 and 310 to 255, because we need values from 0 to 255 for turning on the led
int xRC = map(xR,38,310,0,255);
int xGC = map(xG,38,310,0,255);
int xBC = map(xB,38,310,0,255);
// Sends PWM signal to the pins of the led
analogWrite(redLed, xRC);
analogWrite(greenLed, xGC);
analogWrite(blueLed, xBC);
// Draws a rectangle with the latest color combination
myGLCD.setColor(xRC, xGC, xBC);
myGLCD.fillRoundRect(175, 87, 310, 119);
// Draws the positioners
myGLCD.setColor(255, 255, 255);
myGLCD.fillRect(xR,139,(xR+4),147); // Positioner
myGLCD.setColor(xRC, 0, 0);
myGLCD.fillRect(31, 139, (xR-1), 147);
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect((xR+5), 139, 309, 147);
myGLCD.setColor(255, 255, 255);
myGLCD.fillRect(xG,179,(xG+4),187);
myGLCD.setColor(0, xGC, 0);
myGLCD.fillRect(31, 179, (xG-1), 187);
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect((xG+5), 179, 309, 187);
myGLCD.setColor(255, 255, 255);
myGLCD.fillRect(xB,219,(xB+4),227);
myGLCD.setColor(0, 0, xBC);
myGLCD.fillRect(31, 219, (xB-1), 227);
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect((xB+5), 219, 309, 227);
}
//====================================================
void drawGround() {
myGLCD.setColor(221,216,148);
myGLCD.fillRect(0, 215, 319, 239);
myGLCD.setColor(47,175,68);
myGLCD.fillRect(0, 205, 319, 214);
myGLCD.setColor(0, 0, 0);
myGLCD.setBackColor(221, 216, 148);
myGLCD.setFont(BigFont);
myGLCD.print("Score:",5,220);
myGLCD.setFont(SmallFont);
myGLCD.print("HowToMechatronics.com", 140, 220);
}
void drawPilars(int x, int y) {
if (x>=270){
myGLCD.setColor(0, 200, 20);
myGLCD.fillRect(318, 0, x, y-1);
myGLCD.setColor(0, 0, 0);
myGLCD.drawRect(319, 0, x-1, y);
myGLCD.setColor(0, 200, 20);
myGLCD.fillRect(318, y+81, x, 203);
myGLCD.setColor(0, 0, 0);
myGLCD.drawRect(319, y+80, x-1, 204);
} else if( x<=268) {
myGLCD.setColor(114, 198, 206);
myGLCD.fillRect(x+51, 0, x+53, y);
myGLCD.setColor(0, 200, 20);
myGLCD.fillRect(x+49, 1, x+1, y-1);
myGLCD.setColor(0, 0, 0);
myGLCD.drawRect(x+50, 0, x, y);
myGLCD.setColor(114, 198, 206);
myGLCD.fillRect(x-1, 0, x-3, y);
myGLCD.setColor(114, 198, 206);
myGLCD.fillRect(x+51, y+80, x+53, 204);
myGLCD.setColor(0, 200, 20);
myGLCD.fillRect(x+49, y+81, x+1, 203);
myGLCD.setColor(0, 0, 0);
myGLCD.drawRect(x+50, y+80, x, 204);
myGLCD.setColor(114, 198, 206);
myGLCD.fillRect(x-1, y+80, x-3, 204);
}
myGLCD.setColor(0, 0, 0);
myGLCD.setBackColor(221, 216, 148);
myGLCD.setFont(BigFont);
myGLCD.printNumI(score, 100, 220);
}
//====================================================
void drawBird(int y) {
if(y<=219) {
myGLCD.drawBitmap (50, y, 35, 30, bird01);
myGLCD.setColor(114, 198, 206);
myGLCD.fillRoundRect(50,y,85,y-6);
myGLCD.fillRoundRect(50,y+30,85,y+36);
} else if(y>=200) {
myGLCD.drawBitmap (50, 200, 35, 30, bird01);
myGLCD.setColor(114, 198, 206);
myGLCD.fillRoundRect(50,200,85,200-6);
myGLCD.fillRoundRect(50,200+30,85,200+36);
}
}
void gameOver() {
myGLCD.clrScr();
myGLCD.setColor(255, 255, 255);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setFont(BigFont);
myGLCD.print("GAME OVER", CENTER, 40);
myGLCD.print("Score:", 100, 80);
myGLCD.printNumI(score,200, 80);
myGLCD.print("Restarting...", CENTER, 120);
myGLCD.setFont(SevenSegNumFont);
myGLCD.printNumI(2,CENTER, 150);
delay(1000);
myGLCD.printNumI(1,CENTER, 150);
delay(1000);
myGLCD.setColor(114, 198, 206);
myGLCD.fillRect(0,0,319,239);
drawBird(30);
drawGround();
delay(1000);
}
//====================================================
void restartGame() {
delay(1000);
gameOver();
xP=319;
yB=30;
fallRate=0;
score=0;
}
コード言語: Arduino (arduino)
製造プロセス
- Arduinoで作るセキュアなRFIDロック – ステップバイステップガイド
- Arduinoで携帯型静電容量タッチピアノを自作:MPR121シールド活用・ステップバイステップ
- Arduino UNOで作るLCDアニメーション&ゲーム入門
- Arduino UNOで1.17インチTFT LCDに画像を表示する方法
- Arduino 2.4インチTFT LCDシールドでSDカードからBMP画像を表示する
- Arduino用2.4インチTFT LCDシールドのタッチスクリーン問題を解決
- DHT センサで作る信頼性の高い Arduino 気象ステーション
- Arduino Nanoと指紋センサーで作るバイオメトリクス認証システム – 完全チュートリアル
- Arduino UNOで作るミニピアノ - ステップバイステップチュートリアル
- Arduino LCDプロジェクト: 16x2ディスプレイとプッシュボタンで作るサッカーゲーム
- Arduino初心者チュートリアル01: 最初のプロジェクトを始めよう