-
Rock, Paper, Scissors!
02/21/2017 at 00:39 • 0 commentsAfter spending a lot of time with the accelerometer sample code in Arduino and mapping my movements to a CSV, I wrote some code to handle the RPS when the arm is moved in the normal "throwing chops" motion.
#include <Wire.h> #include <Adafruit_MMA8451.h> #include <Adafruit_Sensor.h> #define TIMEUPDOWN 150 #define TIMEBETWEEN 800 #define THRESHDOWN -8000 #define THRESHUP 8000 Adafruit_MMA8451 mma = Adafruit_MMA8451(); int fistPumpCount = 0; int lastUp = 0; int lastDown = 0; void setup(void) { Serial.begin(9600); Serial.println("Let's throw chops!"); if (! mma.begin()) { //Serial.println("Couldnt start"); while (1); } //Serial.println("MMA8451 found!"); mma.setRange(MMA8451_RANGE_2_G); } void loop() { unsigned long timeStamp = millis(); mma.read(); int fistPump = mma.y; if(fistPump > THRESHUP) { int timeDiff = timeStamp - lastUp; if( timeDiff > TIMEUPDOWN ) { lastUp = timeStamp; if( timeDiff < TIMEBETWEEN ) { fistPumpCount++; } else { fistPumpCount = 1; } Serial.print("Pump Up: "); Serial.print(fistPumpCount); Serial.println(); } } if(fistPumpCount == 3) { randomSeed(timeStamp); int roshambo = random(1,4); // Stupid exclusive bit switch(roshambo) { case 1: Serial.println("Rock"); break; case 2: Serial.println("Paper"); break; case 3: Serial.println("Scissors"); break; } fistPumpCount = 0; } }