Advertisement
AleXGibbs66

thePotatoGame

Nov 22nd, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. int thePotato;
  2. int danceCheck=false;
  3. int onButton=A0;
  4. int buttons[]={A2,2,3,4};
  5. int redLeds[]={5,6,7,8};
  6. int greenLeds[]={9,10,11,12};
  7. int checkOn=false;
  8. int stillInTheGame[]={true,true,true,true};
  9. void lightBlink(int r,int g,int timeDelay){
  10.     digitalWrite(redLeds[r],HIGH);
  11.     digitalWrite(greenLeds[g],HIGH);
  12.     delay(timeDelay);
  13.     digitalWrite(redLeds[r],LOW);
  14.     digitalWrite(greenLeds[g],LOW);
  15. }
  16. void playerLight(int player,int timeDelay,int state){
  17.     digitalWrite(redLeds[player],state);
  18.     digitalWrite(greenLeds[player],state);
  19.     delay(timeDelay);
  20. }
  21. void danceStart(){
  22.     Serial.println("Starting Dance");
  23.     lightBlink(0,0,500);
  24.     lightBlink(1,3,500);
  25.     lightBlink(2,1,500);
  26.     lightBlink(3,2,500);
  27.     for(int i=0;i<4;i++){
  28.         playerLight(i,240,HIGH);
  29.     }
  30.     for(int i=0;i<4;i++){
  31.         playerLight(i,0,LOW);
  32.     }
  33.     delay(1000);
  34.     for(int i=0;i<4;i++){
  35.         playerLight(i,0,HIGH);
  36.     }
  37.     delay(1000);
  38.     for(int i=0;i<4;i++){
  39.         playerLight(i,0,LOW);
  40.     }
  41.     delay(2000);
  42.     danceCheck=true;
  43.     Serial.println("Dancing done!!");
  44. }  
  45. void loser(int lastPlayer){
  46.     checkOn=false;
  47.     danceCheck=false;
  48.     stillInTheGame[lastPlayer]=false;
  49.     for(int i=0;i<4;i++){
  50.         lightBlink(lastPlayer,lastPlayer,250);
  51.     }
  52.     digitalWrite(redLeds[lastPlayer],HIGH);
  53. }
  54. void lightSwitch(int turnOff){
  55.     digitalWrite(greenLeds[turnOff],LOW);
  56.     thePotato=(turnOff+1)%4;
  57.     while(stillInTheGame[thePotato]==false){       
  58.         thePotato=(thePotato+1)%4;
  59.     }
  60.     digitalWrite(greenLeds[thePotato],HIGH);
  61. }
  62. void setup(){
  63.     Serial.begin(9600);
  64.     for(int counter=0;counter<4;counter++){
  65.         pinMode(buttons[counter],INPUT_PULLUP);
  66.         pinMode(redLeds[counter],OUTPUT);
  67.         pinMode(greenLeds[counter],OUTPUT);
  68.         digitalWrite(redLeds[counter],LOW);
  69.         digitalWrite(greenLeds[counter],LOW);
  70.     }
  71.     pinMode(onButton,INPUT);
  72. }
  73. void loop(){
  74.     while(digitalRead(onButton)==LOW){
  75.         Serial.println("The on button is off.");
  76.     }
  77.     Serial.println("The on button has been turned on!!!");
  78.     delay(1000);
  79.     danceStart();
  80.     thePotato=random(0,4);
  81.     for(int losers=0;losers<3;losers++){
  82.         if(losers>=3){
  83.             Serial.println("The game is done!!");
  84.             Serial.print("The winner is player ");
  85.             for(int b=0;b<4;b++){
  86.                 if(stillInTheGame[b]){
  87.                     Serial.print(b+1);
  88.                 }
  89.             }
  90.             Serial.println("!");
  91.         }  
  92.         if(losers<3){
  93.             Serial.print("Round ");
  94.             Serial.print(losers+1);
  95.             Serial.println("!");
  96.         }
  97.         else if(losers==3){
  98.             Serial.println("Final Round!!!!");
  99.         }
  100.         long maxTime=random(30,51)*1000;
  101.         Serial.print("Timer set to ");
  102.         Serial.println(maxTime);
  103.         long startTime=millis();
  104.         boolean released=false;
  105.         while(millis()-startTime<maxTime){
  106.             // check if current player has pressed button
  107.             digitalWrite(greenLeds[thePotato],HIGH);
  108.             int buttonState=digitalRead(buttons[thePotato]);
  109.             if(buttonState==HIGH){
  110.                 released=true;
  111.             }
  112.             if(released&&buttonState==LOW){
  113.                 // move to next player
  114.                 lightSwitch(thePotato);
  115.                 Serial.print("The player is ");
  116.                 Serial.println(thePotato+1);
  117.                 released=false;
  118.             }
  119.         }
  120.         loser(thePotato);
  121.         lightSwitch(thePotato);
  122.     }  
  123.     delay(1000);
  124. }
  125. //LastLine #115
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement