Download Arduino on Raspberry Pi
1. Go to https://www.arduino.cc and download the Arduino IDE 1.6.12 for the ARM processor.
2. Open a terminal window.
3. Type cd ~/Downloads
4. Type tar -xvf arduino -1.6.12-*.tar.xz
5. Type sudo mv arduino-1.6.12 /opt
6. Type cd /opt/arduino-1.6.12/
7. Type chmod +x install.sh
8. Type ./install.sh
2. Open a terminal window.
3. Type cd ~/Downloads
4. Type tar -xvf arduino -1.6.12-*.tar.xz
5. Type sudo mv arduino-1.6.12 /opt
6. Type cd /opt/arduino-1.6.12/
7. Type chmod +x install.sh
8. Type ./install.sh
How the Arduino Joystick Works
Getting to know your Arduino Joystick
Starting to get your Arduino ready to use with Processing
Linking Processing and Arduino
If you've done everything right but your joystick does not work with the Processing game, change the port number in this part of the code. Start at 0; change the number then run the game. If it does not work increase the number by 1 and try again. Continue doing this until the joystick works or you reach the number 8. If it still does not work, something else is wrong in either processing or arduino.
|
Two player pong game with Arduino Controller
//https://forum.processing.org/two/discussion/10398/how-to-test-if-a-number-is-odd-or-even
import processing.serial.*;
Serial myPort;
String val;
float x, y, p1y, p2y, speedx, speedy;
float diam = 30;
float rectLength = 100;
float rectWidth = 10;
float p1x = 10;
float p2x = width-10-rectWidth;
int p1points, p2points;
void setup(){
fullScreen();
fill(0,255,0);
reset();
p1points = 0;
p2points = 0;
p1y = height/2;
p2y = height/2;
String portName = Serial.list()[4];
myPort = new Serial(this,portName,9600);
}
void reset(){
x = width/2;
y = height/2;
speedx = random(3,7);
//speedx *= start;
speedy = random(3,7);
}
void draw(){
background(0);
fill(172, 129, 232);
text("Player 2 Score: ", width/2-50, height/2+50);
text(p2points, width/2+40, height/2+50);
fill(247, 133, 175);
text("Player 1 Score: ", width/2-50, height/2-50);
text(p1points, width/2+40, height/2-50);
if (myPort.available() > 0){
val = myPort.readStringUntil('\n');
if (val != null){
val = trim(val);
if (val.equals("p1up") == true){
p1y -= 20;
}
if (val.equals("p1down") == true){
p1y += 20;
}
if (val.equals("p2up") == true){
p2y -= 20;
}
if (val.equals("p2down") == true){
p2y += 20;
}
}
}
fill(255,255,255);
ellipse(x,y,diam,diam);
fill(172, 129, 232);
rect(width-30,p2y,rectWidth,rectLength);
fill(247, 133, 175);
rect(20,p1y,rectWidth,rectLength);
x += speedx;
y += speedy;
if ((x>width-40) && (x < width-30) && (y<p2y+rectLength) && (y>p2y)){
speedx = speedx*-1;
}
if ((x<40) && (x>30) && (y<p1y+rectLength) && (y>p1y)){
speedx = speedx*-1;
}
if (y>height || y<0){
speedy *= -1;
}
if (x<20){
p2points += 1;
reset();
}
if (x>width-10){
p1points +=1;
reset();
}
}
void mousePressed(){
reset();
}
import processing.serial.*;
Serial myPort;
String val;
float x, y, p1y, p2y, speedx, speedy;
float diam = 30;
float rectLength = 100;
float rectWidth = 10;
float p1x = 10;
float p2x = width-10-rectWidth;
int p1points, p2points;
void setup(){
fullScreen();
fill(0,255,0);
reset();
p1points = 0;
p2points = 0;
p1y = height/2;
p2y = height/2;
String portName = Serial.list()[4];
myPort = new Serial(this,portName,9600);
}
void reset(){
x = width/2;
y = height/2;
speedx = random(3,7);
//speedx *= start;
speedy = random(3,7);
}
void draw(){
background(0);
fill(172, 129, 232);
text("Player 2 Score: ", width/2-50, height/2+50);
text(p2points, width/2+40, height/2+50);
fill(247, 133, 175);
text("Player 1 Score: ", width/2-50, height/2-50);
text(p1points, width/2+40, height/2-50);
if (myPort.available() > 0){
val = myPort.readStringUntil('\n');
if (val != null){
val = trim(val);
if (val.equals("p1up") == true){
p1y -= 20;
}
if (val.equals("p1down") == true){
p1y += 20;
}
if (val.equals("p2up") == true){
p2y -= 20;
}
if (val.equals("p2down") == true){
p2y += 20;
}
}
}
fill(255,255,255);
ellipse(x,y,diam,diam);
fill(172, 129, 232);
rect(width-30,p2y,rectWidth,rectLength);
fill(247, 133, 175);
rect(20,p1y,rectWidth,rectLength);
x += speedx;
y += speedy;
if ((x>width-40) && (x < width-30) && (y<p2y+rectLength) && (y>p2y)){
speedx = speedx*-1;
}
if ((x<40) && (x>30) && (y<p1y+rectLength) && (y>p1y)){
speedx = speedx*-1;
}
if (y>height || y<0){
speedy *= -1;
}
if (x<20){
p2points += 1;
reset();
}
if (x>width-10){
p1points +=1;
reset();
}
}
void mousePressed(){
reset();
}