We received an analog buzzer this week and started to work on it. We found that the buzzer makes noises when the voltage drop across the buzzer changes rapidly, so we decided to use a constantly varying digital output on the arduino board to control the buzzer. We developed the code, and found that this code can not make the buzzer and the LED work together, so we are still working on it. The code is attached below. For the logic-gate based password system, the circuit schematic and design are nearly completed and we are ready to assemble next week.
#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;
int timer = 0;
int state = 0;
void setup()
{
accel.init();
pinMode(TRIG_PIN, OUTPUT);
digitalWrite(TRIG_PIN, LOW);
pinMode(13, OUTPUT);
pinMode(11, OUTPUT);
}
void lightLed()
{
if (cm>100||accel.cz > 1.15)
{
if(timer<10){
timer++;
}
else{
if(state==100)state=0;
if(state%2==0)digitalWrite(13,HIGH);
else digitalWrite(13,LOW);
timer = 0;
state++;
}
}
}
void buzzer()
{
if (cm>100||accel.cz > 1.15)
{
for(int t;t<1000;t++){
digitalWrite(11, HIGH);
delay(5);
digitalWrite(11, LOW);
delay(5);}
}
}
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();
buzzer();
}
}