Wand
The plan was to create a wand from wood or to 3D print it, after which I'd insert the Nano Every inside the wand with the uploaded code.
It's a controller made to cast spells through arm motion in Unity.
Arduino sends a message that Unity can pickup whenever enough movement to cast has been registered.
(The video is in Dutch)
Supplies
Arduino Nano Every
Adafruit ADXL335 Accelerometer
Jumper cables (Female - Female)
Unity
Wooden Branch (Shaped using sanding machine and saw machine.)
Fabric (Originally from pillow case)
Code
const int xInput = A0; const int yInput = A1; const int zInput = A2; const int xRawMin = 407; const int xRawMax = 613; const int yRawMin = 407; const int yRawMax = 609; const int zRawMin = 426; const int zRawMax = 625; const int sampleSize = 10; float netForce = 0; void setup() { analogReference(EXTERNAL); Serial.begin(9600); } void loop() { // put your main code here, to run repeatedly: int xRaw = ReadAxis(xInput); int yRaw = ReadAxis(yInput); int zRaw = ReadAxis(zInput); // Convert raw values to 'milli-Gs" long xScaled = map(xRaw, xRawMin, xRawMax, -1000, 1000); long yScaled = map(yRaw, yRawMin, yRawMax, -1000, 1000); long zScaled = map(zRaw, zRawMin, zRawMax, -1000, 1000); // re-scale to fractional Gs float xAccel = xScaled / 1000.0; float yAccel = yScaled / 1000.0; float zAccel = zScaled / 1000.0; netForce = abs(xAccel) + abs(yAccel) + abs(zAccel - 1); Serial.println(); Serial.print(xRaw); Serial.print(", "); Serial.print(yRaw); Serial.print(", "); Serial.print(zRaw); Serial.print(" :: "); Serial.print(xAccel); Serial.print("G, "); Serial.print(yAccel); Serial.print("G, "); Serial.print(zAccel); Serial.println("G"); Serial.print("Total force applied: "); Serial.println(netForce); if (netForce >= 0.4) { Serial.println("I should cast!"); Serial.write(0); } else { Serial.println("No cast!"); Serial.write(1); } Serial.flush(); delay(200); } int ReadAxis(int axisPin) { long reading = 0; analogRead(axisPin); delay(1); for (int i = 0; i < sampleSize; i++) { reading += analogRead(axisPin); } return reading / sampleSize; } The code is pretty much taken from the ADXL335's Adafruit Learn page, with some minor edits to get the right information to Unity. It can only resend the acceleration info every 0.2 seconds, which is fine, since you can only shoot in 2 second intervals in Unity. I pretty much take the forces working on the axes as absolute values, combine them and check if it's above a certain threshold.
It prints a lot of data, in case you need to see what values are returned in the Serial Monitor, Unity doesn't need it.
Building the Wand
After getting the Nano Every, It was just a matter of soldering and connecting the now female to female cables.
Once I did that, I needed to make the wand. Luckily our school has some great equipment to do so.
I decided to keep the look a bit more natural
Demonstration!
In this video, I demonstrate how the wand works.
It's in Dutch