The Whole Codes (Including ADDED VALUE) - YollyLau/ECE387-Individual-Final GitHub Wiki
#include <IRremote.h>
// Include above IRremote library to send and receive infrared code more information about the library
#include <IRremoteInt.h>
int RECV_PIN = 11; //Define integer "RECV_PIN" as pin 11
IRrecv irrecv(RECV_PIN);// set "RECV_PIN(pin 11)" as the infrared receiver port
decode_results results;// define "results" as the place to storage infrared code
int buzzer = 8; // set the buzzer as pin 8
int led=13;// Set the LED as pin 13
float sinVal; // define the sinusoid signal more precisely by using float type
int toneVal;// transfer arc into sound frequencies
int state=0;
void setup(){
Serial.begin(9600);
irrecv.enableIRIn(); // start infrared decoding
pinMode(buzzer, OUTPUT);
pinMode(led,OUTPUT);
}
void loop(){
if(irrecv.decode(&results) )
{
Serial.println(results.value, HEX);
The corresponding infrared code of turn on/off signal sent from the controller is 0xFFC23D other button codes of this controller
if (state == 0 && results.value == 0xFFC23D)
{
state = 1;
Serial.println(state);
}
else if(state == 1 && results.value == 0xFFC23D)
{
state = 0;
Serial.println(state);
}
irrecv.enableIRIn(); // Start the receiver
}
if(state == 1) {
int val;
val = analogRead(A0);
function(val);
}
else {
digitalWrite(buzzer, LOW);
digitalWrite(led, LOW);
}
}
void newtone(byte tonePin, int frequency,int duration) {
int period = 1000000L / frequency;
int pulse = period / 2;
for (long i = 0; i < duration * 1000L; i+= period) {
digitalWrite(tonePin, HIGH);
delayMicroseconds(pulse);
digitalWrite(tonePin, LOW);
delayMicroseconds(pulse);
}
}
void function(int val){
if(val<1023){ //if the value is lower than 1023, the buzzer will sound
for(int x=0; x<180; x++){
// transfer degree value to arc value
sinVal = (sin(x*(3.1412/180)));
//use the sin value to make sound frequencies
toneVal = 2000+(int(sinVal*1000));
newtone(buzzer,toneVal,10);
analogWrite(led,x);
delay(2);
}
}
else{ // if the value is not lower than 1023, the buzzer will turn off
digitalWrite(buzzer,LOW);
analogWrite(led,LOW);}
}
As I said before, I take the voltage value on the photo-resistor as the parameter and compare it to the biggest value 1023. Since in the dark environment(as the alarm device covered in the closed bag), the resistance of the photo-resistor is the biggest, so its voltage can get up to 1023. Once the photo-resistor feels the light(the theft opens the bag), its resistance will decrease and its corresponding voltage will be less than 1023. Then the alarm will be raised as the buzzer sounds in sinusoidal wave. To achieve this, using the equation (3.1412/180) to turn the degree into arc and assign the arc value to sinVal, which is equal to (sin(x*(3.1412/180))). And then, turn the value into the sound frequencies of the alarm by writing "toneVal = 2000+(int(sinVal*1000))". Now toneVal is an adaptive sound frequency for the buzzer. And then, use newtone() function to send the frequency to the buzzer. By writing "anologWrite(led,x)", the blinking frequency of the LED will be the same as the buzzer.