Created
April 16, 2020 06:33
-
-
Save todocono/51601e2a6567170038aa660a94a621e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* IxLab 2020S - NYU Shanghai | |
* Coded in class 21 to introduce serial communication with multiple variables | |
* Ideas for exercises to practice: | |
* add a start button | |
* make an LED turn on when it's game over | |
* This code uses sources such as: | |
* Based on the readStringUntil() example by Tom Igoe | |
* https://processing.org/reference/libraries/serial/Serial_readStringUntil_.html | |
* Serial Communication Example by Jiwon at NYUSH IMA | |
* https://wp.nyu.edu/shanghai-ima-interaction-lab/ | |
* | |
*/ | |
import processing.serial.*; | |
String myString = null; | |
Serial myPort; | |
float x, y; | |
float spdX, spdY; | |
int paddle = 100; | |
int NUM_OF_VALUES = 2; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/ | |
int[] sensorValues; /** this array stores values from Arduino **/ | |
void setup() { | |
size(500, 500); | |
background(0); | |
setupSerial(); | |
start(); | |
rectMode(CORNERS); | |
} | |
void draw() { | |
updateSerial(); | |
printArray(sensorValues); | |
// use the values like this! | |
// sensorValues[0] | |
// add your code | |
background(0); | |
fill(125, 125, 0); | |
ellipse(x, y, 25, 25); | |
x = x + spdX; | |
y = y + spdY; | |
fill(255); | |
float p1 = map(sensorValues[0], 0, 1023, 0, height); | |
float p2 = map(sensorValues[1], 0, 1023, 0, height); | |
rect(0, p1, 20, p1+paddle); | |
rect(width-20, p2, width, p2+paddle); | |
//bounce on Y axis | |
if ( y < 12 || y> height-12) { | |
spdY = -spdY; | |
} | |
//if p1 or p2 are withing the collision of ball, bounce | |
if ( x < 12 ) { | |
if ( (y < p1+paddle) && (y > p1)) { | |
spdX = -spdX; | |
} else { | |
println("p2 score +1"); | |
start(); | |
} | |
} | |
if ( x > width - 12 ) { | |
if ( (y < p2+paddle) && (y > p2)) { | |
spdX = -spdX; | |
} else { | |
println("p2 score +1"); | |
start(); | |
} | |
} | |
} | |
void start() { | |
x = width/2; | |
y = height/2; | |
while ( millis() < 2000) { | |
println("wait..."); | |
} | |
println("go!"); | |
spdX = random (-5, 5); | |
spdY = random (-5, 5); | |
} | |
void setupSerial() { | |
printArray(Serial.list()); | |
myPort = new Serial(this, Serial.list()[ 3 ], 115200); | |
// WARNING! | |
// You will definitely get an error here. | |
// Change the PORT_INDEX to 0 and try running it again. | |
// And then, check the list of the ports, | |
// find the port "/dev/cu.usbmodem----" or "/dev/tty.usbmodem----" | |
// and replace PORT_INDEX above with the index number of the port. | |
myPort.clear(); | |
// Throw out the first reading, | |
// in case we started reading in the middle of a string from the sender. | |
myString = myPort.readStringUntil( 10 ); // 10 = '\n' Linefeed in ASCII | |
myString = null; | |
sensorValues = new int[NUM_OF_VALUES]; | |
} | |
void updateSerial() { | |
while (myPort.available() > 0) { | |
myString = myPort.readStringUntil( 10 ); // 10 = '\n' Linefeed in ASCII | |
if (myString != null) { | |
String[] serialInArray = split(trim(myString), ","); | |
if (serialInArray.length == NUM_OF_VALUES) { | |
for (int i=0; i<serialInArray.length; i++) { | |
sensorValues[i] = int(serialInArray[i]); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment