Back to Parent

//Create IDs
char melanie[3];
char stephen[3];
char alex[3];
char test[3];

//button inputs
int buttonPin1 = D3;
int buttonPin2 = D4;
int buttonPin3 = D5;
int buttonPin4 = D2;

//previous button state
int lastButtonState1 = HIGH;
int lastButtonState2 = HIGH;
int lastButtonState3 = HIGH;
int lastButtonState4 = HIGH;

//LED outputs
int ledPin1 = D6; //blue, melanie took a tool
int ledPin2 = D1; //red, incorrect code entered
int ledPin3 = A1; //green, melanie unlocked
int ledPin4 = A4; //green, alex unlocked
int ledPin5 = A5; //green, stephen unlocked

//number of code characters entered
int counter = 0;
int status = 0;

//simulating hammer removed from cabinet
int hammerRemove(String command);

void setup(){

  //establishing pin codes for different individuals
  melanie[0] = 'A';
  melanie[1] = 'A';
  melanie[2] = 'A';

  stephen[0] = 'A';
  stephen[1] = 'B';
  stephen[2] = 'A';

  alex[0] = 'C';
  alex[1] = 'A';
  alex[2] = 'B';

  // set pins as input
  pinMode(buttonPin1 , INPUT_PULLUP);
  pinMode(buttonPin2 , INPUT_PULLUP);
  pinMode(buttonPin3 , INPUT_PULLUP);
  pinMode(buttonPin4 , INPUT_PULLUP);

  //digital LED set up
  pinMode(ledPin1, OUTPUT);
  digitalWrite(ledPin1, LOW);
  pinMode(ledPin2, OUTPUT);
  digitalWrite( ledPin2, LOW);

  //analog LED set up
  pinMode(ledPin3, OUTPUT);
  analogWrite( ledPin3, 0);
  pinMode(ledPin4, OUTPUT);
  analogWrite( ledPin4, 0);
  pinMode(ledPin5, OUTPUT);
  analogWrite( ledPin5, 0);

  //create cloud variables for variables as needed
  //Spark.variable("melanie", &melanie, STRING);
  //Spark.variable("stephen", &stephen, STRING);
  //Spark.variable("alex", &alex, STRING);
  Spark.variable("test", &test, STRING);

  //create function to trigger using Yo and IFTTT simulating bluetooth
  Spark.function("hammer", hammerRemove);
}

void loop(){
  //read button states
  int buttonState1 = digitalRead(buttonPin1);
  int buttonState2 = digitalRead(buttonPin2);
  int buttonState3 = digitalRead(buttonPin3);
  int buttonState4 = digitalRead(buttonPin4);

  //check to see if button states have changed since the loop last ran
  if( buttonState1 != lastButtonState1 or buttonState2 != lastButtonState2
    or buttonState3 != lastButtonState3 or buttonState4 != lastButtonState4)
  {
    //set of if statements to input pin codes as buttons are pressed or to
    //reset if the red button is pressed
      if(buttonState1==LOW){
        test[counter] = 'A';
        counter++;
        delay( 1000 );
      }else if(buttonState2 == LOW){
        test[counter] = 'B';
        counter++;
        delay( 1000 );
      }else if(buttonState3 == LOW){
        test[counter] = 'C';
        counter++;
        delay( 1000 );
      }else if(buttonState4 == LOW){
        counter = 0;
        //if reset button is repressed, then go back to beginning in entering
        //code and turn off any lights that are on
        digitalWrite(ledPin1, LOW);
        digitalWrite(ledPin2, LOW);
        analogWrite(ledPin3, 0);
        analogWrite(ledPin4, 0);
        analogWrite(ledPin5, 0);
        delay( 1000 );
      }

  }

  //once three digits have been entered as the pin then that pin is compared to
  //the ID codes, if it matches then that person's green light turns on
  //if none of the codes match then the red light turns on
  if( counter >= 3 ){
      if( doCompare( test, melanie ) )
        status = 1;
      if( doCompare( test, alex ) )
        status = 2;
      if( doCompare( test, stephen ) )
        status = 3;
      //operate LEDs based on status
      if(status == 1){
        analogWrite(ledPin3, 255);
      }else if(status==2){
        analogWrite(ledPin4, 255);
      }else if(status==3){
        analogWrite(ledPin5, 255);
      }else{
        digitalWrite(ledPin2, HIGH);
      }
    //reset the counter to zero so a new code can be entered
    counter = 0;
  }

  //save current button state so it can be compared to the new button state the
  //next time the loops runs
  lastButtonState1 = buttonState1;
  lastButtonState2 = buttonState2;
  lastButtonState3 = buttonState3;
  lastButtonState4 = buttonState4;
  delay( 50 );
}

//this function iterates through the two different character arrays to see
//if they match
boolean doCompare( char array1[], char array2[] )
{
    boolean doesMatch = true;

    for( int i =0 ; i < 3; i++ )
    {
      if( array1[i] != array2[i] )
        doesMatch = false;
    }
    return doesMatch;
}

//
int hammerRemove(String command){
  digitalWrite( ledPin1, HIGH);
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0