After a whole bunch of brainstorming I’ve realized that the project that I’d really like to build is a small cabinet for the pong game I’ve been eking out. I kept trying to come up with ideas for something that’s more artistic and cool, but I kept coming back to this project. So, I guess the best thing to do is just embrace it.
So far the semester has felt like a whole bunch of bits and pieces. I think what really interests me about building this is bringing everything together and solidifying what I have learned in PComp and ICM so far.
I wanted to make some controllers for the pong game I’ve been plugging away at for ICM. Constructing the prototypes was pretty straight forward, but getting the code to work has been a whole other thing.
I had some slide pots already from a previous project, so all I really needed to do was make a housing for them. Cardboard is free, and I actually quite like it as a material.
The Arduino code seems to work just fine:
void setup() {
Serial.begin(9600);
}
void loop() {
//send two things separated by a commma
int slideOne = analogRead(A0); // read the input pin
int mappedSlideOne = map(slideOne, 0, 1023, 0, 255); // remap the pot value to fit in 1 byte
int slideTwo = analogRead(A5); // read the input pin
int mappedSlideTwo = map(slideTwo, 0, 1023, 0, 255); // remap the pot value to fit in 1 byte
Serial.print(mappedSlideOne); // print it out the serial port
Serial.print(",");
Serial.println(mappedSlideTwo);
delay(1);
}
But the p5.js code keeps coming up ‘undefined’. I can’t figure out what’s wrong with the code. After I’ve gotten that worked out it should be pretty easy to drop it into the pong code I’ve already been working on.
var serial; // variable to hold an instance of the serialport library
var portName = '/dev/cu.usbmodem1421'; // fill in your serial port name here
var inData; // for incoming serial data
var myData;
var input;
var input2;
var inString;
function setup() {
serial = new p5.SerialPort(); // make a new instance of the serialport library
serial.on('list', printList); // set a callback function for the serialport list event
serial.on('connected', serverConnected); // callback for connecting to the server
serial.on('open', portOpen); // callback for the port opening
serial.on('data', serialEvent); // callback for when new data arrives
serial.on('error', serialError); // callback for errors
serial.on('close', portClose); // callback for the port closing
serial.open(portName); // open a serial port
serial.list(); // list the serial ports
createCanvas(400, 300);
}
function draw() {
background(0);
fill(255);
//text("sensor value: " + input + "," + input2, 30, 30);
text("sensor value: " + input, 30, 30);
text("hello world", 30, 50);
print(inString);
}
function serverConnected() {
println('connected to server.');
}
function portOpen() {
println('the serial port opened.');
}
function serialEvent() {
inString = serial.readStringUntil('\r\n');
if (inString.length > 0) {
var parts = inString.split(","); //split the incoming into an array based on the comma
input = int(parts[0]);
input2 = int(parts[1]);
print(input2 + "Got data" + input);
}
}
function serialError(err) {
println('Something went wrong with the serial port. ' + err);
}
function portClose() {
println('The serial port closed.');
}
// get the list of ports:
function printList(portList) {
// portList is an array of serial port names
for (var i = 0; i < portList.length; i++) {
// Display the list the console:
println(i + " " + portList[i]);
}
}
I wanted to make a whole bunch of bouncing balls, so that eventually I can add them back into the pong game. I ran into a couple of problems
Too many circles:
I banged my head against this one for a while, until I remembered that we actually did a bouncing ball example in class. I pulled up that code and compared it to mine. Turns out I was creating circles in draw as well as setup, so I was getting way more circles than I wanted. I removed that line of code and everything was good
Balls disappear:
I added some acceleration to the balls, so after they hit the ‘walls’ they would go faster. After I did this the balls would eventually disappear. Maybe they were going faster than the frame rate?
Stuff that would be cool to add to bouncing balls:
Balls bounce off each other.
Balls are added every second, rather than all at once.
Jesse sent me the code for his evil peeps, so I added my bouncing and moving code to the peeps. I’m sure that there’s a lot in the code that doesn’t need to be, since my method was basically to paste in his code and then delete things until it worked correctly. That basically worked.
My bouncing ball code:
var circles = [];
//var circle;
function setup() {
createCanvas(1000, 700);
for (var i = 0; i <= 100; i++) {
circles[i] = new Ball();
//circles[i].move();
//circles[i].display();
}
}
function draw() {
background(64, 161, 76);
//if (millis() < 5000){
fill(198, 237, 44);
for (var i = 0; i <= 100; i++) {
//circles[i] = new Ball();
circles[i].move();
circles[i].display();
circles[i].bounce();
print("check");
print(circles.length);
//}
}
/*for (var i = 0; i < 100; i++) { circles[i].move(); circles[i].display(); }*/ } function Ball() { this.xpos = random(width); this.ypos = random(height); this.xdir = random (-5, 5); this.ydir = random (-5, 5); this.display = function() { noStroke(); ellipse(this.xpos, this.ypos, 20, 20); } this.move = function() { this.xpos = this.xpos + this.xdir; this.ypos = this.ypos + this.ydir; } this.bounce = function() { if (this.xpos >= width || this.xpos <= 0) { this.xdir = this.xdir * -1 } if (this.ypos >= height || this.ypos <= 0) {
this.ydir = this.ydir * -1
}
}
}