I want to set my laser trip wire, which has a keypad to enable/disable the laser, to send me an email notification when the laser has been crossed. I have an ethernet shield but I am new to ethernet shields. Here is my code below:
#include <Password.h> //http://www.arduino.cc/playground/uploads/Code/Password.zip
#include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip
Password password = Password( "1234" );
const byte ROWS = 4; // Four rows
const byte COLS = 3; // columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 5, 4, 3, 2 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 8, 7, 6 };
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int pin = 0;
int laser = 0;
int i = 0;
int alarm = 0;
int firstTime = 0;
boolean crossed;
boolean failed;
void setup()
{
pinMode(13,OUTPUT); //Green LED
pinMode(12,OUTPUT); //Red LED
pinMode(11,OUTPUT); //Laser
Serial.begin(9600);
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}
void loop()
{
keypad.getKey();
//Check to see if laser has been crossed
if(laser == 1)
{
checkLaser();
if(i >= 3)
{
failed = true; //Activates alarm
}
else
{
failed = false; //Deactivates alarm
}
}
else
{
i = 0;
crossed = false;
}
//Code for the alarm
if(crossed || failed)
{
if(alarm < 1)
{
if(failed)
{
//Write code to send log information to servers about too many password attempts
}
if(crossed)
{
//Write code to send log info to servers about laser being crossed
}
Serial.println("Alarm Activated");
alarm = 1;
}
digitalWrite(12,HIGH); //Turns on red LED
}
}
//take care of some special events
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:
Serial.print("Pressed: ");
Serial.println(eKey);
switch (eKey){
case '*': checkPassword(); break;
case '#': password.reset(); break;
default: password.append(eKey);
}
}
}
void checkPassword(){
if (password.evaluate()){
Serial.println("Success");
//Flash green LED
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,LOW);
delay(100);
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,LOW);
i = 0;
failed = false;
crossed = false;
alarm = 0;
firstTime = 0;
digitalWrite(12,LOW);
password.reset();
//Turn on/off laser
if(laser == 0)
{
//Turn on laser
laser = 1;
Serial.println("Laser ON\n");
digitalWrite(11,HIGH);
}
else
{
//Turn off laser
laser = 0;
Serial.println("Laser OFF\n");
digitalWrite(11,LOW);
}
}
else
{
Serial.println("Wrong");
//Flash red LED
digitalWrite(12,HIGH);
delay(100);
digitalWrite(12,LOW);
delay(100);
digitalWrite(12,HIGH);
delay(100);
digitalWrite(12,LOW);
password.reset();
//If wrong more than 3 times will activate the alarm
i++;
}
}
void checkLaser()
{
//Checks laser to see if it has been crossed
if(firstTime < 1)
{
delay(5000); //Delays the alarm so that the laser has time to hit photocell
firstTime = 1;
}
//Serial.println(analogRead(0)); //Used to calibrate photocell, debuging purposes only
if(analogRead(0) > 600) //checks the photocell's resistance
{
crossed = true;
}
else
{
crossed = false;
}
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.