/*
* Simple Pong
*
*
* Play one-person pong
*
* Receives an ASCII number over the serial port,
* terminated with a carriage return (ascii 13) then newline (10).
*
* This matches what Arduino's " Serial.println(val)" function
* puts out.
*
* Created 27 September 2011 by Justin Wang
*/
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/tty.usbmodem641"; //"/dev/tty.usbserial-A4001nLJ"; // or "COM5"
Serial port;
String buf="";
// variables
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
int size = 60; // Width of the shape
float xpos, ypos; // Starting position of shape
float xspeed = 10; // Speed of the shape
float yspeed = 10; // Speed of the shape
int xdirection = 1; // Left or Right
int ydirection = 1; // Top to Bottom
int rectwidth = 60;
int rectheight = 10;
int xsize = 600;
int ysize = 400;
float rectpos = 0;
/*
int rect1xpos = 0;
int rect1ypos = ysize/2 - rectheight/2;
int rect2xpos = xsize - rectwidth;
int rect2ypos = ysize/2 - rectheight/2;
int score1 = 0;
int score2 = 0;
int totalscore = 0;
*/
void setup()
{
size(xsize, ysize);
noStroke();
frameRate(30);
smooth();
// Set the starting position of the shape
xpos = 0;
ypos = 0;
/*
background(102);*/
port = new Serial(this, portname, 9600);
}
void draw()
{
background(102);
// Update the position of the shape
xpos = xpos + ( xspeed * xdirection );
ypos = ypos + ( yspeed * ydirection );
// Test to see if ball hits a player
// If it does, reverse its y-direction by multiplying by -1
// Left side, player 1
if (ydirection==1 && (ypos>(height-(rectheight+size))) && (ypos < (height-size/2)) && (abs(xpos-rectpos) < 30)) {
ydirection *= -1;
}
println("begin");
println(ydirection);
println(ypos);
println(height-(rectheight+size));
println(xpos);
println(rectpos+(rectwidth/2));
println(rectpos-(rectwidth/2));
// Test to see if ball exceeds the y-boundary of the screen
// If it does, reverse its direction by multiplying by -1
if (xpos > width-size || xpos < 0) {
xdirection *= -1;
}
if (/*ypos > height-size ||*/ ypos < 0) {
ydirection *= -1;
}
// Draw the player
rect(rectpos, height-rectheight, rectwidth, rectheight);
// Draw the ball
if (ypos>height+60) { // restart
xpos = 0;
ypos = 0;
xdirection = 1;
ydirection = 1;
}
// else, draw ellipse in updated position
ellipse(xpos+size/2, ypos+size/2, size, size);
}
// called whenever serial data arrives
void serialEvent(Serial p) {
int c = port.read();
if (c != lf && c != cr) {
buf += char(c);
}
if (c == lf) {
float val = int(buf);
val = map(val,0,1023,0,540);
rectpos = val;
//println(val);
buf = "";
}
}
*/
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/tty.usbmodem641"; //"/dev/tty.usbserial-A4001nLJ"; // or "COM5"
Serial port;
String buf="";
// variables
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
int size = 60; // Width of the shape
float xpos, ypos; // Starting position of shape
float xspeed = 10; // Speed of the shape
float yspeed = 10; // Speed of the shape
int xdirection = 1; // Left or Right
int ydirection = 1; // Top to Bottom
int rectwidth = 60;
int rectheight = 10;
int xsize = 600;
int ysize = 400;
float rectpos = 0;
/*
int rect1xpos = 0;
int rect1ypos = ysize/2 - rectheight/2;
int rect2xpos = xsize - rectwidth;
int rect2ypos = ysize/2 - rectheight/2;
int score1 = 0;
int score2 = 0;
int totalscore = 0;
*/
void setup()
{
size(xsize, ysize);
noStroke();
frameRate(30);
smooth();
// Set the starting position of the shape
xpos = 0;
ypos = 0;
/*
background(102);*/
port = new Serial(this, portname, 9600);
}
void draw()
{
background(102);
// Update the position of the shape
xpos = xpos + ( xspeed * xdirection );
ypos = ypos + ( yspeed * ydirection );
// Test to see if ball hits a player
// If it does, reverse its y-direction by multiplying by -1
// Left side, player 1
if (ydirection==1 && (ypos>(height-(rectheight+size))) && (ypos < (height-size/2)) && (abs(xpos-rectpos) < 30)) {
ydirection *= -1;
}
println("begin");
println(ydirection);
println(ypos);
println(height-(rectheight+size));
println(xpos);
println(rectpos+(rectwidth/2));
println(rectpos-(rectwidth/2));
// Test to see if ball exceeds the y-boundary of the screen
// If it does, reverse its direction by multiplying by -1
if (xpos > width-size || xpos < 0) {
xdirection *= -1;
}
if (/*ypos > height-size ||*/ ypos < 0) {
ydirection *= -1;
}
// Draw the player
rect(rectpos, height-rectheight, rectwidth, rectheight);
// Draw the ball
if (ypos>height+60) { // restart
xpos = 0;
ypos = 0;
xdirection = 1;
ydirection = 1;
}
// else, draw ellipse in updated position
ellipse(xpos+size/2, ypos+size/2, size, size);
}
// called whenever serial data arrives
void serialEvent(Serial p) {
int c = port.read();
if (c != lf && c != cr) {
buf += char(c);
}
if (c == lf) {
float val = int(buf);
val = map(val,0,1023,0,540);
rectpos = val;
//println(val);
buf = "";
}
}
/* Arduino Code */
/*
* one pot fades one led
* modified version of AnalogInput
* by DojoDave <http://www.0j0.org>
* http://www.arduino.cc/en/Tutorial/AnalogInput
*/
int potPin = 0; // select the input pin for the potentiometer
int ledPin1 = 9; // select the pin for the LED
int ledPin2 = 10; // select the pin for the LED
int ledPin3 = 11; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
int newVal = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
val = analogRead(potPin); // read the value from the sensor, between 0 - 1024
newVal = map(val, 0, 1023, 0, 255); // remap value
Serial.println(val);
analogWrite(ledPin1, newVal); // analogWrite can be between 0-255
analogWrite(ledPin2, newVal); // analogWrite can be between 0-255
analogWrite(ledPin3, newVal); // analogWrite can be between 0-255
}