Created
April 18, 2017 16:36
-
-
Save giantmolecules/a367bf478ca926450ed52ab5b7915f8c to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* Project UNITY-CONTROLLER-FIRMWARE | |
* Description: UDP controller for Unity | |
* Author: Brett Ian Balogh | |
* Email: brettbalogh@gmail.com | |
* Date: April 11, 2017 | |
* License: CC-BY-SA-NC | |
*/ | |
// Include Libraries | |
#include <Particle.h> | |
#include <neopixel.h> | |
#include <simple-OSC.h> | |
#include <Wire.h> | |
#include <Adafruit_MMA8451.h> | |
#include <Adafruit_Sensor.h> | |
#include <Adafruit_DRV2605.h> | |
#define PIXEL_PIN D6 | |
#define PIXEL_COUNT 5 | |
#define PIXEL_TYPE WS2812B | |
#define TEMP_PIN A0 | |
#define LIGHT_PIN A1 | |
#define PIR_PIN D2 | |
#define PIEZO_PIN D3 | |
#define STATUS_PIN 7 | |
Adafruit_DRV2605 drv; | |
Adafruit_MMA8451 mma = Adafruit_MMA8451(); | |
Adafruit_NeoPixel neo(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE); | |
double accelX = 0; | |
double accelY = 0; | |
double accelZ = 0; | |
String localIpString; | |
int theTime; | |
Timer publishTimer(2000, timedPublish); | |
UDP Udp; | |
IPAddress outIp(192, 168, 105, 147);//your computer IP | |
unsigned int outPort = 8000; //computer incoming port | |
void setup() { | |
Serial.begin(9600); | |
Serial.println("Adafruit MMA8451 test!"); | |
if (! mma.begin()) { | |
Serial.println("Couldnt start"); | |
while (1); | |
} | |
Serial.println("MMA8451 found!"); | |
mma.setRange(MMA8451_RANGE_2_G); | |
Serial.print("Range = "); Serial.print(2 << mma.getRange()); | |
Serial.println("G"); | |
Particle.variable("X", accelX); | |
Particle.variable("Y", accelY); | |
Particle.variable("Z", accelZ); | |
localIpString = WiFi.localIP(); | |
Udp.begin(0); | |
pinMode(STATUS_PIN, OUTPUT); | |
Particle.function("setIP", setIP); | |
Particle.variable("time", theTime); | |
Particle.variable("localIP", localIpString); | |
publishTimer.start(); | |
RGB.control(true); | |
RGB.brightness(255); | |
} | |
// loop() runs over and over again, as quickly as it can execute. | |
void loop() { | |
theTime = millis(); | |
// Read the 'raw' data in 14-bit counts | |
mma.read(); | |
Serial.print("X:\t"); Serial.print(mma.x); | |
Serial.print("\tY:\t"); Serial.print(mma.y); | |
Serial.print("\tZ:\t"); Serial.print(mma.z); | |
Serial.println(); | |
/* Get a new sensor event */ | |
sensors_event_t event; | |
mma.getEvent(&event); | |
accelX = event.acceleration.x; | |
accelY = event.acceleration.y; | |
accelZ = event.acceleration.z; | |
/* Display the results (acceleration is measured in m/s^2) */ | |
Serial.print("X: \t"); Serial.print(event.acceleration.x); Serial.print("\t"); | |
Serial.print("Y: \t"); Serial.print(event.acceleration.y); Serial.print("\t"); | |
Serial.print("Z: \t"); Serial.print(event.acceleration.z); Serial.print("\t"); | |
Serial.println("m/s^2 "); | |
/* Get the orientation of the sensor */ | |
uint8_t o = mma.getOrientation(); | |
switch (o) { | |
case MMA8451_PL_PUF: | |
Serial.println("Portrait Up Front"); | |
sendAccelToUnity(1); | |
// RED | |
RGB.color(255,0,0); | |
break; | |
case MMA8451_PL_PUB: | |
Serial.println("Portrait Up Back"); | |
sendAccelToUnity(2); | |
// GREEN | |
RGB.color(0,255,0); | |
break; | |
case MMA8451_PL_PDF: | |
Serial.println("Portrait Down Front"); | |
sendAccelToUnity(3); | |
// BLUE | |
RGB.color(0,0,255); | |
break; | |
case MMA8451_PL_PDB: | |
Serial.println("Portrait Down Back"); | |
sendAccelToUnity(4); | |
// YELLOW | |
RGB.color(255,255,0); | |
break; | |
case MMA8451_PL_LRF: | |
Serial.println("Landscape Right Front"); | |
sendAccelToUnity(5); | |
// CYAN | |
RGB.color(0,255,255); | |
break; | |
case MMA8451_PL_LRB: | |
Serial.println("Landscape Right Back"); | |
sendAccelToUnity(6); | |
// MAGENTA | |
RGB.color(255,0,255); | |
break; | |
case MMA8451_PL_LLF: | |
Serial.println("Landscape Left Front"); | |
sendAccelToUnity(7); | |
// WHITE | |
RGB.color(255,255,255); | |
break; | |
case MMA8451_PL_LLB: | |
Serial.println("Landscape Left Back"); | |
sendAccelToUnity(8); | |
// OFF | |
RGB.color(0,0,0); | |
break; | |
} | |
Serial.println(); | |
delay(500); | |
} | |
int setIP(String args){ | |
// Doesn't do anything yet. | |
// A reminder to: | |
// set remote IP address; | |
return 1; | |
} | |
void sendAccelToUnity(int orient){ | |
OSCMessage outMessage("/accel"); | |
outMessage.addInt(orient); | |
outMessage.send(Udp,outIp,outPort); | |
status(); | |
} | |
void timedPublish(){ | |
OSCMessage outMessage("/timer"); | |
outMessage.addInt(theTime); | |
outMessage.send(Udp,outIp,outPort); | |
status(); | |
} | |
void status(){ | |
digitalWrite(STATUS_PIN, HIGH); | |
delay(100); | |
digitalWrite(STATUS_PIN, LOW); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment