Play pong on a little LCD screen with two big joysticks.
// Pins SCLK and MOSI are fixed in hardware, and pin 10 (or 53) // must be an output//#define sclk 13 // for MEGAs use pin 52//#define mosi 11 // for MEGAs use pin 51#define cs 10 // for MEGAs you probably want this to be pin 53#define dc 9#define rst 8 // you can also connect this to the Arduino reset// Color definitions#defineBLACK 0x0000#defineBLUE 0x001F#defineRED 0xF800#defineGREEN 0x07E0#define CYAN 0x07FF#define MAGENTA 0xF81F#define YELLOW 0xFFE0 #define WHITE 0xFFFF#include#include // Option 1: use any pins but a little slower//ST7735 tft = ST7735(cs, dc, mosi, sclk, rst); // Option 2: must use the hardware SPI pins // (for UNO thats sclk = 13 and sid = 11) and pin 10 must be // an output. This is much faster - also required if you want// to use the microSD card (see the image drawing example)ST7735 tft = ST7735(cs, dc, rst);float p = 3.141592; // value for piint tft_width = 128;int tft_height = 160;int x1 = 60;int y1 = 60;int x2 = 20;int y2 = 80;int ballx = 60;int bally = 60;int deltay = 1;int deltax = 1;int ball_speed = 24;int left_score = 4;int right_score = 4;int background_color = 0x00;void setup(void) { tft.initR(); // initialize a ST7735R chip tft.writecommand(ST7735_DISPON); tft.fillScreen(background_color); delay(700); for(int i = 0; i < 9; i++){ pinMode(i,OUTPUT); } for(int i = 0; i < 9; i++){ digitalWrite(i,HIGH); } delay(500); }void loop() { for(int i = 0; i < left_score; i++){ digitalWrite(i,HIGH); } for(int i = 0; i < right_score; i++){ digitalWrite(i+4,HIGH); } int cur_r = random(20,32); int cur_g = random(55,64); int cur_b = random(10,32); int color = cur_r | (cur_g << 5) | (cur_b << 11); int radius = 4; draw_paddles(); draw_ball(color); delay(ball_speed);}void draw_input_text(){ int cur_x2 = analogRead(2); int cur_y2 = analogRead(3); String xs = String(cur_x2); String ys = String(cur_y2); char cx[4]; cx[0] = xs.charAt(0); cx[1] = xs.charAt(1); cx[2] = xs.charAt(2); cx[3] =0; char cy[4]; cy[0] = ys.charAt(0); cy[1] = ys.charAt(1); cy[2] = ys.charAt(2); cy[3] =0; tft.fillRect(10,20,40,40,background_color); tft.drawString(12,22,cx ,YELLOW); tft.drawString(12,40,cy ,YELLOW);}void draw_paddles(){ int cur_x = analogRead(0); int cur_y = analogRead(1); int hval = 620; int lval = 350; if ( y1 < tft_height - 20 && cur_y > hval) y1++; else if ( y1 > 0 && cur_y < lval ) y1--; int cur_x2 = analogRead(2); int cur_y2 = analogRead(3); if ( y2 < tft_height - 20 && cur_y2 > hval ) y2++; else if (y2 > 0 && cur_y2 < lval ) y2--; tft.fillRect(2, y1-3, 5, 27, background_color); tft.fillRect(3, y1, 3, 20, YELLOW); tft.fillRect(tft_width-3, y2-3, 5, 27,background_color); tft.fillRect(tft_width-3, y2, 3, 20, YELLOW);}void draw_ball(int color){ int bradius = 6; tft.fillCircle(ballx, bally, bradius , color); ballx += deltax; bally += deltay; if ( ballx < 7 && bally > y1 && bally < y1+20) deltax *= -1; else if ( ballx > tft_width-7 && bally > y2 && bally < y2+20) deltax *= -1; else if ( bally <= bradius || bally >= tft_height - bradius ) deltay *= -1; if (ballx <= 0 ) { left_score--; tft.drawString(2,42, "Right Won!" ,~background_color,2); tft.drawString(2,62, "Get Ready!" ,CYAN,2); for (int i = 0; i <250; i++){ delay(10); draw_paddles(); } int cur_r = random(0,26); int cur_g = random(0,30); int cur_b = random(0,18); background_color = cur_r | (cur_g << 5) | (cur_b << 11); tft.fillScreen(background_color); ballx = 64; bally = 80; if (ball_speed > 1) ball_speed--; digitalWrite(left_score,LOW); } else if (ballx >= tft_width ) { right_score --; tft.drawString(2,42, "Left Won!" ,YELLOW,2); tft.drawString(2,62, "Get Ready!" ,GREEN,2); for (int i = 0; i <250; i++){ delay(10); draw_paddles(); } int cur_r = random(0,26); int cur_g = random(0,30); int cur_b = random(0,18); background_color = cur_r | (cur_g << 5) | (cur_b << 11); tft.fillScreen(background_color); ballx = 64; bally = 80; if (ball_speed > 1) ball_speed--; digitalWrite(4+right_score,LOW); }}
| Title | Description | # | Cost | Link | Picture |
| LCD and joystick PCB | This PCB holds two joysticks and an LCD display. | 1 | $10.0 | Link |
|
| TFT LCD Screen | 1.8 inch 160 x 128 pixels LCD screen. ST7735R driver. JDT-1800. | 1 | $9.95 | Link |
|
| Joystick | Thumb joystick with push button. | 2 | $3.95 | Link |
|
| ATMEGA328P-AU | Integrated Circuits (ICs) MCU AVR 32K FLASH 32TQFPValue:1.8 V ~ 5.5 V 20MHz | 1 | $3.05 | Link |
|
| Crystal | CRYSTAL 16.00000 MHZ 20PF SMDValue:16MHz | 1 | $0.53 | Link |
|
| Voltage Regulator 3v3 SMD | IC REG LDO 3.3V .15A SOT23 Up to 6V input output 3.3V 150mA (Max)Value:3.3V | 1 | $0.46 | Link |
|
| CD4050 | IC BUFF/CONVERTER HEX 16SOICN 8mA, 48mAValue: 3 V ~ 18 V | 1 | $0.45 | Link |
|
Comments: