processing interfacing

Hi again :slight_smile:

I am now trying to set up a communication between Processing and Arduino. I created a simple pong game to do this where the paddle is controlled by a potentiometer connected to the board.

This part works really well, but I want to add communication in the other direction too: I want Processing to send a signal when the paddle is hit, and when the Arduino board receives this signal, it plays a sound on a connected speaker.

How can I tell the Arduino what is incoming and what is outgoing information?

I am attaching the code I’ve got so far (in the following posts since I’m running out of space), but the speaker element doesn’t work.

Thanks for the help!

Processing:

import processing.serial.*;


/****************************************************
*  ezPong_processing                                
*                                                   
*  copyleft 2006 | Elie Zananiri                    
*  prisonerjohn@gmail.com                           
*                                                   
*  based on the Arduino meets Processing tutorials  
*****************************************************/



// serial connection variables 
Serial port;
String portName = "/dev/cu.usbserial-3B1";  
int baudRate = 19200;
int value = 0; 
String buffer=""; 
int potValue = 0;  

// visuals variables
Ball ball;
int ballSize = 10;
Paddle paddle;
int paddleWidth = 80;
int paddleTop = 25;
int paddlePos; 


void setup() {
  size(600, 400); 
  framerate(30);
  smooth();
  
  // establish serial port connection      
  port = new Serial(this, portName, baudRate);
  
  // create ball
  ball = new Ball(random(ballSize, width-ballSize), random(ballSize, height-ballSize), ballSize, color(200, 0, 0));
  // create paddle
  paddle = new Paddle(random(paddleWidth, width-paddleWidth), height-paddleTop, paddleWidth, ballSize, color(200, 200, 200));
}


void serialEvent(int serial){
  if (serial == 10) { 
    // if serial is line break store and clear the buffer       
    potValue = 1023-int(buffer);
    buffer = "";
  } else if (serial == 60) {
    // if serial event is beep signal clear the buffer
    buffer = "";
  } else {
    // add an event to the buffer 
    buffer += char(serial);
  }
  
  // convert potValue to the correct range for the paddle position
  paddlePos = (potValue*(width-ballSize-paddleWidth))/1023+ballSize/2;
  // move the paddle
  paddle.move(paddlePos);
}


void draw() {
  // listen to serial port and trigger serial event  
  while (port.available() > 0) {
    value = port.read();
    serialEvent(value);
  }
 
  // draw graphics
  // erase bg using a semi-transparent square to have a motion blur effect
  drawBG(); 
  // move and draw the ball
  ball.step();
  // draw the paddle
  paddle.draw();
}


void drawBG() {
  // draw a semi-transparent rect over the entire canvas to create a motion blur effect
  noStroke();
  fill(200, 200, 100, 150);
  rect(0, 0, width, height);
}



/**
 * class Ball
 */
class Ball {
  // position variables
  float x;
  float y;
  
  // speed variables
  float dX;
  float dY;
  int frame;
  
  // graphics variables
  int size;
  color col;
  
  
  // constructor
  public Ball(float startX, float startY, int theSize, color theColor) {
    x = startX;
    y = startY;
    
    size = theSize;
    col = theColor;
    
    // randomly set a starting speed
    dX = ceil(random(0, 5));
    dY = ceil(random(0, 5));
    frame = 0;
  }
 
 
  public void move() {
    x += dX;
    y += dY;   
  }
  
  
  public void accelerate() {
    dX *= 1.01;
    dY *= 1.01;
  }
  
  
  public void draw() {
    stroke(0);
    fill(col);
    ellipse(x, y, size, size);   
  }
  
  
  public void checkCollisions() {
    // check for right wall collision
    if ((x+size/2) > width) {
      x = width-size-1;
      dX *= -1;
    // check for left wall collision
    } else if ((x-size/2) < 0) {
      x = size+1;
      dX *= -1;
    }
    
    // check for paddle collision
    if ((y+(size/2) > paddle.y) && (x > paddle.x) && (x < (paddle.x+paddle.w))) {
      y = paddle.y-size-1;
      dY *= -1;
      // send signal to serial
      //port.write(60);
    // check for bottom wall collision
    } else if (y+(size/2) > height) {
      y = height-size-1;
      dY *= -1;
    // check for top wall collision
    } else if (y-(size/2) < 0) {
      y = size+1;
      dY *= -1;
    }
  }
  
  
  public void step() {
    move();
    // accelerate ball speed every 30 frames i.e. every second
    if (++frame%30 == 0) {
      accelerate();
    }
    checkCollisions();
    
    draw();
  }
}



/**
 * class Paddle
 */
class Paddle {
  // position variables
  float x;
  float y;
  
  // graphics variables
  int w;
  int h;
  color col;
  
  
  // constructor
  public Paddle(float startX, float startY, int theWidth, int theHeight, color theColor) {
    x = startX;
    y = startY;
    
    w = theWidth;
    h = theHeight;
    col = theColor;
  }
  
  
  public void move(float newX) {
    // assign the received x position
    x = newX;  
  }
  
  
  public void draw() {
    // set the fill color
    noStroke();
    fill(col);
    // draw the left rounded edge
    ellipse(x, y, h, h);  
    // draw the center
    rect(x, y-h/2, w, h);
    // draw the right rounded edge
    ellipse(x+w, y, h, h);
    
    // set the stroke color
    stroke(0);
    // add the outline around the paddle shape
    line(x, y-h/2, x+w, y-h/2);
    line(x, y+h/2, x+w, y+h/2);
    arc(x, y, h, h, PI/2, -PI/2);
    arc(x+w, y, h, h, -PI/2, PI/2);
  }
}

Arduino:

/**************************************************** 
*  ezPong_arduino                                                     
*                                                   
*  copyleft 2006 | Elie Zananiri                    
*  prisonerjohn@gmail.com                           
*                                                   
*  based on the following sketches                  
*   pwm_sound/play_melody                           
*   serial_comm/analog_read_send                    
*****************************************************/



// program variables
int potIn = 2;
int value = 0;
int speakerOut = 5;
int tone = 1915;
int inData = 0;
  

void setup() {
  // set the pin modes
  pinMode(potIn, INPUT);
  pinMode(speakerOut, OUTPUT); 
  
  // begin sending over serial port
  Serial.begin(19200);
}


void loop() {
  // read the value using analog input
  value = analogRead(potIn);

  // send value over the serial port...
  printInteger(value);
  // ..and a separator signal 
  printByte(10);

  delay(10);
  
  
  if (Serial.available() > 0) {
    // read the incoming date
    inData = Serial.read();
    
    // check if the incoming data is the 'play sound' code
    if (inData == 60) {
      Serial.print(inData, DEC);
      
       // play a sound through the speaker
      digitalWrite(speakerOut, HIGH);
      delayMicroseconds(tone);
      digitalWrite(speakerOut, LOW);
      delayMicroseconds(tone);      
    
      // pause
      digitalWrite(speakerOut, 0);
      delayMicroseconds(500);
    }
  }
}

anyone?..

Your Arduino code looks fine, but you've commented out the call to port.write() in the Processing code. Is that the problem? Otherwise, have you tried the speaker code on its own, independent of the serial code? Does it work?

No, that's not the problem, it's just another case of copying/pasting too quickly.

Do Arduino and Processing automatically know what is incoming and what is outgoing data? Is it possible the signal the Processing code is sending is being read by Processing too or does it just read whatever is being "Serial.written" from other sources?

Yes, they know what's incoming and what's outgoing. The serial communication uses two data lines, one in each direction, so there shouldn't be any confusion.

Are you sure your collision detection routine is working properly? Have you tried printing to the Processing console along with sending serial data?

i know the collisions work because the ball bounces back :) but i will try again with the console when i get home later and see what i get

Sorry for replying so late, I totally forgot about this.

I just tested my program again and it works. I don't know what was wrong, but I'm suspecting it's something like reading from the wrong serial port.

Anyways, thanks for the help.

I'm having another problem with this code that you can check out here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1149391368