Home » Articles » Сорс код

Game with Serial function

#include <Time.h>
int btn1=2;
int btn2=3;
int led1=4;
int led2=5;
int buttonState1=0;
int buttonState2=0;
int a=random(7,12);
int b=0;
int c=0;
int d=0;
int sw1=0;
int sw2=0;

void setup(){
  Serial.begin(9600);
  pinMode(btn1,INPUT);
  pinMode(btn2,INPUT);  
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  setTime(0,0,0,0,0,0);
  //Serial.println(a);
  delay(500);
  Serial.println("Ready?!");
  delay(500);
  Serial.println("Set!");
  delay(500);
  Serial.println("GO!");
  Serial.println(a);
}

void loop(){
  buttonState1=digitalRead(btn1);
  buttonState2=digitalRead(btn2);
  //Serial.println(buttonState1);
  //Serial.println(buttonState2);

  b=second();
  if(buttonState1==HIGH&&sw1==0){
    c=a-b;
    sw1=1;
  }
  if(buttonState2==HIGH&&sw2==0){
    d=a-b;
    sw2=1;
  }
  if(fabs(c)==fabs(d)&&sw1==1&&sw2==1){
    digitalWrite(led1,HIGH);
    digitalWrite(led2, HIGH);
    Serial.println("Draw!");
  }

  if(fabs(c)>fabs(d)&&sw1==1&&sw2==1){
    digitalWrite(led1, HIGH);
    Serial.println("Player 1 Wins");
}

if(fabs(c)<fabs(d)&&sw1==1&&sw2==1){
  digitalWrite(led2, HIGH);
  Serial.println("Player 2 Wins");
}
}

Category: Сорс код | Added by: MilenTsolov (2016-03-07)
Views: 207 | Rating: 0.0/0
Total comments: 0
avatar