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!