Making a connection between Arduino and proccessing

Hi,

I'm new in this environment for programming. For my study I need to control a programm of processing by a signal of Arduino. But somehow I really can't figure out how to do it, and I hope that you can help me.

The idea is to let a pacman move around by a handclap signal. The pacman should go a random direction when you clap. Firstly I wrote a programm for Arduino, which makes a LED light up if there is a clapping sound. I also made an electronic circuit with a microphone which works well. I programmed that every time when there is a clap, a random number 0 - 3 will be produced in the serial. This is for the direction of the pacman (4 directions: up, down, left, right)

Arduino programm

int sensorPin = A0; // Microphone Sensor Pin
int LED = 13; //LED on the arduino at pin 13 (?)
int sensorValue = 0;
int randomDirection = 0;


void setup () {
 pinMode (LED, OUTPUT);
 Serial.begin(9600);
}

void loop() {
 //read the value from the sensor
 sensorValue = analogRead(sensorPin);
 //Serial.println(sensorValue);
 if (sensorValue>900){ //The 'clap' sensor value is higher than 900
 randomDirection = random(0,4);
 Serial.write(randomDirection);
 Serial.println(randomDirection);
 digitalWrite(LED, HIGH); // The LED stays on for 2 seconds
 delay(2000); // The red LED stays on for 2 secondsz
} else {
 digitalWrite(LED, LOW); 
}

}

Then I found a programm on the internet (so I did not write it myself), of a moving pacman. This pacman's direction was originally controlled by the keyboard. I changed this in "randomDirection" 0-3. With the help of some tuturials on the internet I tried to do serial connection.

Programma processing

import processing.serial.*;
import cc.arduino.*;


int radius = 20;
int direction = 1;
int time;
int intervel = 300;
int randomDirection = 2;
Arduino arduino;


//time and intervel to open and close mouth 
float x = 10, y = 10, speed = 0.5; 

void setup() 
{ 
arduino = new Arduino(this, Arduino.list()[0], 57600);
 size(500,500); 
smooth(); 
ellipseMode(RADIUS);
 fill(62,1,124);
 noStroke(); 
time = millis();//number of milliseconds since program is run...
} 

void draw() 
{ 
println(Arduino.list());
 background(175); 
if((x > width - radius) || (x < radius)) 
direction = -direction;
 arc(x,y,radius,radius,0.52,5.76);
 background(175);
 frameRate(13);
 //slows it down a bit (program runs 13 times a second as opposed to 60) 
//so you can see the mouth close
 if(randomDirection == 0)// if the up arrow is pressed... 
{
 arc(x,y,radius,radius,5.2,10.6);
 if(millis() > time + intervel)
 //milliseconds plus 700 - 700 milliseconds - 
//if milliseconds is greater than 700 milliseconds
 //something will happen (every 0.7 seconds)
 {
 time = millis();
 arc(x,y,radius,radius,5.18,10.6);
 arc(x,y,radius,radius,5.08,10.7);
 arc(x,y,radius,radius,4.8,10.8);
 arc(x,y,radius,radius,4.5,11);
 ellipse(x,y,radius,radius);
 arc(x,y,radius,radius,4.5,11);
 arc(x,y,radius,radius,4.8,10.8);
 arc(x,y,radius,radius,5.08,10.7);
 //lots of different arcs with the angles geting smaller/bigger 
//to give the illusion of a mouth opening/closing
 }

 y = y - 1;
 //changes the y value of the arcs every time the draw function is run 
//so the pacman moves up on the y axis 
}
 else if(randomDirection == 1)
 {
 arc(x,y,radius,radius,8.4,13.7);
 if(millis() > time + intervel)
 {
 time = millis();
 arc(x,y,radius,radius,8.3,13.6);
 arc(x,y,radius,radius,8.2,13.5);
 arc(x,y,radius,radius,8.1,13.4);
 arc(x,y,radius,radius,8,13.3);
 arc(x,y,radius,radius,7.8,14);
 ellipse(x,y,radius,radius);
 arc(x,y,radius,radius,7.8,14);
 arc(x,y,radius,radius,8,13.3);
 arc(x,y,radius,radius,8.1,13.4);
 arc(x,y,radius,radius,8.2,13.5);
 }
 y = y + 1;
 }
 else if(randomDirection == 2)
 {
 arc(x,y,radius,radius,0.52,5.76);
 if(millis() > time + intervel)
 {
 time = millis();
 arc(x,y,radius,radius,0.52,5.76);
 arc(x,y,radius,radius,0.32,5);
 arc(x,y,radius,radius,0.12,3.5);
 arc(x,y,radius,radius,-3,0);
 ellipse(x,y,radius,radius);
 arc(x,y,radius,radius,-3,0);
 arc(x,y,radius,radius,0.12,3.5);
 arc(x,y,radius,radius,0.32,5);
 } 
x = x + 1;
 }
 else if (randomDirection == 3)
 {
 arc(x,y,radius,radius,3.67,8.90);
 if(millis() > time + intervel)
 {
 time = millis();
 arc(x,y,radius,radius,3.67,8.90);
 arc(x,y,radius,radius,3.47,9.1);
 arc(x,y,radius,radius,3.37,9.2);
 arc(x,y,radius,radius,3.17,9.4);
 arc(x,y,radius,radius,3.07,9.5);
 arc(x,y,radius,radius,3.17,9.4);
 arc(x,y,radius,radius,3.37,9.2);
 arc(x,y,radius,radius,3.47,9.1);
 }
 x = x - 1;
 } 
if(x <= 10) 
x = 10;
 if(x >= width - 10)
 x = width - 10; 
if(y >= height - 10)
 y = height - 10;
 if(y <= 10)
 y = 10;
 //that's all to make it stop when it reaches the edge of the box.
 //if y = 10 then its stationary. 
//I made it stop 10 away from the edge because 
//otherwise half of it goes through the the edge/wall. 

}

Now the point is, that when I only run the processing programma, when I make a clap-sound the LED will light up, but there does not change anything in the direction of the pacman. What am I doing wrong and how do I make it working?

Many thanks!

arduino = new Arduino(this, Arduino.list()[0], 57600);

This statement makes two assumptions. First, it assumes that the Arduino is connected to the first COM port in the list (or port of other types if you are using a smarter operating system). Have you confirmed that the Arduino is indeed connected to the first port in the list? Do you even know what is in the list?

Second, this statement presumes that a Firmata sketch is uploaded to the Arduino. That Arduino sketch doesn't look anything like a Firmata sketch.

I'm sure about the COM, not about the Firmata sketch. I actually don't even know what it is :blush:
i tried it on a second option, by using the example programm "simpleRead". It gave me the same result: only a LED which lights up, not a moving pacman.

import processing.serial.*;
Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port


int radius = 20;
int direction = 1;
int time;
int intervel = 300;
int randomDirection = 2;

//time and intervel to open and close mouth 
float x = 10, y = 10, speed = 0.5; 

void setup() 
{ 
  size(500,500); 
  smooth(); 
  ellipseMode(RADIUS);
  fill(62,1,124);
  noStroke(); 
  time = millis();//number of milliseconds since program is run...
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
} 

void draw() 
{ 
   if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();         // read it and store it in val
  }

background(175);  
  if((x > width - radius) || (x < radius)) 
     direction = -direction;
  arc(x,y,radius,radius,0.52,5.76);
    background(175);
    frameRate(13);
    //slows it down a bit (program runs 13 times a second as opposed to 60) 
    //so you can see the mouth close
    if(randomDirection == 0)// if the up arrow is pressed... 
      {
        arc(x,y,radius,radius,5.2,10.6);
        if(millis() > time + intervel)
        //milliseconds plus 700 - 700 milliseconds - 
        //if milliseconds is greater than 700 milliseconds
        //something will happen (every 0.7 seconds)
        {
          time = millis();
          arc(x,y,radius,radius,5.18,10.6);
          arc(x,y,radius,radius,5.08,10.7);
          arc(x,y,radius,radius,4.8,10.8);
          arc(x,y,radius,radius,4.5,11);
          ellipse(x,y,radius,radius);
          arc(x,y,radius,radius,4.5,11);
          arc(x,y,radius,radius,4.8,10.8);
          arc(x,y,radius,radius,5.08,10.7);
          //lots of different arcs with the angles geting smaller/bigger 
          //to give the illusion of a mouth opening/closing
        }

        y = y - 1;
        //changes the y value of the arcs every time the draw function is run 
        //so the pacman moves up on the y axis  
      }
    else if(randomDirection == 1)
      {
        arc(x,y,radius,radius,8.4,13.7);
        if(millis() > time + intervel)
        {
          time = millis();
          arc(x,y,radius,radius,8.3,13.6);
          arc(x,y,radius,radius,8.2,13.5);
          arc(x,y,radius,radius,8.1,13.4);
          arc(x,y,radius,radius,8,13.3);
          arc(x,y,radius,radius,7.8,14);
          ellipse(x,y,radius,radius);
          arc(x,y,radius,radius,7.8,14);
          arc(x,y,radius,radius,8,13.3);
          arc(x,y,radius,radius,8.1,13.4);
          arc(x,y,radius,radius,8.2,13.5);
        }
        y = y + 1;
      }
    else if(randomDirection == 2)
      {
        arc(x,y,radius,radius,0.52,5.76);
        if(millis() > time + intervel)
        {
          time = millis();
          arc(x,y,radius,radius,0.52,5.76);
          arc(x,y,radius,radius,0.32,5);
          arc(x,y,radius,radius,0.12,3.5);
          arc(x,y,radius,radius,-3,0);
          ellipse(x,y,radius,radius);
          arc(x,y,radius,radius,-3,0);
          arc(x,y,radius,radius,0.12,3.5);
          arc(x,y,radius,radius,0.32,5);
        } 
        x = x + 1;
      }
    else if (randomDirection == 3)
      {
        arc(x,y,radius,radius,3.67,8.90);
        if(millis() > time + intervel)
        {
          time = millis();
          arc(x,y,radius,radius,3.67,8.90);
          arc(x,y,radius,radius,3.47,9.1);
          arc(x,y,radius,radius,3.37,9.2);
          arc(x,y,radius,radius,3.17,9.4);
          arc(x,y,radius,radius,3.07,9.5);
          arc(x,y,radius,radius,3.17,9.4);
          arc(x,y,radius,radius,3.37,9.2);
          arc(x,y,radius,radius,3.47,9.1);
        }
        x = x - 1;
      } 
    if(x <= 10) 
      x = 10;
    if(x >= width - 10)
      x = width - 10; 
    if(y >= height - 10)
      y = height - 10;
    if(y <= 10)
      y = 10;
    //that's all to make it stop when it reaches the edge of the box.
    //if y = 10 then its stationary. 
    //I made it stop 10 away from the edge because 
    //otherwise half of it goes through the the edge/wall. 

}

This is the example programm simpleRead, which actually works on my computer (so this is why I'm sure that the COM value is right)

/**
 * Simple Read
 * 
 * Read data from the serial port and change the color of a rectangle
 * when a switch connected to a Wiring or Arduino board is pressed and released.
 * This example works with the Wiring / Arduino program that follows below.
 */


import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port

void setup() 
{
  size(200, 200);
  // 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()
{
  if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();         // read it and store it in val
  }
  background(255);             // Set background to white
  if (val == 0) {              // If the serial value is 0,
    fill(0);                   // set fill to black
  } 
  else {                       // If the serial value is not 0,
    fill(204);                 // set fill to light gray
  }
  rect(50, 50, 100, 100);
}



/*

// Wiring / Arduino Code
// Code for sensing a switch status and writing the value to the serial port.

int switchPin = 4;                       // Switch connected to pin 4

void setup() {
  pinMode(switchPin, INPUT);             // Set pin 0 as an input
  Serial.begin(9600);                    // Start serial communication at 9600 bps
}

void loop() {
  if (digitalRead(switchPin) == HIGH) {  // If switch is ON,
    Serial.write(1);               // send 1 to Processing
  } else {                               // If the switch is not ON,
    Serial.write(0);               // send 0 to Processing
  }
  delay(100);                            // Wait 100 milliseconds
}

*/

This:

import processing.serial.*;
Serial myPort;  // Create object from Serial class
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);

expects some device to be on the other end of the serial port.

This:

import processing.serial.*;
import cc.arduino.*;
arduino = new Arduino(this, Arduino.list()[0], 57600);

expects some device to be on the other end of the serial port AND expects that device to recognize specifically formatted messages and to respond appropriately. That is, it expects the device to be running a Firmata application/sketch.

If you don't know what that means, don't use this code.