Arduino TFT
Arduino TFT
h>
#include <UTouch.h>
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;
void setup() {
// Initial setup
myGLCD.InitLCD();
myGLCD.clrScr();
myTouch.InitTouch();
myTouch.setPrecision(PREC_MEDIUM);
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
}
}
}
//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);
}
}
}
// 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);
}
}
// 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);
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.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;
}