We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi I am very new to arduino and processing and just about understand the basics. I have a school project and I just cant get my head around it. I am using Arduino Uno
I want to use an Accelerometer (3-5V GY-61 ADXL335 Module 3-Axis Analog Output Accelerometer Angular Transducer) to control a simple processing game. So instead of the mouse curser I want the Accelerometer to control the game by tilting left and right.
This is the arduino code I uploaded to the board;
const int groundpin = 18; // analog input pin 4 -- ground const int powerpin = 19; // analog input pin 5 -- voltage const int xpin = A3; // x-axis of the accelerometer const int ypin = A2; // y-axis const int zpin = A1; // z-axis (only on 3-axis models)
void setup() { // initialize the serial communications:
Serial.begin(9600);
}
void loop() { // print the sensor values: Serial.print(analogRead(xpin));
// print a tab between values: Serial.print("\t"); Serial.print(analogRead(ypin));
// print a tab between values: Serial.print("\t"); Serial.print(analogRead(zpin)); Serial.println();
// delay before next reading: delay(100); }
And here is the processing code that I am using - its a very simple game, move the penguin to overlap as many fish as you can.
int numfish = 3; //number of fish you want in the game fish[] fishArray; //array of fish PImage bg; //background image PImage jf; //fish image PImage sb; //penguin image int fishSize = 50; //size of target fish int score = 0; //current score int timeLeft = 1000; //duration of each game (app. timeLeft / 100 in seconds) boolean gameStart = false; //if true, game is in progress PFont Font; //declaring the font used in the gameplay text int endOfGameDelayCycles = 300; //how long the end screen lasts boolean endScreenActive = false; //is the end screen currently showing? true if yes
//setting up the game, this only runs once void setup() { size(900, 700); noCursor();
//initializing the array fishArray = new fish[numfish]; for (int i = 0; i < numfish; i++) { fishArray[i] = new fish(); }
//loading the images bg = loadImage("background.jpg"); jf = loadImage("fish.png"); sb = loadImage("penguin.png"); }
//this is the loop which will keep running again and again until you stop playing void draw() { background(bg); textSize(11);
//start game menu if (gameStart == false) { fill(255);
ellipse(460, 300, 500, 500);
fill(0);
textSize(30);
text("Press 'A' to start", 220, 300);
if (keyPressed) {
if (key == 'a' || key == 'A') {
gameStart = true;
}
}
}
//while the game is on, between the start and end screen if ((gameStart == true) && (endScreenActive == false)) {
for (int i = 0; i < numfish; i++)
{
fishArray[i].drawfish();
}
movepenguin ();
fill(0);
textSize(15);
text("Score: " + score, 20, 20);
text("Time Left: " + timeLeft/100, 200, 20);
}
//end screen if (timeLeft < 10) { endScreenActive = true; fill(255); fill(255);
ellipse(460, 300, 500, 500);
fill(0);
textSize(30);
text("Well Done! Your Score is", 300, 300);
text(score, 450, 450);
endOfGameDelayCycles--;
if (endOfGameDelayCycles < 5) {
timeLeft = 2000;
endOfGameDelayCycles = 300;
gameStart = false;
score = 0;
endScreenActive = false;
}
} }
//this function moves penguin when called void movepenguin () {
//put the penguin image on the screen image(sb, mouseX, mouseY, 190, 153);
//check whether penguin is overlapping any of the fish, if so, add the score and delete the fish for (int i = 0; i < numfish; i++) { if ((abs(mouseX - fishArray[i].getXLoc()) < 35) && (abs(mouseY - fishArray[i].getYLoc()) < 35)) { fishArray[i].setLifeStatus(false); fill(224, 27, 96); score++; } } timeLeft--; }
//create a fish object class fish {
//x location, y location, how long it will stay on screen, dead (0) or alive (1) float xLoc, yLoc, lifeDuration; boolean lifeStatus;
fish() { xLoc = random(fishSize, width - (fishSize + 10)); yLoc = random(fishSize, height - (fishSize + 10));
lifeDuration = random(25, 100);
lifeStatus = true;
}
float getXLoc() { return xLoc; }
float getYLoc() { return yLoc; }
void setLifeStatus(boolean tempLifeStatus) { lifeStatus = tempLifeStatus; }
void drawfish() { lifeDuration--;
if ((lifeDuration >= 0) && (lifeStatus == true))
image(jf, xLoc, yLoc, fishSize, fishSize);
else {
xLoc = random(fishSize, width - (fishSize + 10));
yLoc = random(fishSize, height - (fishSize + 10));
lifeDuration = random(25, 100);
lifeStatus = true;
}
} }
Thank you so much for helping if anyone can I would really appreciate it. :)