Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE PinDetect SDFileSystem mbed wave_player
Revision 4:0be9cd917c17, committed 2015-10-23
- Comitter:
- pkulkarni34
- Date:
- Fri Oct 23 02:56:50 2015 +0000
- Parent:
- 3:fb5a647123cb
- Commit message:
- Rock Paper Scissors - Player 2
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Thu Oct 22 01:15:48 2015 +0000
+++ b/main.cpp Fri Oct 23 02:56:50 2015 +0000
@@ -1,33 +1,28 @@
//ECE 4180 Mini Project
//Prachi Kulkarni and Kendra Dodson
+//This code is for Player 2
+
#include "mbed.h"
#include "uLCD_4DGL.h"
#include "SDFileSystem.h"
#include "wave_player.h"
-//#include "beep.h"
+//Leds to show status of sending and receiving messages
DigitalOut myled(LED1);
DigitalOut myled2(LED2);
DigitalOut myled3(LED3);
SDFileSystem sd(p5, p6, p7, p8, "sd");
-//AnalogOut DACout(p18);
-//Beep DACout(p18);
AnalogOut DACout(p18);
wave_player waver(&DACout); // Wave Player
-//DigitalOut myled(LED1);
uLCD_4DGL lcd(p28, p27, p29); //uLCD
DigitalIn pb1(p17); //Pushbutton 1
DigitalIn pb2(p19); //Pushbutton 2
DigitalIn pb3(p20); //Pushbutton 3
-//PwmOut led1(p21); //led 1
-//PwmOut led2(p22); //led2
-//PwmOut led3(p23); //led3
-
-//shiftbrite
+//Shiftbrite
DigitalOut latch(p15);
DigitalOut enable(p16);
SPI spi(p11, p12, p13);
@@ -113,16 +108,16 @@
//If push button 1 is pressed, start the game!
if(pb1 != 1){
- //Play button select
- /*
+
+ //Play button select
FILE *wave_file;
printf("\n\n\nHello, wave world!\n");
- wave_file=fopen("/sd/select.wav","r");
+ wave_file=fopen("/sd/mydir/select.wav","r");
waver.play(wave_file);
wait(1);
printf("ok!!");
fclose(wave_file);
- */
+
L3:
lcd.cls(); //Clear screen
@@ -253,163 +248,16 @@
}
//--------------------------------------------------------------------------------------------------------------------------------------
-
-
-
- // lcd.cls(); //Clear screen
- // wait(2);
-
- /*
- //--------------------------------------------------------------------------------------------------------------------------------
- //------------------------------- PLAYER 2 --------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------------------------------------
-
- //Check to see if Player 2 is ready to play
- lcd.rectangle(0,0,125,125,WHITE); //Draw white border
- lcd.locate(0,5);
- lcd.printf(" Press 1 if P2\n\n is ready");
-
- //Start timer for player 2 if player 2 presses pushbutton 1
- while(1){
- if(pb1 != 1)
- { //Play button select
- FILE *wave_file;
- printf("\n\n\nHello, wave world!\n");
- wave_file=fopen("/sd/mydir/select.wav","r");
- waver.play(wave_file);
-
- printf("ok!!");
- fclose(wave_file);
- break;
- }
- }
- lcd.cls();
- wait(1);
-
- lcd.rectangle(0,0,125,125,WHITE); //Draw white border
- //Tell player 1 that she/he has 5 seconds to chose between rock,paper and scissors
- lcd.locate(0,4);
- lcd.color(BLUE); //Set font color to yellow
- lcd.printf(" Player 2 \n\n\n");
- lcd.locate(0,6);
- lcd.color(GREEN); //Set font color to green
- lcd.printf("You have 5 seconds");
- lcd.printf(" to choose\n");
- lcd.printf(" an option!\n");
- wait(4);
-
-
-
- int j;
- int blue = 1;
- for(j = 5; j > -1 ; j--){
-
- lcd.cls();
- lcd.locate(4,4);
- lcd.text_width(8);
- lcd.text_height(8);
- RGB_LED(0,0,blue*25);
- blue = blue + 5;
- lcd.printf("%d",j);
- wait(1);
-
- //Rock
- if(pb1 != 1) {
- lcd.cls();
- choice2 = 1;
- lcd.rectangle(0,0,125,125,WHITE); //Draw white border
- lcd.locate(0,1);
- lcd.printf(" You chose rock!");
- //Play button select
- FILE *wave_file;
- printf("\n\n\nHello, wave world!\n");
- wave_file=fopen("/sd/mydir/select.wav","r");
- waver.play(wave_file);
-
- printf("ok!!");
- fclose(wave_file);
- lcd.circle(60, 60, 30,WHITE);
- wait(3);
- lcd.cls();
- break;
- }
- //Paper
- if(pb2 != 1) {
- lcd.cls();
- choice2 = 2;
- lcd.rectangle(0,0,125,125,WHITE); //Draw white border
- lcd.locate(0,1);
- lcd.printf(" You chose paper");
- //Play button select
- FILE *wave_file;
- printf("\n\n\nHello, wave world!\n");
- wave_file=fopen("/sd/mydir/select.wav","r");
- waver.play(wave_file);
-
- printf("ok!!");
- fclose(wave_file);
- lcd.filled_rectangle(50, 45, 80,90,WHITE);
- wait(3);
- lcd.cls();
- break;
- }
- //Scissors
- if(pb3 != 1) {
- lcd.cls();
- choice2 = 3;
- lcd.rectangle(0,0,125,125,WHITE); //Draw white border
- lcd.locate(0,1);
- lcd.printf("You chose scissors");
- //Play button select
- FILE *wave_file;
- printf("\n\n\nHello, wave world!\n");
- wave_file=fopen("/sd/mydir/select.wav","r");
- waver.play(wave_file);
-
- printf("ok!!");
- fclose(wave_file);
- lcd.circle(50, 60, 10,WHITE);
- lcd.circle(50, 80, 10,WHITE);
- lcd.line(62, 61, 90, 85, WHITE);
- lcd.line(62, 81, 90, 55, WHITE);
- wait(3);
- lcd.cls();
- break;
- }
- }
-
-//----------------------------------------------------------------------------------------------------------------------------------------
-// TIME-OUT
-//----------------------------------------------------------------------------------------------------------------------------------------
- //Time out message
- if(j == -1){
- lcd.cls();
- lcd.rectangle(0,0,125,125,WHITE); //Draw white border
- lcd.locate(0,5);
- lcd.color(RED);
- lcd.printf(" Your time is up!");
- lcd.color(GREEN);
- //Play time up tune
- FILE *wave_file;
- printf("\n\n\nHello, wave world!\n");
- wave_file=fopen("/sd/mydir/timeup.wav","r");
- waver.play(wave_file);
-
- printf("ok!!");
- fclose(wave_file);
- wait(3);
- goto L1;
- }
- RGB_LED(0,0,0);
-
- */
-
+ //Send player 2's choice to player 1
player2.printf("%c",choice2);
myled2 = 1;
wait(1);
+ //Receive Player 1's choice
while(1){
+ lcd.locate(1,4);
+ lcd.printf(" Waiting for \n the other player");
if(player2.readable()){
choice1 = player2.getc();
myled = 1;
@@ -430,22 +278,6 @@
lcd.locate(1,2);
lcd.printf(" It's a draw!");
wait(4);
- /*
- lcd.cls();
- lcd.locate(1,4);
- lcd.rectangle(0,0,125,125,WHITE); //Draw white border
- lcd.printf(" P1 Score : %d\n",p1);
- lcd.printf(" P2 Score : %d\n\n\n\n",p2);
- lcd.printf(" 1 to continue\n\n");
- lcd.printf(" 2 to end\n\n");
-
- while(1){
- if(pb1 != 1) {
- lcd.cls();
- goto L3;
- }
- else if(pb2 != 1) goto L2;
- }*/
}
//Case 2:
//Player 1 : Rock
@@ -463,22 +295,6 @@
lcd.filled_rectangle(50, 60, 80,90,WHITE);
p2++;
wait(4);
- /*
- lcd.cls();
- lcd.locate(1,4);
- lcd.rectangle(0,0,125,125,WHITE); //Draw white border
- lcd.printf(" P1 Score : %d\n",p1);
- lcd.printf(" P2 Score : %d\n\n\n\n",p2);
- lcd.printf(" 1 to continue\n\n");
- lcd.printf(" 2 to end\n\n");
-
- while(1){
- if(pb1 != 1) {
- lcd.cls();
- goto L3;
- }
- else if(pb2 != 1) goto L2;
- }*/
}
//Case 3:
//Player 1 : Paper
@@ -495,23 +311,6 @@
lcd.filled_rectangle(50, 60, 80,90,WHITE);
p1++;
wait(5);
- /*
- lcd.cls();
- lcd.locate(1,4);
- lcd.rectangle(0,0,125,125,WHITE); //Draw white border
- lcd.printf(" P1 Score : %d\n",p1);
- lcd.printf(" P2 Score : %d\n\n\n\n",p2);
- lcd.printf(" 1 to continue\n\n");
- lcd.printf(" 2 to end\n\n");
-
- while(1){
- if(pb1 != 1) {
- lcd.cls();
- goto L3;
- }
- else if(pb2 != 1) goto L2;
- }
- */
}
//Case 4:
@@ -532,23 +331,7 @@
lcd.line(62, 61, 90, 85, WHITE);
lcd.line(62, 81, 90, 55, WHITE);
wait(5);
- /*
- lcd.cls();
- lcd.locate(1,4);
- lcd.rectangle(0,0,125,125,WHITE); //Draw white border
- lcd.printf(" P1 Score : %d\n",p1);
- lcd.printf(" P2 Score : %d\n\n\n\n",p2);
- lcd.printf(" 1 to continue\n\n");
- lcd.printf(" 2 to end\n\n");
- while(1){
- if(pb1 != 1) {
- lcd.cls();
- goto L3;
- }
- else if(pb2 != 1) goto L2;
- }
- */
}
//Case 5:
//Player 1 : Scissor
@@ -567,23 +350,7 @@
lcd.line(62, 61, 90, 85, WHITE);
lcd.line(62, 81, 90, 55, WHITE);
wait(5);
- /*
- lcd.cls();
- lcd.locate(1,4);
- lcd.rectangle(0,0,125,125,WHITE); //Draw white border
- lcd.printf(" P1 Score : %d\n",p1);
- lcd.printf(" P2 Score : %d\n\n\n\n",p2);
- lcd.printf(" 1 to continue\n\n");
- lcd.printf(" 2 to end\n\n");
- while(1){
- if(pb1 != 1) {
- lcd.cls();
- goto L3;
- }
- else if(pb2 != 1) goto L2;
- }
- */
}
//Case 6:
//Player 1 : Rock
@@ -599,22 +366,6 @@
p1++;
lcd.circle(60, 70, 30,WHITE);
wait(5);
- /*
- lcd.cls();
- lcd.locate(1,4);
- lcd.rectangle(0,0,125,125,WHITE); //Draw white border
- lcd.printf(" P1 Score : %d\n",p1);
- lcd.printf(" P2 Score : %d\n\n\n\n",p2);
- lcd.printf(" 1 to continue\n\n");
- lcd.printf(" 2 to end\n\n");
-
- while(1){
- if(pb1 != 1) {
- lcd.cls();
- goto L3;
- }
- else if(pb2 != 1) goto L2;
- }*/
}
//Case 7:
//Player 1 : Scissors
@@ -624,29 +375,13 @@
lcd.cls();
lcd.locate(1,1);
- lcd.printf("P1 chose Scissors\n");
+ lcd.printf("P1 chose Scissors ");
lcd.printf(" P2 chose Rock\n\n");
lcd.locate(1,3);
lcd.printf("P2 wins!");
p2++;
lcd.circle(60, 70, 30,WHITE);
wait(5);
- /*
- lcd.cls();
- lcd.locate(1,4);
- lcd.rectangle(0,0,125,125,WHITE); //Draw white border
- lcd.printf(" P1 Score : %d\n",p1);
- lcd.printf(" P2 Score : %d\n\n\n\n",p2);
- lcd.printf(" 1 to continue\n\n");
- lcd.printf(" 2 to end\n\n");
-
- while(1){
- if(pb1 != 1) {
- lcd.cls();
- goto L3;
- }
- else if(pb2 != 1) goto L2;
- }*/
}
lcd.cls();
@@ -682,6 +417,8 @@
wait(1);
while(1){
+ lcd.locate(1,4);
+ lcd.printf(" Waiting for \n the other player");
if(player2.readable()){
continue1 = player2.getc();
myled = 1;
@@ -692,34 +429,12 @@
//If both say continue then you need to go back to L3
if(continue1 == 'Y' && continue2 == 'Y') goto L3;
else goto L2;
-
- }
-
- }
+ } }
-
- // Code to play a wave file
- /* FILE *wave_file;
- printf("\n\n\nHello, wave world!\n");
- wave_file=fopen("/sd/mydir/HG.wav","r");
- waver.play(wave_file);
- wait(5);
- printf("ok!!");
- fclose(wave_file);
- */
-
-
-
-
-
-
-
//If game ends :
L2:
lcd.cls();
-
-
//Create animation of two bouncing balls
float fx=50.0,fy=21.0,vx=5.0,vy=0.4,vx2=2,fx2=40.0;
int x=50,y=21,radius=4;
@@ -729,15 +444,12 @@
wait(2);
//If Player 1's score is higher than Player 2's score
if(p1 > p2)
- //lcd.printf("Player 1 wins!");
lcd.text_string("Player 1 Wins!", 2, 4, FONT_7X8, WHITE);
//If Player 1's score is equal to Player 2's score
- else if(p1 == p2)
- //lcd.printf("It's a draw!");
+ else if(p1 == p2)
lcd.text_string("It's a draw!", 2, 4, FONT_7X8, WHITE);
//If Player 2's score is higher than Player 1's score
else if(p2 > p1)
- //lcd.printf("Player 2 wins!");
lcd.text_string("Player 2 Wins!", 2, 4, FONT_7X8, WHITE);
// Play winning tune