Since our parts haven't arrived in the mail yet, we made do with what the Sparkfun Inventor's Kit had. We used the kit's piezo instead of a larger one, a potentiometer instead of a vibration sensor, a button instead of an infrared remote switch, and base LEDs instead of brighter ones. I recently bought a larger servo from radioshack for the part where it shoots silly string, but this part of the project was done before then.
I coded the Arduino to arm itself when the button is pressed. A LED comes on to show that it's armed and if the potentiometer's analog reading reaches a certain value, red LEDs flash on and the piezo makes a buzzing noise. This code has the basic logic of our system. The system can be armed or disarmed, and the sensor triggers a reaction once a certain value is detected.
Here's our code:
#define LED_ALARM 13 //Red LEDS on output pin 13
#define LED_ON 12 //Green LED on ouotput pin 12
#define BUTTON 2 //Button switch on input pin 2
#define SENSORPIN 0 //Potentiometer on analog input pin A0
#define BUZZER 11 //Piezo buzzer on output pin 11
int val = 0; //It's value changes based on whether the button is pressed or not
int old_val = 0; //Stores the old value of the button so it properly turn on or off
int state = 0; //State of the button, is it on or off?
int sensorValue; //Value of the potentiometer
void setup() {
pinMode(LED_ALARM, OUTPUT);
pinMode(LED_ON, OUTPUT);
pinMode(BUTTON, INPUT);
pinMode(BUZZER, OUTPUT);
}
void loop() {
val = digitalRead(BUTTON); //Sets the value to LOW or HIGH based on the button being pressed or not
if ((val == HIGH) && (old_val == LOW)){ //Changes the state of the button to on or off
state = 1 - state;
delay(10);
}
old_val = val; //So we know that we're going from one button state to another when it's pressed
if (state == 1) { //If the button is pressed the green light comes on and the potentiometer starts to read
digitalWrite(LED_ON, HIGH);
sensorValue = analogRead(SENSORPIN);
} else {
digitalWrite(LED_ON, LOW); //If the button isnt pressed then the light is off
}
if (sensorValue > 500) { //If the potentiometer is a certain value, the alarm sounds and the red LEDs flash
digitalWrite(LED_ALARM, HIGH);
delay(250);
digitalWrite(LED_ALARM, LOW);
delay(250);
buzz(BUZZER, 2500, 500); //buzz the buzzer on pin 11 at 2500Hz for 500 milliseconds
delay(1000);
}
}
void buzz(int targetPin, long frequency, long length) {
long delayValue = 1000000/frequency/2; //calculate the delay value between transitions
//// 1 second's worth of microseconds, divided by the frequency, then split in half since
//// there are two phases to each cycle
long numCycles = frequency * length/ 1000; //calculate the number of cycles for proper timing
//// multiply frequency, which is really cycles per second, by the number of seconds to
//// get the total number of cycles to produce
for (long i=0; i < numCycles; i++){ //for the calculated length of time...
digitalWrite(targetPin,HIGH); //write the buzzer pin high to push out the diaphragm
delayMicroseconds(delayValue); //wait for the calculated delay value
digitalWrite(targetPin,LOW); //write the buzzer pin low to pull back the diaphragm
delayMicroseconds(delayValue); //wait again for the calculated delay value
}
}
No comments:
Post a Comment