Wednesday, November 12, 2014

Glove Hero Code

This post is mainly for documentation purposes. I'll be posting something really cool soon! Below is the code you can use to make your own. By copying it here I can link to it in other places. Lets see if any Code Monkeys read my blog and can guess what I might be making!


Arduino Code:


/*
Full Game


 The circuit:
 * LED attached from pin 13 to ground
 * Thumb connected to A0
 * 4 finger inputs connect to digital pins 2-5
 * Finger pins 2-5 connected to rows 2-5 on bread board
 * 360 ohm resistors connected from rows 2-5 to ground on bread board
 * Ground on bread board connected to GND on Arduino
 * (Serial monitor connected to pins 0 and 1 via usb)

 * Note: there is already an LED on the board attached to pin 13.

 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to
// set pin numbers:
const int G1ThumbPin = A0;     // the number of the G1 Thumb pin
const int G1FingerPinI =  5;      // the number of G1 Index Finger input pin
const int G1FingerPinM =  4;      // the number of G1 Middle Finger input pin
const int G1FingerPinR =  3;      // the number of G1 Ring Finger input pin
const int G1FingerPinP =  2;      // the number of G1 Pinky Finger input pin

const int G2ThumbPin = A1;       // the number of the G2 Thumb pin
const int G2FingerPinI =  11;      // the number of G2 Index Finger input pin
const int G2FingerPinM =  10;      // the number of G2 Middle Finger input pin
const int G2FingerPinR =  9;      // the number of G2 Ring Finger input pin
const int G2FingerPinP =  8;      // the number of G Pinky Finger input pin

const int LED1Pin = A2;
const int LED2Pin = A3;
const int LED3Pin = A4;
const int LED4Pin = A5;

const int P1GreenPin = 6;
const int P1RedPin = 7;
const int P2GreenPin = 12;
const int P2RedPin = 13;

const String PatternOptions = "IMRP";

// variables will change:
int randNumber;
int RoundNumber;

// declare three strings:
String G1Pattern, G2Pattern, ActualPattern;
String G1Input, G2Input;
String PatternInput;


void setup() {
    // if analog input pin 0 is unconnected, random analog
  // noise will cause the call to randomSeed() to generate
  // different seed numbers each time the sketch runs.
  // randomSeed() will then shuffle the random function.
  pinMode(A0,INPUT);
  randomSeed(analogRead(A0));
 
  // initialize Thumb pins as an output:
  pinMode(G1ThumbPin, OUTPUT);
  pinMode(G2ThumbPin, OUTPUT);
  // initialize the G1 pins as inputs:
  pinMode(G1FingerPinI, INPUT);
  pinMode(G1FingerPinM, INPUT);
  pinMode(G1FingerPinR, INPUT);
  pinMode(G1FingerPinP, INPUT);
 // initialize the G1 pins as inputs:
  pinMode(G2FingerPinI, INPUT);
  pinMode(G2FingerPinM, INPUT);
  pinMode(G2FingerPinR, INPUT);
  pinMode(G2FingerPinP, INPUT);
  //Initialize Board LEDs as an output:
  pinMode(LED1Pin, OUTPUT);
  pinMode(LED2Pin, OUTPUT);
  pinMode(LED3Pin, OUTPUT);
  pinMode(LED4Pin, OUTPUT);
  //Initialize Indicator lights
  pinMode(P1GreenPin, OUTPUT);
  pinMode(P1RedPin, OUTPUT);
  pinMode(P2GreenPin, OUTPUT);
  pinMode(P2RedPin, OUTPUT);
 
   // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
    ActualPattern = "";
    RoundNumber=1;
   
}

void loop(){
  //Start game loop
 
  //initialize round number to 1 and pattern to Null string
//  RoundNumber=1;
//  ActualPattern="";
  int RoundOver=false;
 
  //start round loop
  do {
    Serial.println("begin round");
  //create the pattern
  while (ActualPattern.length() < RoundNumber) {
     // print a random number from 0 to 3
     randNumber = random(4);
     // Serial.println(PatternOptions[randNumber]);
     PatternInput = String(PatternOptions[randNumber]);
 
      if (ActualPattern.endsWith(PatternInput)) {
       ActualPattern = ActualPattern;
      }
      else {
       ActualPattern = ActualPattern + PatternInput;
       Serial.println("ActPat");
       Serial.println(ActualPattern);
      }
  }

  //Send pattern to LEDs
  for (int x=0; x<=ActualPattern.length(); x++){
   switch (ActualPattern[x]){
     case 'I':
     //send signal to LED1
     digitalWrite(LED1Pin, HIGH); //turn the LED on by making the voltage HIGH
     delay(1000);              // wait for a second
     digitalWrite(LED1Pin, LOW);    // turn the LED off by making the voltage LOW
     break;
   
     case 'M':
     //send signal to LED2
     digitalWrite(LED2Pin, HIGH); //turn the LED on by making the voltage HIGH
     delay(1000);              // wait for a second
     digitalWrite(LED2Pin, LOW);    // turn the LED off by making the voltage LOW
     break;
   
     case 'R':
     //send signal to LED3
     digitalWrite(LED3Pin, HIGH); //turn the LED on by making the voltage HIGH
     delay(1000);              // wait for a second
     digitalWrite(LED3Pin, LOW);    // turn the LED off by making the voltage LOW
     break;
   
     case 'P':
     //send signal to LED4
     digitalWrite(LED4Pin, HIGH); //turn the LED on by making the voltage HIGH
     delay(1000);              // wait for a second
     digitalWrite(LED4Pin, LOW);    // turn the LED off by making the voltage LOW
     break;
   }
  }
 
  //***** BEGIN Reading inputs from Gloves *****
   // initialize G1 states
  int G1IState=LOW;
  int G1MState=LOW;
  int G1RState=LOW;
  int G1PState=LOW;
   // initialize G2 states
  int G2IState=LOW;
  int G2MState=LOW;
  int G2RState=LOW;
  int G2PState=LOW;
  //reset G1Input to null
  G1Input = "";
  G2Input = "";
  G1Pattern = "";
  G2Pattern = "";

  //set ThumbPins to High
  digitalWrite(G1ThumbPin, HIGH);
  digitalWrite(G2ThumbPin, HIGH);
 
 
  while (G1Pattern.length()<ActualPattern.length() || G2Pattern.length()<ActualPattern.length())
  {
  // read the state of the G1FingerPinI value:
  G1IState = digitalRead(G1FingerPinI);
  // check if the G1I is pressed.
  // if it is, the G1IState is HIGH:
  if (G1IState == HIGH) {    
     // print out I:
   // Serial.println("I");
    G1Input = "I";
  }
 
  // read the state of the G1FingerPinM value:
  G1MState = digitalRead(G1FingerPinM);
   // check if the G1M is pressed.
  // if it is, the G1MState is HIGH:
  if (G1MState == HIGH) {    
     // print out M:
  //  Serial.println("M");
    G1Input = "M";
  }
 
  // read the state of the G1FingerPinR value:
  G1RState = digitalRead(G1FingerPinR);
   // check if the G1R is pressed.
  // if it is, the G1RState is HIGH:
  if (G1RState == HIGH) {    
     // print out R:
  //  Serial.println("R");
    G1Input = "R";
  }
 
  // read the state of the G1FingerPinP value:
  G1PState = digitalRead(G1FingerPinP);
   // check if the G1P is pressed.
  // if it is, the G1PState is HIGH:
  if (G1PState == HIGH) {    
     // print out P:
  //  Serial.println("P");
    G1Input = "P";
  }
 
  if (G1Pattern.endsWith(G1Input)) {
     G1Pattern = G1Pattern;
  }  
  else {
    G1Pattern = G1Pattern + G1Input;  
    Serial.println("G1Pat");
    Serial.println(G1Pattern);
  }
   // read the state of the G2FingerPinI value:
  G2IState = digitalRead(G2FingerPinI);
  // check if the G2I is pressed.
  // if it is, the G2IState is HIGH:
  if (G2IState == HIGH) {    
     // print out I:
   // Serial.println("I");
    G2Input = "I";
  }
 
  // read the state of the G2FingerPinM value:
  G2MState = digitalRead(G2FingerPinM);
   // check if the G2M is pressed.
  // if it is, the G2MState is HIGH:
  if (G2MState == HIGH) {    
     // print out M:
  //  Serial.println("M");
    G2Input = "M";
  }
 
  // read the state of the G2FingerPinR value:
  G2RState = digitalRead(G2FingerPinR);
   // check if the G2R is pressed.
  // if it is, the G2RState is HIGH:
  if (G2RState == HIGH) {    
     // print out R:
  //  Serial.println("R");
    G2Input = "R";
  }
 
  // read the state of the G2FingerPinP value:
  G2PState = digitalRead(G2FingerPinP);
   // check if the G2P is pressed.
  // if it is, the G2PState is HIGH:
  if (G2PState == HIGH) {    
     // print out P:
  //  Serial.println("P");
    G2Input = "P";
  }
 
  if (G2Pattern.endsWith(G2Input)) {
     G2Pattern = G2Pattern;
  }  
  else {
    G2Pattern = G2Pattern + G2Input;
 Serial.println("G2Pat");
  Serial.println(G2Pattern);
  }
  }
 
//***** END Reading inputs from Gloves *****
 // Serial.println(G1Pattern);
 int P1Correct=false;
 int P2Correct=false;
 
 if (G1Pattern == ActualPattern) {
   P1Correct=true;
   digitalWrite(P1GreenPin, HIGH);
 }
 else {
   digitalWrite(P1RedPin, HIGH);
 }

 if (G2Pattern == ActualPattern) {
   P2Correct=true;
   digitalWrite(P2GreenPin, HIGH);
 }
 else {
   digitalWrite(P2RedPin, HIGH);
 }
  delay (3000);
  digitalWrite(P1GreenPin, LOW);
  digitalWrite(P1RedPin, LOW);
  digitalWrite(P2GreenPin, LOW);
  digitalWrite(P2RedPin, LOW);
  Serial.println(P1Correct);
  Serial.println(P2Correct);

 

 if (P1Correct==true && P2Correct==true) {
   RoundNumber=RoundNumber+1;
   Serial.println("both correct");
   Serial.println(RoundNumber);
 }
 else {
   if (P1Correct==true) {
     RoundOver=true;
     RoundNumber=1;
     ActualPattern="";
      digitalWrite(P1GreenPin, HIGH);
      delay (500);
      digitalWrite(P1GreenPin, LOW);
      digitalWrite(P1RedPin, HIGH);
      delay (500);
      digitalWrite(P1RedPin, LOW);
      digitalWrite(P1GreenPin, HIGH);
      delay (500);
      digitalWrite(P1GreenPin, LOW);
      digitalWrite(P1RedPin, HIGH);
      delay (500);
      digitalWrite(P1RedPin, LOW);
      digitalWrite(P1GreenPin, HIGH);
      delay (500);
      digitalWrite(P1GreenPin, LOW);
      digitalWrite(P1RedPin, HIGH);
      delay (500);
      digitalWrite(P1RedPin, LOW);
      digitalWrite(P1GreenPin, HIGH);
      delay (500);
      digitalWrite(P1GreenPin, LOW);
      digitalWrite(P1RedPin, HIGH);
      delay (500);
      digitalWrite(P1RedPin, LOW);
      digitalWrite(P1GreenPin, HIGH);
      delay (500);
      digitalWrite(P1GreenPin, LOW);
      digitalWrite(P1RedPin, HIGH);
      delay (500);
      digitalWrite(P1RedPin, LOW);
     
   }
   else{
     if (P2Correct==true) {
      RoundOver=true;
      RoundNumber=1;
      ActualPattern="";
      digitalWrite(P2GreenPin, HIGH);
      delay (500);
      digitalWrite(P2GreenPin, LOW);
      digitalWrite(P2RedPin, HIGH);
      delay (500);
      digitalWrite(P2RedPin, LOW);
      digitalWrite(P2GreenPin, HIGH);
      delay (500);
      digitalWrite(P2GreenPin, LOW);
      digitalWrite(P2RedPin, HIGH);
      delay (500);
      digitalWrite(P2RedPin, LOW);
      digitalWrite(P2GreenPin, HIGH);
      delay (500);
      digitalWrite(P2GreenPin, LOW);
      digitalWrite(P2RedPin, HIGH);
      delay (500);
      digitalWrite(P2RedPin, LOW);
      digitalWrite(P2GreenPin, HIGH);
      delay (500);
      digitalWrite(P2GreenPin, LOW);
      digitalWrite(P2RedPin, HIGH);
      delay (500);
      digitalWrite(P2RedPin, LOW);
      digitalWrite(P2GreenPin, HIGH);
      delay (500);
      digitalWrite(P2GreenPin, LOW);
      digitalWrite(P2RedPin, HIGH);
      delay (500);
      digitalWrite(P2RedPin, LOW);
     }
     else {
      digitalWrite(P1RedPin, LOW);
      digitalWrite(P2RedPin, LOW);
      delay (500);
      digitalWrite(P1RedPin, HIGH);
      digitalWrite(P2RedPin, HIGH);
      delay (500);
      digitalWrite(P1RedPin, LOW);
      digitalWrite(P2RedPin, LOW);
      delay (500);
      digitalWrite(P1RedPin, HIGH);
      digitalWrite(P2RedPin, HIGH);
      delay (500);
      digitalWrite(P1RedPin, LOW);
      digitalWrite(P2RedPin, LOW);
      delay (500);
      digitalWrite(P1RedPin, HIGH);
      digitalWrite(P2RedPin, HIGH);
      delay (500);
      digitalWrite(P1RedPin, LOW);
      digitalWrite(P2RedPin, LOW);
     }
   }
  }

   Serial.println("end round");
  }   // closes do
 while (RoundOver=false);
 
}