Ex: Rock_Paper_Scissor w/ Arduinos Connected

Arduinos connected through serial connections. Using RGB in place of rock, paper and scissor.

R beats G
G beats B
B beats R

-------- ARDUINO CODE -------------

 int RxPin = 0;
 int TxPin = 1;

 int button1Pin = 2;    // select the input pin for the potentiometer
 int button2Pin = 4;

 int p1color = -1;
 int b1currState = 0;
 int b1prevState = 0;

 int b2count = 0;
 int b2currState = 0;
 int b2prevState = 0;

 int ledPin1 = 11;
 int ledPin2 = 10;
 int ledPin3 = 9;

 int ledWinPin = 5;
 int ledLosPin = 6;

 boolean hold = true;
 int checkbit = 0;
 int p2color = -1;

 void setup() {  
   pinMode(button1Pin, INPUT);
   pinMode(button2Pin, INPUT);
  
   pinMode(ledPin1, OUTPUT);
   pinMode(ledPin2, OUTPUT);
   pinMode(ledPin3, OUTPUT);
  
   pinMode(ledWinPin, OUTPUT);
   pinMode(ledLosPin, OUTPUT);

//   pinMode(RxPin, INPUT);
//   pinMode(TxPin, OUTPUT);
  
   Serial.begin(9600);
 }


void loop()
{
  b1currState  = digitalRead(button1Pin);
  b2currState  = digitalRead(button2Pin);

/*  STEP 1: CHOOSING WHICH COLOR
    0 = nothing - cannot send when LED is in this state
    1 = R
    2 = G
    3 = B   
*/
  // getting input from the RGB LED button
  // to learn which LED is chosen
  // and saaving the chosen color in p1color
 
   if (b1currState != b1prevState)
   {
       if (b1currState == HIGH)
       {
         if (p1color == 2) p1color = 0;
         else  p1color++;
         Serial.print("RGB LED state:  ");
         Serial.println(p1color, DEC);
       }
       b1prevState = b1currState; 
       delay (100);   
   }
  
   if (p1color == 0)
   {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, HIGH);
   } else
   if (p1color == 1)
   {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, LOW);
   } else
   if (p1color == 2)
   {
    digitalWrite(ledPin1, HIGH);  
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
   }else
   {
    digitalWrite(ledPin1, LOW);  
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
   }
  
/*  STEP 2: SENDING CHOSEN COLOR TO OTHER PLAYER
*/
   if (b2currState != b2prevState)
   {
       if (b2currState == HIGH)
       {
         Serial.print("Sending state = "); Serial.print(p1color, DEC);
         Serial.println(" to other player's Arduino...");
                
//         digitalWrite(TxPin, 3);
//         digitalWrite(TxPin, p1color);
        
         Serial.write(255);
         Serial.write(p1color);
       }
       b2prevState = b2currState;   
       delay(100);   
   }
  
  
/*  STEP 3: HOLDING UNTIL COLOR RECEIVED
            FROM OTHER PLAYER
*/
  if (Serial.available())
  {
//    p2color = digitalRead(RxPin); //Serial.read();
    p2color = Serial.read();
   
    if (!hold)
    {     
      if ( (p1color == 0 && p2color == 1)
         ||(p1color == 1 && p2color == 2)
         ||(p1color == 2 && p2color == 0)  )
      {
        digitalWrite(ledWinPin,HIGH);
        digitalWrite(ledLosPin,LOW);
      }
      else    
      if (p2color == p1color)
      {
        digitalWrite(ledWinPin,HIGH);
        digitalWrite(ledLosPin,HIGH);
      }
      else
      {
        digitalWrite(ledWinPin,LOW);
        digitalWrite(ledLosPin,HIGH);
      }
     
      delay (20000);
     
      // resetting after some time
      hold = true;     
      p2color = -1;
      digitalWrite(ledWinPin,LOW);
      digitalWrite(ledLosPin,LOW);
    }
   
    if (p2color == 255) //255
      hold = false; 
  }
}










No comments:

Post a Comment