We received the ultrasonic sensor this week. We updated our code to accommodate both the ultrasonic sensor and the accelerometer sensor. The new code also enables the light to flash incessantly after the condition is fulfilled, instead of just lighting. 

The digital buzzer had not arrived yet, and we were considering to use the analog buzzer the school had. The hardest part now is the password system, which we decided to build using the ECE120 Finite State Machine. 

 

 

 

The Arduino code we have so far:

#include <Wire.h>
#include <SFE_MMA8452Q.h>


MMA8452Q accel;

const int TRIG_PIN = 7;
const int ECHO_PIN = 8;
const unsigned int MAX_DIST = 23200;
unsigned long t1;
unsigned long t2;
unsigned long pulse_width;
float cm;


void setup()
{
  accel.init();
  pinMode(TRIG_PIN, OUTPUT);
  digitalWrite(TRIG_PIN, LOW);
  pinMode(13, OUTPUT);
}

void lightLed()
{
if (cm>100||accel.cz > 1.15)
{
    for(int t;t<1000;t++){
      digitalWrite(13, HIGH);
      delay(500);
      digitalWrite(13,LOW);
      delay(500);}
  }
}

void loop()
{
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  while ( digitalRead(ECHO_PIN) == 0 );
  t1 = micros();
  while ( digitalRead(ECHO_PIN) == 1);
  t2 = micros();
  pulse_width = t2 - t1;
  cm = pulse_width / 58.0;
  if (accel.available())
  {
    accel.read();
    lightLed();
   }

}