Introduction: Duel of the Fate

Picture of Duel of the Fate

This is a pong game works on Processing with two Arduino potentiometer as controller.

Step 1: Circuit Example

Picture of Circuit Example

Arduino UNO x 1

Mini breadbaord x1


Potentiometer x 2

Wires x 12

Step 2: Example Code for Arduino

Picture of Example Code for Arduino

In this project you will need to code in both Processing and Arduino.



For Arduino

int secondSensor = 0; // second analog sensor

int firstSensor = 0; // first analog sensor

int inByte = 0; // incoming serial byte
char val;
void setup()
{
  // start serial port at 9600 bps:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  pinMode(2, OUTPUT); // digital sensor is on digital pin 2
  
  establishContact(); // send a byte to establish contact until receiver
  // responds
}
void loop()
{
  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.read();
    // read first analog input, divide by 4 to make the range 0-255:
    firstSensor = analogRead(A0) / 4;
    // delay 10ms to let the ADC recover:
    delay(10);
    // read second analog input, divide by 4 to make the range 0-255:
    secondSensor = analogRead(1) / 4;
    // read switch, map it to 0 or 255L
    // send sensor values:
    Serial.write(firstSensor);
    Serial.write(secondSensor);
   
  }
}
void establishContact() {
  while (Serial.available() <= 0) {
    Serial.print('A'); // send a capital A
    delay(100);
  }
}

Step 3: Example Code for Processing

Picture of Example Code for Processing

For Proccessing

// This example code is in the public domain.
import processing.serial.*; int bgcolor; // Background color int fgcolor; // Fill color Serial myPort; // The serial port int[] serialInArray = new int[3]; // Where we'll put what we receive int serialCount = 0; // A count of how many bytes we receive int apos, bpos; // Starting position of the player int p1score=0; // Score int p2score=0; boolean start=false; // The status of game boolean p1goal=false; boolean p2goal=false; boolean Gameover=false;
boolean Hit=false;
boolean Goal=false;
ball b;
Letter l;
Number n;
Score s;
boolean firstContact = false;  // Whether we've heard from the
// microcontroller
void setup() {
  size(500, 305); // Stage size
  noStroke();  // No border on the next thing drawn
  // Set the starting position of the ball (middle of the stage)
  apos = width/2;
  bpos = height/2;
  b= new ball();
  l= new Letter();
  n= new Number();
  s= new Score();
  // Print a list of the serial ports, for debugging purposes:
  // I know that the first port in the serial list on my mac
  // is always my FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
  
}
void draw() {
  
  background(bgcolor);
  fill(255);
  // Draw the shape
  rect(30, apos, 20, 60);
  rect(470, bpos, 20, 60);
  b.balldraw();
  b.ballcheck();
  s.display();
  s.check();
}
void serialEvent(Serial myPort) {
  // read a byte from the serial port:
  int inByte = myPort.read();
  // if this is the first byte received, and it's an A,
  // clear the serial buffer and note that you've
  // had first contact from the microcontroller.
  // Otherwise, add the incoming byte to the array:
  if (firstContact == false) {
    if (inByte == 'A') {
      myPort.clear();   // clear the serial port buffer
      firstContact = true;  // you've had first contact from the microcontroller
      myPort.write('A');  // ask for more
    }
  } else {
    // Add the latest byte from the serial port to array:
    serialInArray[serialCount] = inByte;
    serialCount++;
    // If we have 3 bytes:
    if (serialCount > 2 ) {
      apos = serialInArray[0];
      bpos = serialInArray[1];
      fgcolor = serialInArray[2];
      // print the values (for debugging purposes only):
      println(apos + "t" + bpos + "t");
      // Send a capital A to request new sensor readings:
      
      
      
      myPort.write('A');
      // Reset serialCount:
      serialCount = 0;
    }
  }
}

Step 4: Example Code for Processing (2)

class ball {
  float x=250; // Starting position of the ball
  float y=150;
  float xspeed=3; // Speed of the ball
  float yspeed=3;
  void balldraw() {
    fill(255);
    rectMode(CENTER);
    rect(width/2, height/2, 6, height);
    rectMode(CORNER);
    if ((keyPressed)&&(key==CODED)) { //Press UP to start the game
      if (keyCode==UP)
      {
        if (Gameover==false) {
          start=!start;
        }
      }
    }
    if (start==false) {
      fill(255);
      x=250;
      y=150;
      ellipse(width/2, height/2, 30, 30);
    }
    if (start==true) {   
      fill(255);
      ellipse(x, y, 30, 30);
      x=x+xspeed;
      y=y+yspeed;
    }
  }
  void ballcheck() {
    if (y<15) {
      yspeed=-yspeed;
    }
    
    
    if (y>290) {
      yspeed=-yspeed;
    }
   
    if (x>485) {
      start=false;
      p1score+=1;
      p1goal=true;
      Goal=true;
     
    }
    if (x<15) {
      start=false;
      p2score+=1;
      p2goal=true;
      Goal=true;
      
    }
    if ((x>45)&&(x<65)&&(y>apos-15)&&(y<apos+75)) {<="" p=""></apos+75))>
      xspeed=-xspeed;
      Hit=true;
    }
    
    if ((x>450)&&(x<470)&&(y>bpos)&&(y<bpos+75)) {<="" p=""></bpos+75))>
      xspeed=-xspeed;
      Hit=true;
    }       

if ((x>40)&&(x<width-<width-40)&&(y style="background-color: initial;">40)&&(y>40)&&(y<height-40)){Hit=false;}</width-40)&&(y>
<width-40)&&(y style="background-color: initial;">}</width-40)&&(y>
<width-40)&&(y style="background-color: initial;">}</width-40)&&(y>
<width-40)&&(y style="background-color: initial;"><p>class Score{</p><p>void display(){
  if(p1score==0){n.Zero(width/2-15,15,2);}
  if(p1score==1){n.One(width/2-15,15,2);}
  if(p1score==2){n.Two(width/2-15,15,2);}
  if(p1score==3){n.Three(width/2-15,15,2);}
  
  if(p2score==0){n.Zero(width/2+12,15,2);}
  if(p2score==1){n.One(width/2+12,15,2);}
  if(p2score==2){n.Two(width/2+12,15,2);}
  if(p2score==3){n.Three(width/2+12,15,2);}
  
}</p><p>void check(){</p><p>  if(p1score==3){
  
  l.P(width/2-42,105,8);
  n.One(width/2+40,105,8);
  
  l.W(width/2-46,180,8);
  l.I(width/2-4,180,8);
  l.N(width/2+38,180,8);
  
  Gameover=true;
  }
   if(p2score==3){
  
  l.P(width/2-42,105,8);
  n.Two(width/2+40,105,8);
  
  l.W(width/2-46,180,8);
  l.I(width/2-4,180,8);
  l.N(width/2+38,180,8);
  
  Gameover=true;
  }</p><p>}</p><p>}</p></width-40)&&(y>

Step 5: Example Code for Processing (3)

For Processing (3)


This part is unnecessary, but I still use this way in my code. If you can use text function, you can ignore this part and fix the code on your own.

 class Letter {
  void A(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x, y, p, p);
    rect(x-p*1, y+p*1, p, p*4);
    rect(x+p*1, y+p*1, p, p*4);
    rect(x, y+p*2, p, p);
  }
  void B(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x-p*1, y, p, p*5);
    rect(x-p*1, y, p*2, p);
    rect(x-p*1, y+p*2, p*2, p);
    rect(x-p*1, y+p*4, p*2, p);
    rect(x+p*1, y+p*1, p, p*3);
  }
  void C(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x-p*1, y+p*1, p, p*3);
    rect(x, y, p*2, p);
    rect(x, y+p*4, p*2, p);
  }
  void D(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x-p*1, y, p, p*5);
    rect(x, y, p, p);
    rect(x+p*1, y+p*1, p, p*3);
    rect(x, y+p*4, p, p);
  }
  void E(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x-p*1, y, p*3, p);
    rect(x-p*1, y, p, p*5);
    rect(x-p*1, y+p*2, p*3, p);
    rect(x-p*1, y+p*4, p*3, p);
  }
  void F(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x-p*1, y, p*3, p);
    rect(x-p*1, y, p, p*5);
    rect(x-p*1, y+p*2, p*3, p);
  }
  void G(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x-p*1, y, p*3, p);
    rect(x-p*1, y, p, p*5);
    rect(x-p*1, y+p*4, p*3, p);
    rect(x+p*1, y+p*3, p, p);
  }
  void H(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x-p*1, y, p, p*5);
    rect(x+p*1, y, p, p*5);
    rect(x, y+p*2, p, p);
  }
  void I(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x, y, p, p*5);
    rect(x-p*1, y+p*4, p*3, p);
    rect(x-p*1, y, p*3, p);
  }
  void J(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x-p*1, y, p*3, p);
    rect(x+p*1, y, p, p*4);
    rect(x, y+p*4, p, p);
    rect(x-p*1, y+p*3, p, p);
  }
  void K(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x-p*1, y, p, p*5);
    rect(x, y+p*2, p, p);
    rect(x+p*1, y, p, p*2);
    rect(x+p*1, y+p*3, p, p*2);
  }
  void L(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x-p*1, y, p, p*5);
    rect(x-p*1, y+p*4, p*3, p);
  }
  void M(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x-p*1, y, p, p*5);
    rect(x+p*1, y, p, p*5);
    rect(x, y+p*1, p, p*3);
  }
  void N(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x-p*1, y, p, p*5);
    rect(x+p*1, y, p, p*5);
    rect(x, y, p, p);
  }
  void O(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x-p*1, y, p*3, p);
    rect(x-p*1, y, p, p*5);
    rect(x+p*1, y, p, p*5);
    rect(x-p*1, y+p*4, p*3, p);
  }
  void P(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x-p*1, y, p, p*5);
    rect(x-p*1, y, p*3, p);
    rect(x+p*1, y, p, p*3);
    rect(x-p*1, y+p*2, p*3, p);
  }
  void Q(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x-p*1, y, p*3, p);
    rect(x-p*1, y, p, p*4);
    rect(x+p*1, y, p, p*4);
    rect(x-p*1, y+p*3, p*3, p);
    rect(x+p*1, y+p*4, p, p);
    
  }
  void R(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x-p*1, y, p, p*5);
    rect(x-p*1, y, p*3, p);
    rect(x+p*1, y, p, p*3);
    rect(x-p*1, y+p*2, p*3, p);
    rect(x, y+p*3, p, p);
    rect(x+p*1, y+p*4, p, p);
  }
  void S(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x, y, p*2, p);
    rect(x-p*1, y+p*1, p, p*2);
    rect(x-p*1, y+p*2, p*3, p);
    rect(x+p*1, y+p*2, p, p*2);
    rect(x-p*1, y+p*4, p*2, p);
  }
  void Y(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x-p*1, y, p, p*3);
    rect(x+p*1, y, p, p*3);
    rect(x, y+p*2, p, p*3);
  }
  void V(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x-p*1, y, p, p*4);
    rect(x+p*1, y, p, p*4);
    rect(x, y+p*3, p, p*2);
  }
  void W(float x, float y, float p) {
    rect(x-p*1, y, p, p*5);
    rect(x+p*1, y, p, p*5);
    rect(x, y+p*2, p, p*2);
  }
}
class Number {
void Zero(float x, float y, float p) { rectMode(CORNER); fill(255); rect(x-p*1, y, p*3, p); rect(x-p*1, y, p, p*5); rect(x+p*1, y, p, p*5); rect(x-p*1, y+p*4, p*3, p); } void One(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x, y, p, p*4);
    rect(x-p*1, y, p, p);
    rect(x-p*1, y+p*4, p*3, p);
  }
  void Two(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x-p*2, y, p*3, p);
    rect(x, y, p, p*3);
    rect(x-p*2, y+p*2, p*3, p);
    rect(x-p*2, y+p*2, p, p*3);
    rect(x-p*2, y+p*4, p*3, p);
  }
  void Three(float x, float y, float p) {
    rectMode(CORNER);
    fill(255);
    rect(x-p*2, y, p*3, p);
    rect(x, y, p, p*3);
    rect(x-p*2, y+p*2, p*3, p);
    rect(x, y+p*2, p, p*3);
    rect(x-p*2, y+p*4, p*3, p);
  }
}

Step 6:

Comments