A friend and I have been working on a simple alarm system for a while to help guard our rooms. I think the project has finally reached the finished stage, or at least getting close. This is a continuance of the project i posted about here http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1248484845/8#8.
I am posting in hopes that someone may be able to help me understand what i can do to make my code smaller. It works almost perfectly, in that if a sensor is tripped, it updates the lcd to show that a sensor was tripped, and also sends a twitter to let you know about it if you are away.
You can read more about the setup we have going to: http://www.grinanbarrett.com/index.php?option=com_content&view=article&id=59:a-twitter-enabled-alarm-system&catid=34:arduino&Itemid=54
if you would like to see what it twitters, you can see that here:
http://www.twitter.com/grinanbarrett2
Can anyone tell me what i can change to shrink my code? I know that I am not a programmer, therefore, there must be things that I have here that are unncessary, or could be reworded to be smaller.
here's the code we are working with
#include <LiquidCrystal.h>
#include <Ethernet.h>
#include <Twitter.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 17 };
byte gateway[] = { 192, 168, 1, 2 };
byte subnet[] = { 255, 255, 255, 0 };
Twitter twitter("USERNAME:PASSWORD");
// LiquidCrystal display with:
// rs on pin 12
// rw on pin 11
// enable on pin 10
// d4, d5, d6, d7 on pins 5, 4, 3, 2
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
const byte strobePin = 28; // pin for the strobe light
const byte windowPin = 8; // pin for the window plunger
const byte relayPin = 17; // pin for the Relay (this was the ledPin, this is just easier for me to understand than ledpin.)
const byte pirPin = 9; // pin for the pir sensor
const byte window2Pin = 14; // pin for window 2
const byte disableWinPin = 15; // NEW DISABLE BUTTON
const byte disablePir = 16; // NEW DISABLE BUTTON 2
boolean currentState = LOW; // holder for the current state of the alarm
boolean previousState = HIGH; // holder for the previous state of the pin
boolean currentDisState = LOW;
boolean previousDisState = HIGH;
int Tripped=0; // variable to hold the count of times the alarm was tripped
int Disabled=0;
void setup() {
pinMode(relayPin, OUTPUT);
pinMode(pirPin, INPUT);
pinMode(strobePin, OUTPUT);
pinMode(windowPin, INPUT);
pinMode(window2Pin, INPUT);
pinMode(disableWinPin,INPUT);
pinMode(disablePir, INPUT);
digitalWrite(relayPin, LOW);
digitalWrite(pirPin, LOW); // this sets the initial state of the pin
digitalWrite(windowPin, LOW); // this sets the intial state of the pin
digitalWrite(window2Pin, LOW); // this sets the intial state of the pin
digitalWrite(disablePir, LOW);
digitalWrite(disableWinPin, LOW);
lcd.begin(2, 12); // This sets the lcd to a two line, 16 char display
lcd.clear(); // clear the display
Ethernet.begin(mac, ip, gateway, subnet);
Serial.begin(9600);
}
void CheckWindow(){
currentState = digitalRead(windowPin); // read the pirpin and store the state
if (currentState!=previousState && currentState==LOW) { // do comparison to see if we have a change or not
digitalWrite(relayPin, LOW); // alarm not tripped - relaypin is low
} else {
Trip(); // alarm is tripped, relaypin goes high
delay(20); // this is to debounce the input
digitalWrite(relayPin, HIGH); // turn the relay on
twitter.post("Window one alarm has been tripped");
int status = twitter.wait();
if(status == 200) {
Serial.println("OK.");
}else{
Serial.print("Failed: code ");
Serial.println(status);
}
delay(25000); // keep relay on and stop checking for low state for 25 seconds
previousState = currentState; // set the previous state so we can compare on the next loop through
}
}
void CheckWindow2(){
currentState=digitalRead(window2Pin); // Read the windowPin and store as currentstate
if(currentState!=previousState && currentState==LOW){ // do comparisons to see if we have a change or not
digitalWrite(strobePin,LOW); // alarm is not tripped - strobe pin is low
}else{
digitalWrite(relayPin,HIGH); // alarm is tripped - strobe pin is high
Trip(); // alarm function to count alarm and display to lcd
twitter.post("Window two alarm has been tripped");
int status = twitter.wait();
if(status == 200) {
Serial.println("OK.");
}else{
Serial.print("Failed: code ");
Serial.println(status);
}
delay(25000); // keep strobe on for 25 seconds
previousState = currentState; // set the previous state so we can compare on the next loop through
}
}
void CheckPir(){
currentState=digitalRead(pirPin);
if(currentState!=previousState && currentState==LOW){
digitalWrite(strobePin,LOW);
}else{
digitalWrite(strobePin,HIGH);
Trip();
twitter.post("Pir alarm has been tripped");
int status = twitter.wait();
if(status == 200) {
Serial.println("OK.");
}else{
Serial.print("Failed: code ");
Serial.println(status);
}
delay(15000);
previousState = currentState;
}
}
void Trip(){ // tripped state function
Tripped++;
lcd.clear();
lcd.print("Alarm tripd "); // Printed on default line on lcd
lcd.setCursor(0,1); // moves the cursor to the second line
lcd.print(Tripped); // prints on the second line.
lcd.print(" Times :=)");
lcd.home(); // Returns cursor to first line.
}
void CheckDisable(){
currentState=digitalRead(disableWinPin);
if(currentState!=previousState && currentState==LOW){
//no window alarm disable
}else{
lcd.print("Win Pin Off");
Disabled=1;
delay(250);
previousState = currentState;
}
currentState=digitalRead(disablePir);
if (currentState!=previousState && currentState==LOW){
//no pir alarm disable
}else {
lcd.print("PIR Pin Off");
Disabled=2;
delay(250);
previousState=currentState;
}
}
void loop() {
if (Disabled==0){ // 0 - all alarms on 1 - window alarm off pir on 2 - window on pir off
CheckWindow();
CheckWindow2();
CheckPir();
CheckDisable();
}else if(Disabled==1){
CheckPir();
CheckDisable();
} else if(Disabled==2){
CheckWindow();
CheckWindow2();
CheckDisable();
}
}
Thanks in advance!