Last active
December 11, 2015 08:58
-
-
Save mbparks/4576955 to your computer and use it in GitHub Desktop.
The Wiring code for the Arduino Esplora. Reads the Joystick X, Y, and Joystick Button. As well as the Up, Down, Left, and Right buttons. Then sets status to Processing sketch via Serial communication. Cat and Mouse is a two player game where my one player uses the Esplora and acts as the cat. The other user uses the computer's mouse and plays th…
This file contains hidden or 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
////////////////////////////////////////////// | |
// CAT AND MOUSE GAME | |
// Author: Mike Parks | |
// | |
////////////////////////////////////////////// | |
// INCLUDE LIBRARIES | |
#include <Esplora.h> | |
// DECLARE GLOBAL VARIABLES | |
int Xvalue = 0; | |
int Yvalue = 0; | |
int JbuttonStatus = 1023; | |
int upButtonStatus = 1; | |
int downButtonStatus = 1; | |
int rightButtonStatus = 1; | |
int leftButtonStatus = 1; | |
// FOR DEBOUNCING INPUTS | |
unsigned long ButtonTime[4] = {0,0,0,0}; //DOWN = 0, LEFT = 1, UP = 2, RIGHT = 3 | |
unsigned long ButtonTime_Last[4] = {0,0,0,0}; | |
unsigned long debounceTime = 175; | |
// | |
// SETUP ROUTINE | |
// | |
void setup(){ | |
Serial.begin(9600); | |
} | |
// | |
// MAIN LOOP | |
// | |
void loop() | |
{ | |
// READ SENSORS | |
Xvalue = Esplora.readJoystickX(); | |
Yvalue = Esplora.readJoystickY(); | |
downButtonStatus = checkButtonStatus(SWITCH_DOWN, 0); | |
leftButtonStatus = checkButtonStatus(SWITCH_LEFT, 1); | |
upButtonStatus = checkButtonStatus(SWITCH_UP, 2); | |
rightButtonStatus = checkButtonStatus(SWITCH_RIGHT, 3); | |
JbuttonStatus = Esplora.readJoystickSwitch(); | |
// TRANSMIT SENSOR DATA TO PROCESSING | |
// VIA SERIAL COMMUNICATIONS LINK | |
Serial.print(Xvalue); | |
Serial.print(","); | |
Serial.print(Yvalue); | |
Serial.print(","); | |
Serial.print(JbuttonStatus); | |
Serial.print(","); | |
Serial.print(upButtonStatus); | |
Serial.print(","); | |
Serial.print(downButtonStatus); | |
Serial.print(","); | |
Serial.print(rightButtonStatus); | |
Serial.print(","); | |
Serial.println(leftButtonStatus); | |
// LET THINGS SETTLE | |
delay(30); | |
} | |
// | |
// READ AND DEBOUNCE THE Up, DOWN, LEFT, RIGHT BUTTONS | |
// | |
int checkButtonStatus(int buttonID, int arrayID) { | |
int buttonStatus = Esplora.readButton(buttonID); | |
if (buttonStatus == 0) { | |
ButtonTime[arrayID] = millis(); | |
if (ButtonTime[arrayID] - ButtonTime_Last[arrayID] > debounceTime) { | |
ButtonTime_Last[arrayID] = ButtonTime[arrayID]; | |
return buttonStatus; | |
} | |
else { | |
return 1; | |
} | |
} | |
else { | |
return 1; | |
} | |
} |
This file contains hidden or 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
////////////////////////////////////////////// | |
// CAT AND MOUSE GAME | |
// Author: Mike Parks | |
// | |
////////////////////////////////////////////// | |
// INCLUDE LIBRARIES | |
import processing.serial.*; | |
// DECLARE GLOBAL VARIABLES | |
Serial myPort; | |
int linefeed = 10; | |
int XVal = 0; | |
int YVal = 0; | |
int JbuttonPress = 1; | |
int upButtonPress = 1; | |
int downButtonPress = 1; | |
int rightButtonPress = 1; | |
int leftButtonPress = 1; | |
int defaultBackgroundR = 0; | |
int defaultBackgroundG = 100; | |
int defaultBackgroundB = 0; | |
int cursorSizeX = 10; | |
int cursorSizeY = 10; | |
int cursorAdjustX = 510; | |
int cursorAdjustY = 510; | |
int mouseScore = 0; | |
int catScore = 0; | |
int catPoints = 1; | |
boolean debug = true; | |
// | |
// SETUP ROUTINE | |
// | |
void setup() { | |
size(1030, 1030); | |
println(Serial.list()); | |
myPort = new Serial(this, Serial.list()[6], 9600); | |
background(defaultBackgroundR, defaultBackgroundG, defaultBackgroundB); | |
} | |
// | |
// MAIN LOOP | |
// | |
void draw() { | |
if (JbuttonPress == 0) { // Cat made an attack attempt | |
JButtonDetect(); | |
} | |
else { // No attack made this pass | |
background(defaultBackgroundR, defaultBackgroundG, defaultBackgroundB); | |
} | |
checkButtons(); // Check Up, Down, Left, Right buttons and take action as needed | |
refreshScreen(); // Update the output screen as needed | |
if (debug) { // Print debug data onto serial port if needed | |
printDebug(); | |
} | |
} | |
// | |
// DETECT INPUT DATA PACKET FROM ARDUINO SERIAL PORT | |
// | |
void serialEvent(Serial myPort) { | |
String inString = myPort.readStringUntil('\n'); | |
if (inString != null) { | |
inString = trim(inString); | |
int sensors[] = int(split(inString, ',')); | |
if (sensors.length == 7) { | |
XVal = (-sensors[0])+cursorAdjustX; | |
YVal = sensors[1]+cursorAdjustY; | |
JbuttonPress = sensors[2]; | |
upButtonPress = sensors[3]; | |
downButtonPress = sensors[4]; | |
rightButtonPress = sensors[5]; | |
leftButtonPress = sensors[6]; | |
} | |
} | |
} | |
// | |
// Determine if cat has hit the mouse | |
// | |
void JButtonDetect() { | |
background(255, 0, 0); | |
if (mouseX <= (XVal + cursorSizeX) && mouseX >= XVal) { | |
if (mouseY <= (YVal + cursorSizeY) && mouseY >= YVal) { | |
textSize(32); | |
text("HIT!", 10, 30); | |
fill(0, 102, 153); | |
catPoints = (200/cursorSizeX); | |
catScore += catPoints; | |
} | |
else { | |
textSize(32); | |
text("MISS!", 10, 30); | |
fill(0, 102, 153); | |
mouseScore += 1; | |
} | |
} | |
else { | |
textSize(32); | |
text("MISS!", 10, 30); | |
fill(0, 102, 153); | |
mouseScore += 1; | |
} | |
} | |
// | |
// Check Up, Down, Left, Right Buttons | |
// | |
void checkButtons() { | |
// | |
// ADJUST CURSOR SIZE | |
// | |
if (upButtonPress == 0 && cursorSizeX < 100) { | |
cursorSizeX += 10; | |
cursorSizeY += 10; | |
cursorAdjustX -= 5; | |
cursorAdjustY -= 5; | |
} | |
if (downButtonPress == 0 && cursorSizeX > 10) { | |
cursorSizeX -= 10; | |
cursorSizeY -= 10; | |
cursorAdjustX += 5; | |
cursorAdjustY += 5; | |
} | |
// | |
// ADJUST SCREEN COLOR | |
// | |
if (leftButtonPress == 0 && defaultBackgroundG > 10) { | |
defaultBackgroundG -= 10; | |
} | |
if (rightButtonPress == 0 && defaultBackgroundG < 250) { | |
defaultBackgroundG += 10; | |
} | |
} | |
// | |
// REFRESH SCREEN | |
// | |
void refreshScreen() { | |
rect(XVal, YVal, cursorSizeX, cursorSizeY); | |
textSize(32); | |
text("MOUSE SCORE: ", 100, 30); | |
text(mouseScore, 350, 30); | |
text("CAT SCORE: ", 600, 30); | |
text(catScore, 800, 30); | |
fill(255, 255, 255); | |
} | |
// | |
// DEBUGGING RAW SERIAL DATA | |
// | |
void printDebug() { | |
print("CursorSize: "); | |
println(cursorSizeX); | |
print("X, Xmax: "); | |
print(XVal); | |
print(" "); | |
println(XVal + cursorSizeX); | |
print("Y, Ymax: "); | |
print(YVal); | |
print(" "); | |
println(YVal + cursorSizeY); | |
print("Mouse X: "); | |
print(mouseX); | |
print(" "); | |
print("Mouse Y: "); | |
println(mouseY); | |
print("JB U/D/R/L: "); | |
print(JbuttonPress); | |
print(" "); | |
print(upButtonPress); | |
print(downButtonPress); | |
print(rightButtonPress); | |
println(leftButtonPress); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment