this is a game very similar to pong, use touch sensor to hit the ball on the LCD back and forth

Dependencies:   SLCD TSI mbed

Committer:
MattShilling
Date:
Wed Sep 28 07:00:13 2016 +0000
Revision:
0:cb5f98f06882
this is a game, very similar to pong;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MattShilling 0:cb5f98f06882 1 #include "mbed.h"
MattShilling 0:cb5f98f06882 2 #include "TSISensor.h"
MattShilling 0:cb5f98f06882 3 #include "SLCD.h"
MattShilling 0:cb5f98f06882 4
MattShilling 0:cb5f98f06882 5 Serial pc(USBTX, USBRX);
MattShilling 0:cb5f98f06882 6 SLCD lcd;
MattShilling 0:cb5f98f06882 7 TSISensor tsi; // touch sensor
MattShilling 0:cb5f98f06882 8
MattShilling 0:cb5f98f06882 9 bool send = true;
MattShilling 0:cb5f98f06882 10 bool hit_right = false;
MattShilling 0:cb5f98f06882 11 float speed = 0.2;
MattShilling 0:cb5f98f06882 12
MattShilling 0:cb5f98f06882 13 void send_ball_left(){
MattShilling 0:cb5f98f06882 14 lcd.clear();
MattShilling 0:cb5f98f06882 15 lcd.CharPosition=2;
MattShilling 0:cb5f98f06882 16 lcd.putc('o');
MattShilling 0:cb5f98f06882 17 wait(speed);
MattShilling 0:cb5f98f06882 18
MattShilling 0:cb5f98f06882 19 lcd.clear();
MattShilling 0:cb5f98f06882 20 lcd.CharPosition=1;
MattShilling 0:cb5f98f06882 21 lcd.putc('o');
MattShilling 0:cb5f98f06882 22 wait(speed);
MattShilling 0:cb5f98f06882 23
MattShilling 0:cb5f98f06882 24 lcd.clear();
MattShilling 0:cb5f98f06882 25
MattShilling 0:cb5f98f06882 26 }
MattShilling 0:cb5f98f06882 27
MattShilling 0:cb5f98f06882 28 void send_ball_right(){
MattShilling 0:cb5f98f06882 29 lcd.clear();
MattShilling 0:cb5f98f06882 30 lcd.CharPosition=0;
MattShilling 0:cb5f98f06882 31 lcd.putc('o');
MattShilling 0:cb5f98f06882 32 wait(speed);
MattShilling 0:cb5f98f06882 33
MattShilling 0:cb5f98f06882 34 lcd.clear();
MattShilling 0:cb5f98f06882 35 lcd.CharPosition=1;
MattShilling 0:cb5f98f06882 36 lcd.putc('o');
MattShilling 0:cb5f98f06882 37 wait(speed);
MattShilling 0:cb5f98f06882 38
MattShilling 0:cb5f98f06882 39 lcd.clear();
MattShilling 0:cb5f98f06882 40 lcd.CharPosition=2;
MattShilling 0:cb5f98f06882 41 lcd.putc('o');
MattShilling 0:cb5f98f06882 42 wait(speed);
MattShilling 0:cb5f98f06882 43 }
MattShilling 0:cb5f98f06882 44
MattShilling 0:cb5f98f06882 45 void send_ball_back(){
MattShilling 0:cb5f98f06882 46
MattShilling 0:cb5f98f06882 47 lcd.clear();
MattShilling 0:cb5f98f06882 48 lcd.CharPosition=1;
MattShilling 0:cb5f98f06882 49 lcd.putc('o');
MattShilling 0:cb5f98f06882 50 wait(speed);
MattShilling 0:cb5f98f06882 51
MattShilling 0:cb5f98f06882 52 lcd.clear();
MattShilling 0:cb5f98f06882 53 lcd.CharPosition=2;
MattShilling 0:cb5f98f06882 54 lcd.putc('o');
MattShilling 0:cb5f98f06882 55 wait(speed);
MattShilling 0:cb5f98f06882 56 }
MattShilling 0:cb5f98f06882 57
MattShilling 0:cb5f98f06882 58 void check_swipe_left(){
MattShilling 0:cb5f98f06882 59 lcd.clear();
MattShilling 0:cb5f98f06882 60 lcd.CharPosition=3;
MattShilling 0:cb5f98f06882 61 lcd.putc('o');
MattShilling 0:cb5f98f06882 62 wait(speed);
MattShilling 0:cb5f98f06882 63
MattShilling 0:cb5f98f06882 64 if (tsi.readPercentage() > .75){
MattShilling 0:cb5f98f06882 65 send_ball_left();
MattShilling 0:cb5f98f06882 66 send = false;
MattShilling 0:cb5f98f06882 67 hit_right = true;
MattShilling 0:cb5f98f06882 68 }
MattShilling 0:cb5f98f06882 69 else {
MattShilling 0:cb5f98f06882 70 lcd.clear();
MattShilling 0:cb5f98f06882 71 lcd.CharPosition=1;
MattShilling 0:cb5f98f06882 72 lcd.printf("no");
MattShilling 0:cb5f98f06882 73 wait(.7);
MattShilling 0:cb5f98f06882 74 send = true;
MattShilling 0:cb5f98f06882 75 hit_right = false;
MattShilling 0:cb5f98f06882 76 speed = speed + 0.01;
MattShilling 0:cb5f98f06882 77 }
MattShilling 0:cb5f98f06882 78 }
MattShilling 0:cb5f98f06882 79
MattShilling 0:cb5f98f06882 80 void check_swipe_right(){
MattShilling 0:cb5f98f06882 81 lcd.clear();
MattShilling 0:cb5f98f06882 82 lcd.CharPosition=0;
MattShilling 0:cb5f98f06882 83 lcd.putc('o');
MattShilling 0:cb5f98f06882 84 wait(speed);
MattShilling 0:cb5f98f06882 85 if (tsi.readPercentage() > .1 && tsi.readPercentage() < .5){
MattShilling 0:cb5f98f06882 86 send_ball_back();
MattShilling 0:cb5f98f06882 87 send = false;
MattShilling 0:cb5f98f06882 88 speed = speed - 0.01;
MattShilling 0:cb5f98f06882 89 }
MattShilling 0:cb5f98f06882 90 else {
MattShilling 0:cb5f98f06882 91 lcd.clear();
MattShilling 0:cb5f98f06882 92 lcd.printf("no");
MattShilling 0:cb5f98f06882 93 wait(.7);
MattShilling 0:cb5f98f06882 94 send = true;
MattShilling 0:cb5f98f06882 95 speed = speed + 0.01;
MattShilling 0:cb5f98f06882 96 }
MattShilling 0:cb5f98f06882 97 }
MattShilling 0:cb5f98f06882 98
MattShilling 0:cb5f98f06882 99
MattShilling 0:cb5f98f06882 100
MattShilling 0:cb5f98f06882 101
MattShilling 0:cb5f98f06882 102 int main(void) {
MattShilling 0:cb5f98f06882 103
MattShilling 0:cb5f98f06882 104
MattShilling 0:cb5f98f06882 105 // initiate LCD
MattShilling 0:cb5f98f06882 106 lcd.Home();
MattShilling 0:cb5f98f06882 107
MattShilling 0:cb5f98f06882 108 while (true) {
MattShilling 0:cb5f98f06882 109 if (send) {send_ball_right();}
MattShilling 0:cb5f98f06882 110 check_swipe_left();
MattShilling 0:cb5f98f06882 111 if(hit_right){check_swipe_right();}
MattShilling 0:cb5f98f06882 112 }
MattShilling 0:cb5f98f06882 113 }