Dependencies:   mbed TextLCD

Committer:
vishiswoz
Date:
Tue Apr 30 03:49:07 2019 +0000
Revision:
2:20aa5ae37a46
Parent:
1:b3e26728357c
updated code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
4180_1 0:e0bef6b1aa57 1 #include "mbed.h"
vishiswoz 1:b3e26728357c 2 #include "TextLCD.h"
vishiswoz 1:b3e26728357c 3 #include "SongPlayer.h"
vishiswoz 1:b3e26728357c 4 #include "Speaker.h"
vishiswoz 1:b3e26728357c 5
vishiswoz 1:b3e26728357c 6 Serial leftSensor(p9, p10); // tx, rx
vishiswoz 1:b3e26728357c 7 Serial rightSensor(p13, p14); // tx, rx
vishiswoz 1:b3e26728357c 8 Serial gameOverSensor(p28, p27); // tx, rx
4180_1 0:e0bef6b1aa57 9 PwmOut IRLED(p21);
vishiswoz 1:b3e26728357c 10 DigitalIn leftButton(p7);
vishiswoz 1:b3e26728357c 11 DigitalIn middleButton(p8);
vishiswoz 1:b3e26728357c 12 DigitalIn rightButton(p11);
vishiswoz 1:b3e26728357c 13 Timer t;
vishiswoz 1:b3e26728357c 14 bool check = false;
vishiswoz 1:b3e26728357c 15 TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2);
vishiswoz 1:b3e26728357c 16 DigitalOut leftLED(p5);
vishiswoz 1:b3e26728357c 17 DigitalOut rightLED(p6);
vishiswoz 1:b3e26728357c 18 Serial pc(USBTX, USBRX);
vishiswoz 1:b3e26728357c 19 int score = 0;
vishiswoz 1:b3e26728357c 20 bool leftLEDOn = false;
vishiswoz 1:b3e26728357c 21 bool rightLEDOn = false;
4180_1 0:e0bef6b1aa57 22
vishiswoz 1:b3e26728357c 23 float note[1]= {1568.0};
vishiswoz 1:b3e26728357c 24 float duration[1]= {0.25};
vishiswoz 1:b3e26728357c 25
vishiswoz 1:b3e26728357c 26 Speaker mySpeaker(p25);
vishiswoz 1:b3e26728357c 27
4180_1 0:e0bef6b1aa57 28 int main() {
4180_1 0:e0bef6b1aa57 29 IRLED.period(1.0/38000.0);
4180_1 0:e0bef6b1aa57 30 IRLED = 0.5;
vishiswoz 1:b3e26728357c 31 leftSensor.baud(2400);
vishiswoz 1:b3e26728357c 32 rightSensor.baud(2400);
vishiswoz 1:b3e26728357c 33 gameOverSensor.baud(2400);
vishiswoz 1:b3e26728357c 34 t.start();
vishiswoz 1:b3e26728357c 35 lcd.printf("MBED PINBALL\nSCORE:%010d", score);
vishiswoz 1:b3e26728357c 36
vishiswoz 1:b3e26728357c 37 //put switches in pullup mode
vishiswoz 1:b3e26728357c 38 leftButton.mode(PullUp);
vishiswoz 1:b3e26728357c 39 middleButton.mode(PullUp);
vishiswoz 1:b3e26728357c 40 rightButton.mode(PullUp);
vishiswoz 1:b3e26728357c 41
vishiswoz 1:b3e26728357c 42 wait(0.05);
vishiswoz 1:b3e26728357c 43 leftLED = 0;
vishiswoz 1:b3e26728357c 44 rightLED = 0;
vishiswoz 1:b3e26728357c 45
4180_1 0:e0bef6b1aa57 46 while(1) {
vishiswoz 1:b3e26728357c 47 //score += 1;
vishiswoz 1:b3e26728357c 48 leftSensor.putc(0);
vishiswoz 1:b3e26728357c 49 rightSensor.putc(0);
vishiswoz 1:b3e26728357c 50 gameOverSensor.putc(0);
vishiswoz 1:b3e26728357c 51
vishiswoz 1:b3e26728357c 52 //handle switch being pressed (in any of the 5 directions)
vishiswoz 1:b3e26728357c 53 if (leftButton == 0)
vishiswoz 1:b3e26728357c 54 {
vishiswoz 1:b3e26728357c 55 //add points here?
vishiswoz 2:20aa5ae37a46 56 score += 2000;
vishiswoz 1:b3e26728357c 57 }
vishiswoz 1:b3e26728357c 58
vishiswoz 1:b3e26728357c 59 if (middleButton == 0)
vishiswoz 1:b3e26728357c 60 {
vishiswoz 1:b3e26728357c 61 //add points here?
vishiswoz 2:20aa5ae37a46 62 score += 2000;
vishiswoz 1:b3e26728357c 63 }
vishiswoz 1:b3e26728357c 64
vishiswoz 1:b3e26728357c 65 if (rightButton == 0)
vishiswoz 1:b3e26728357c 66 {
vishiswoz 1:b3e26728357c 67 //add points here?
vishiswoz 2:20aa5ae37a46 68 score += 2000;
vishiswoz 1:b3e26728357c 69 }
vishiswoz 1:b3e26728357c 70
vishiswoz 1:b3e26728357c 71
vishiswoz 1:b3e26728357c 72 if (leftSensor.readable())
vishiswoz 1:b3e26728357c 73 {
vishiswoz 1:b3e26728357c 74 leftSensor.getc();
4180_1 0:e0bef6b1aa57 75 }
vishiswoz 1:b3e26728357c 76
vishiswoz 1:b3e26728357c 77 else if (!leftSensor.readable() && (t.read() > 0.5 || check)) {
vishiswoz 1:b3e26728357c 78 //pinball went through left side
vishiswoz 1:b3e26728357c 79 if (!leftLEDOn)
vishiswoz 1:b3e26728357c 80 {
vishiswoz 1:b3e26728357c 81 t.stop();
vishiswoz 1:b3e26728357c 82 check = true;
vishiswoz 1:b3e26728357c 83 leftLED = 1;
vishiswoz 1:b3e26728357c 84 leftLEDOn = true;
vishiswoz 1:b3e26728357c 85 if (rightLED == 1)
vishiswoz 1:b3e26728357c 86 {
vishiswoz 1:b3e26728357c 87 //both leds are on give points and turn off leds
vishiswoz 1:b3e26728357c 88 score += 1000;
vishiswoz 1:b3e26728357c 89 wait(1.0);
vishiswoz 1:b3e26728357c 90 leftLED = 0;
vishiswoz 1:b3e26728357c 91 rightLED = 0;
vishiswoz 1:b3e26728357c 92 leftLEDOn = false;
vishiswoz 1:b3e26728357c 93 rightLEDOn = false;
vishiswoz 1:b3e26728357c 94 t.reset();
vishiswoz 1:b3e26728357c 95 t.start();
vishiswoz 1:b3e26728357c 96 check = false;
vishiswoz 1:b3e26728357c 97 }
vishiswoz 1:b3e26728357c 98 }
vishiswoz 1:b3e26728357c 99 }
vishiswoz 1:b3e26728357c 100
vishiswoz 1:b3e26728357c 101 if (rightSensor.readable())
vishiswoz 1:b3e26728357c 102 {
vishiswoz 1:b3e26728357c 103 rightSensor.getc();
4180_1 0:e0bef6b1aa57 104 }
vishiswoz 1:b3e26728357c 105
vishiswoz 1:b3e26728357c 106 else if(!rightSensor.readable() && (t.read() > 0.5 || check)) {
vishiswoz 1:b3e26728357c 107 if (!rightLEDOn)
vishiswoz 1:b3e26728357c 108 {
vishiswoz 1:b3e26728357c 109 //pinball went through right side
vishiswoz 1:b3e26728357c 110 t.stop();
vishiswoz 1:b3e26728357c 111 check = true;
vishiswoz 1:b3e26728357c 112 rightLED = 1;
vishiswoz 1:b3e26728357c 113 rightLEDOn = true;
vishiswoz 1:b3e26728357c 114 if (leftLED == 1)
vishiswoz 1:b3e26728357c 115 {
vishiswoz 1:b3e26728357c 116 //both leds are on give points and turn off leds
vishiswoz 1:b3e26728357c 117 score += 1000;
vishiswoz 1:b3e26728357c 118 wait(1.0);
vishiswoz 1:b3e26728357c 119 leftLED = 0;
vishiswoz 1:b3e26728357c 120 rightLED = 0;
vishiswoz 1:b3e26728357c 121 leftLEDOn = false;
vishiswoz 1:b3e26728357c 122 rightLEDOn = false;
vishiswoz 1:b3e26728357c 123 t.reset();
vishiswoz 1:b3e26728357c 124 t.start();
vishiswoz 1:b3e26728357c 125 check = false;
vishiswoz 1:b3e26728357c 126 }
vishiswoz 1:b3e26728357c 127 }
vishiswoz 1:b3e26728357c 128 }
vishiswoz 1:b3e26728357c 129
vishiswoz 1:b3e26728357c 130 if (gameOverSensor.readable())
vishiswoz 1:b3e26728357c 131 {
vishiswoz 1:b3e26728357c 132 gameOverSensor.getc();
vishiswoz 1:b3e26728357c 133 }
vishiswoz 1:b3e26728357c 134
vishiswoz 1:b3e26728357c 135 else if(!gameOverSensor.readable() && (t.read() > 0.5 || check)) {
vishiswoz 1:b3e26728357c 136 //game over
vishiswoz 1:b3e26728357c 137 t.stop();
vishiswoz 1:b3e26728357c 138 check = true;
vishiswoz 1:b3e26728357c 139 lcd.cls();
vishiswoz 1:b3e26728357c 140 lcd.printf("GAME OVER\nSCORE:%010d", score);
vishiswoz 1:b3e26728357c 141 //play game over sound
vishiswoz 1:b3e26728357c 142 mySpeaker.PlayNote(277.1826, 0.25, 0.5);
vishiswoz 1:b3e26728357c 143 mySpeaker.PlayNote(261.6256, 0.5, 0.5);
vishiswoz 1:b3e26728357c 144 mySpeaker.PlayNote(174.6141, 1.0, 0.5);
vishiswoz 1:b3e26728357c 145 leftLED = 0;
vishiswoz 1:b3e26728357c 146 rightLED = 0;
vishiswoz 1:b3e26728357c 147 break;
vishiswoz 1:b3e26728357c 148 }
vishiswoz 1:b3e26728357c 149
vishiswoz 1:b3e26728357c 150 lcd.printf("MBED PINBALL\nSCORE:%010d", score);
4180_1 0:e0bef6b1aa57 151 }
vishiswoz 1:b3e26728357c 152 }