Month: November 2015
Solo Pong Joystick
For this week’s assignment, I took two of my earlier assignments and connected them through the magic of serial communication. I modified the code for Solo Pong to read information sent by my arduino, hence enabling me to control the game through a physical joystick controller. 😀
Wannabe Mateo
I made a snake game.
You push the buttons to move the snake and try not to die.
Also there’s a cool space background just to make things more interesting.
import processing.serial.*; Serial myPort; ArrayList<Sphere> spheres; int xPos = 400; int yPos = 300; int Xspeed = 1; int Yspeed; int diam = 10; float [] coordinatesX = {}; float [] coordinatesY = {}; int game = 0; void setup(){ size(800,600); background(30); fill(255); String portName = "/dev/tty.usbserial-DA01LJI0"; myPort = new Serial(this,portName, 9600); myPort.bufferUntil('\n'); spheres = new ArrayList<Sphere>(); } void draw(){ background(30); spheres.add(new Sphere()); for (int i = spheres.size()-1; i>=0; i--) { // manipulate the objects Sphere s = spheres.get(i); s.update(); s.render(); //s.bounds(); if (s.isDead()) { spheres.remove(i); } } game = 1; if(game == 1){ fill(255); stroke(255); xPos = constrain(xPos,0,800); yPos = constrain(yPos,0,600); xPos += Xspeed; yPos += Yspeed; for (int i = 0; i < coordinatesX.length; i++){ rect(coordinatesX[i],coordinatesY[i],diam,diam); if ((xPos == coordinatesX[i] && yPos == coordinatesY[i]) || xPos == 0 || xPos == 800 || yPos == 0 || yPos == 600 ){ game = 2; } } rect(xPos,yPos,diam,diam); //'UP' command coordinatesX = append(coordinatesX,int(xPos)); coordinatesY = append(coordinatesY,int(yPos)); if(game == 2){ background(30); textSize(20); text("Bruh you dead.",200,height/2); yPos = 0; xPos = 0; fill(30); } } } void serialEvent(Serial myPort) { String input = myPort.readString(); String[] numbers = split(input, ','); float[] values = float(numbers); //'UP' command if(values[2] == 1){ Yspeed= -3; Xspeed=0; } //'DOWN' command if(values[1] == 1){ Yspeed = 3; Xspeed = 0; } //'LEFT' command if(values[3] == 1){ Yspeed = 0; Xspeed = -3; } if(values[0] == 1){ Yspeed = 0; Xspeed = 3; } myPort.write('x'); } class Sphere { // variables for the object float xPos; float yPos; float xSpeed; float ySpeed; float diam; float alpha; Sphere() { alpha = random(250, 255); xPos = 400; yPos = 300; xSpeed = random(-7.,7.); ySpeed = random(-7.,7.); diam = 100; } boolean isDead() { if (alpha <= 0) { return true; } else { return false; } } // declare all my functions void render() { alpha -=3; diam--; fill(210, alpha); //for(int i = 0; i<100;i = i+5){ ellipse(xPos, yPos, xSpeed, ySpeed ); // ellipse(xPos-i,yPos+i,xSpeed,ySpeed); // ellipse(xPos+i,yPos-i,xSpeed,ySpeed); // ellipse(xPos-i,yPos-i,xSpeed,ySpeed); // } if (mousePressed == true){ for(int i = 0; i<100;i = i+50){ ellipse(xPos+i, yPos+i, xSpeed, ySpeed ); ellipse(xPos-i,yPos+i,xSpeed,ySpeed); ellipse(xPos+i,yPos-i,xSpeed,ySpeed); ellipse(xPos-i,yPos-i,xSpeed,ySpeed); } } } void update() { xPos-=xSpeed; yPos-=ySpeed; } void bounds() { if (xPos >= width || xPos <= 0) { xSpeed = xSpeed * -1; } if (yPos >= height || yPos <= 0) { ySpeed = ySpeed * -1; } } }
This is the code for Arduino:
void setup() { // put your setup code here, to run once: pinMode(2,INPUT); //GREEN pinMode(3,INPUT); //YELLOW pinMode(5,INPUT); //RED pinMode(7,INPUT); //BLUE Serial.begin(9600); Serial.println("0,0,0,0"); } void loop() { // put your main code here, to run repeatedly: if (Serial.available() > 0) { int processing = Serial.read(); Serial.print(digitalRead(2)); Serial.print(","); delay(1); Serial.print(digitalRead(3)); Serial.print(","); delay(1); Serial.print(digitalRead(5)); Serial.print(","); delay(1); Serial.println(digitalRead(7)); delay(1); } }
Arduino // Processing
I made a project wherein a push button connected to Arduino controls the size of a pair of circles in Processing.
Photocell Controller
I decided to use photocell sensors as controllers for my game!
Pot control spin&viz
My physical controller for a previous processing sketch consists of one single potentiometer. I chose to use a pot primarily because it is more stable than many sensors, and it’s handy. The sketch I used is the spinning INTER letters. This time, instead of having the letters rotate by themselves, the pot would control their rotation speed and visibility.
Video:
FFTs and force sensors
For this project I recycled some code from an earlier project. Instead of distorting the webcam image using the position of the mouse on the screen, I input from a force sensor. One can also turn the Fourier transform plot on using a switch. Below is a .gif showing the images on the screen. I also have two photos of the physical setup. I would have taken a video but my phone doesn’t have any space left 🙁
For those who are interested, I’m actually using the method I’m employing in my Processing sketch for my capstone! By cutting away high frequency channels in the frequency-domain matrix, one can extract features. Even when I’m using about 100th of the original data, one can still see some of the main features in the spacial-domain image. In my capstone, I’m using this method to get rid of noise in signals from a dark matter detector.
Diverse use of the pulse sensor
I did a simple example of getting data from the pulse sensor and then using the value of the heartbeat value as the size of the radius of the ellipses. The color is also determined using the heartbeat value. I thought it was a little funny when I jokingly put the pulse sensor against my speakers. It was creating a visual representation of the beat of the music.
Physical Controller
I made a physical controller for my maze game. There are four momentary switches and one push button connected to the breadboard. The four momentary switches are used to control the direction of the dot moves, and the push button controls the fight mode. If I had a large breadboard, I would connect more buttons so that two players can play at the same time.
Alphabets Fountain
Here I used the code from the class where we made lots of colourful spheres coming out, and I replaced the spheres to alphabets. The result looks like below.