Pages: [1]   Go Down
Author Topic: burglary and fire power - problems with exit status code of "ALARM!" HELP  (Read 1773 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am a Romanian and I need help, please help me I do not know who to call!
my problem is I do not know to program an Arduino so good that I would have liked ... In a few days ago I heard of this card and bought it as a project to help me. I managed to make it functional in one week with all assembled, soldered, so he can be mounted on a wooden board, and I reached a point where I never thought from the beginning and now we are crying learned too late about this and now I need help!

I saw the forum as you start to explain something Millis () I think it's the next site http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
I do not know how to use this command to my program

I'll explain my program to realize you do not know what to do! ask for a suggestion not practical to ask you show me how you put it into operation as code, I do not want to say that I use or something you really do not know that here in Romania at the moment I have not found anyone like this or even to know the function to use.

I use them: two LEDs, a serial LCD, a magnetic contact, four buttons, a Epirus, and a temperature sensor LM35DZ, and obviously an Arduno.
with everything I said above I simulate a burglary and fire station.

thing is that the two LEDs are placed in parallel, because I wanted because I wanted to turn lights on each kind of flashing!

With three of the four have formed a keypad buttons and with the fourth button in this kind of panic button that sets the alarm me, the problem is that when you go into alarm (the alarm state of the two LEDs light up and write a message on the LCD series, which he writes on , ALARM); same thing happens to me and temperature sensor, and Epirus, except the magnetic contact, magnetic contact me as I made ??contact and I detect the opening of the LCD display "enter code", and this allows me to introduce a code composed of the three buttons, and correct me if I entered the LCD displays "authenticated " and if you enter the wrong code in May I give permission to enter the code again and now if I write good code inserted "authenticated " and enters the standby and enter the code again if I go wrong in an "ALARM".

My problem is the state of alarm, because I only can get out of the alarm condition only RESET button, I want to leave the state of alarm by a combination of three buttons! I was very beginner, I never realized that I have used many loops and while () and more delay () I found out that I could solve this by rewriting the code and I'm thinking how to rewrite it to you in following site: http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

I show you the code that I've done, is not too great, but please help me so much I really need help, and show me a new code should look like to be able to give me what I I need!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


many things are the type, the LCD display are in Romanian, is not a problem if you put them in English ....

 My code is as follows:

Code:
// ver. 1.0

#include "SoftwareSerial.h"
#include "NewSoftSerial.h"

#define LED_1 7      // led 1
#define LED_2 6      // led 2
#define TX_PIN 5     // TX for LCD

SoftwareSerial SerialLCD = SoftwareSerial(0, TX_PIN);

#define rxPin 2    // RX for ePIR
#define txPin 3    // TX for ePIR

NewSoftSerial mySerial(rxPin, txPin);

// datasheet messages
const char ACK = char(6); // ACK = 0x06 (ASCII ACK character)
const char NACK = char(21); // NACK = 0x15 (ASCII NACK character).
// The SBC will respond with a 'NACK'
// on all unrecognized commands, and when command requiring data (that is,
// Write, Clear, and Confirmation types) does not receive the required data
// within the inactivity timeout period
const char Cmd_a = char(97); // 0x61 - 'a'    Read Motion Status

//  // code
int codul[3];
int cod_corect[3]; //correct code
int primulCod; // first code
int autentificat; //authenticated

// Pini
const int button1 = 14;     // pin A0 - button 1
const int button2 = 15;     // pin A1 - button 2
const int button3 = 16;     // pin A2 - button 3
const int contact = 17;       // pin A3 - // magnetic contact
const int tempPin = 4;        // pin A4 -  //temperature
const int buttonTST = 19;     // pin A5 - Panic Button


// Variables and initial values
int buttonState = HIGH;         // // buttons are drawn up
int contactState = LOW;         // //magnetic contact is pulled down


//  //temperature
float tempInit;
float tempC;

long previousMillis = 0;        // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 3000;           // interval at which to blink (milliseconds)

void setup() {
 autentificat = false;
 // Cod corect
 cod_corect[0] = 2;
 cod_corect[1] = 1;
 cod_corect[2] = 3;
 //  //Initialization code
 codul[0] = 0;
 codul[1] = 0;
 codul[2] = 0;
 //  //The first entry code
 primulCod = 0;

 //  //Read the initial temperature
 tempInit = analogRead(tempPin);           //read the value from the sensor
 tempInit = (5.0 * tempInit * 100.0)/1024.0;  //convert the analog data to temperature

 // initialize the pushbutton pin as an input:
 pinMode(contact, INPUT);
 pinMode(buttonTST, INPUT);
 pinMode(button1, INPUT);
 pinMode(button2, INPUT);
 pinMode(button3, INPUT);

 pinMode(LED_1, OUTPUT);
 digitalWrite(LED_1, HIGH);
 pinMode(LED_2, OUTPUT);
 digitalWrite(LED_2, HIGH);
 pinMode(TX_PIN, OUTPUT);

 SerialLCD.begin(9600);

 // initializare ePIR
 pinMode(rxPin, INPUT);
 pinMode(txPin, OUTPUT);
 mySerial.begin(9600);

 stergeLCD();
 nivelIluminare(10);
 mesaj_Armat();    // mesaj intampinare "Armat" - 5 sec pe urma trece in standby


 Serial.begin(9600);
}

void loop(){
 autentificat = false;

 // 1. Read ePIR
 mySerial.print(Cmd_a);
 if (mySerial.available() > 0) {
   char c = mySerial.read();
   if (c == 'Y') {
     while (!autentificat)
       ev_ePIR();
   }
 }

 // 2. temperature reading
 tempC = analogRead(tempPin);           //read the value from the sensor
 tempC = (5.0 * tempC * 100.0)/1024.0;  //convert the analog data to temperature
 if (tempC > (tempInit + 2.5)) {
   ev_temp();
 }

 // 3. reading magnetic contact
 contactState = digitalRead(contact);
 if (contactState == LOW) {
   while (!autentificat)
     ev_contact();
 }

 // 4. Read panic button

 buttonState = digitalRead(buttonTST);
 if (buttonState == LOW) {
   ev_but_TST();
 }


 delay(300);

}

// message "armed" for 5 seconds - 5 sec
void mesaj_Armat(void) {
 stergeLCD();
 nivelIluminare(15);
 mutaCursorLaPozitia(0, 0);
 SerialLCD.print("Gata de lucru!");
 cursorInvizibil();
 mutaCursorLaPozitia(1, 0);
 SerialLCD.print("Standby in 5 sec");
 delay(5000);  // delay 5 sec
 stareStandby();
}

// 1. ePIR event
void ev_ePIR(void) {
    stergeLCD();
  nivelIluminare(20);
 
   while (1) {
     digitalWrite(LED_1, LOW);
    digitalWrite(LED_2, HIGH);
    mutaCursorLaPozitia(0, 0);
    SerialLCD.print("ALARMA! ");
    mutaCursorLaPozitia(1, 0);
    SerialLCD.print("       MISCARE!");
    cursorInvizibil();
    delay(500);
    stergeLCD();
    digitalWrite(LED_1, HIGH);
    digitalWrite(LED_2, LOW);
    delay(500);
  }

}

// testing code introduced
void testare_cod(void) {
 if ((codul[0] == cod_corect[0]) && (codul[1] == cod_corect[1]) &&
(codul[2] == cod_corect[2])) {
   stare_Autentificat();
   autentificat = true;
   primulCod = 0;
 }
 else {
   if (primulCod == 0) {
     autentificat = false;
     primulCod++;
   }
   else {
     autentificat = false;
     stare_Efractie();
   }
 }
}

// code has been correctly
void stare_Autentificat(void) {
 stergeLCD();
 nivelIluminare(25);
 mutaCursorLaPozitia(0, 0);
 SerialLCD.print("AUTENTIFICAT!");
 cursorInvizibil();
 delay(5000);
 stergeLCD();
 stareStandby();
 //  while (1) { }
}

// code was wrong
void stare_Efractie(void) {
 ev_but_TST();
}

// 2. Event temperature
void ev_temp(void) {
 stergeLCD();
 nivelIluminare(25);

 while (1) {
   digitalWrite(LED_1, LOW);
   digitalWrite(LED_2, HIGH);
   mutaCursorLaPozitia(0, 0);
   SerialLCD.print("INCENDIU!");
   cursorInvizibil();
   delay(500);
   stergeLCD();
   digitalWrite(LED_1, HIGH);
   digitalWrite(LED_2, LOW);
   delay(500);
 }
}

// 3. Event magnetic contact
void ev_contact(void) {
 stergeLCD();
 nivelIluminare(15);
 mutaCursorLaPozitia(0, 0);
 SerialLCD.print("FEREASTRA!!!");
 cursorInvizibil();
 mutaCursorLaPozitia(1, 0);
 SerialLCD.print("COD:");

 int cifra = 0;

 int unu = 0;
 int doi = 0;
 int trei = 0;

 long time=millis();

 while (cifra < 3) {

   if(millis()-time>10000){
     stare_Efractie();
     return ;
   }



   buttonState = HIGH;
   if (unu == 0) {
     buttonState = digitalRead(button1);
     if (buttonState == LOW) {
       codul[cifra] = 1;
       cifra++;
       unu = 1;

       delay(500);
       time=millis();
     }

   }

   buttonState = HIGH;
   if (doi == 0) {
     buttonState = digitalRead(button2);
     if (buttonState == LOW) {
       codul[cifra] = 2;
       cifra++;
       doi = 1;

       delay(500);
       time=millis();
     }

   }

   buttonState = HIGH;
   if (trei == 0) {
     buttonState = digitalRead(button3);
     if (buttonState == LOW) {
       codul[cifra] = 3;
       cifra++;
       trei = 1;

       delay(500);
       time=millis();
     }

   }



 }
 testare_cod();
}

// 4. Event panic button
void ev_but_TST(void) {
 stergeLCD();
 nivelIluminare(25);

 while (1) {
   digitalWrite(LED_1, LOW);
   digitalWrite(LED_2, HIGH);
   mutaCursorLaPozitia(0, 0);
   SerialLCD.print("ALARMA!");
   cursorInvizibil();
   delay(500);
   stergeLCD();
   digitalWrite(LED_1, HIGH);
   digitalWrite(LED_2, LOW);
   delay(500);
 }
}

// LCD standby
void stareStandby (void) {
 stergeLCD();
 nivelIluminare(0);
}

//
// LCD preset functions begin
//

//level value between 0 and 29 including
void nivelIluminare(int nivel) {
 SerialLCD.print(124, BYTE);
 SerialLCD.print(128 + nivel, BYTE);
 delay(100);
}

void selecteazaLiniaUnu(){
 SerialLCD.print(254, BYTE);
 SerialLCD.print(128, BYTE);
 delay(100);
}

void selecteazaLiniaDoi(){
 SerialLCD.print(254, BYTE);
 SerialLCD.print(192, BYTE);
 delay(100);
}

void stergeLCD(){
 SerialLCD.print(254, BYTE);
 SerialLCD.print(1, BYTE);
 delay(100);
}

void mutaCursorDreapta(){
 SerialLCD.print(254, BYTE);
 SerialLCD.print(0x14, BYTE);

}

void mutaCursorStanga(){
 SerialLCD.print(254, BYTE);
 SerialLCD.print(0x10, BYTE);

}

//line 0 or 1, and the column between 0 and 16
void mutaCursorLaPozitia(int linie, int coloana){
 SerialLCD.print(254, BYTE);
 if (linie == 0) {
   SerialLCD.print(128 + coloana, BYTE);
 }
 else {
   SerialLCD.print(128 + 64 + coloana, BYTE);
 }

}

void cursorTipLinieVizibil() {
 SerialLCD.print(254, BYTE);
 SerialLCD.print(0x0E, BYTE);
 delay(100);
}

void cursorTipBaraVizibil() {
 SerialLCD.print(254, BYTE);
 SerialLCD.print(0x0D, BYTE);
 delay(100);
}

void cursorInvizibil() {
 SerialLCD.print(254, BYTE);
 SerialLCD.print(0x0C, BYTE);
 delay(100);
}

void scrollTextDreapta() {
 SerialLCD.print(254, BYTE);
 SerialLCD.print(0x1C, BYTE);
 delay(100);
}

void scrollTextStanga() {
 SerialLCD.print(254, BYTE);
 SerialLCD.print(0x18, BYTE);
 delay(100);
}

//
// END LCD preset functions
//
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13742
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


The code is too big to dive into the details. But some generic remarks.

- Try to put things that are logically one unit together in a separate file (e.g. LCD thingies)
  This improves readability and reusability
- Try to write separate functions for the PIR and other units so that loop can be fairly simple
   (ultimately they should form a class)

Loop() should look something like: (note this is pseudo code, see it as an advise how to structure the program)

Code:
NewSoftSerial PIR(tx, rx);  // something like this
int buttonPins[10] = {0,3,4,5,6};

void loop()
{
  // IF CHECK THEN ACTION pattern
  if (check_PIR() == HIGH)  Alarm("PIR");
  if (check_magnetic_contact() == HIGH) Alarm("MC");
  if (check_temperature() > 30)  Alarm("Temperature too high");
  if (check_button(1) == HIGH) Alarm("1");
  if (check_button(2) == HIGH) Alarm("2");
  if (check_button(3) == HIGH) Alarm("3");
  if (check_button(4) == HIGH) Alarm("4");
  // etc
}



int check_PIR()
{
  PIR.print(0x61);  // CMD A
  int timeout = 100;
  while (PIR.available == 0 && timeout != 0) timeout--;  // wait for answer; max check 100 times
  if (PIR.available() > 0)
  {
    char c = mySerial.read();
    if (c == 'Y' && !autentificat) return HIGH;
  }
  return LOW;
}

int check_MC()
{
   return digitalRead(MC_PIN);
}

int check_button(int b)
{
  return digitalRead( buttonPins[b] );
}

int checkTemperature()
{
   int rawTempC = analogRead(tempPin);           //read the value from the sensor
   return (500.0 * rawTempC)/1024;                //convert the analog data to temperature
}

int Alarm( char * message)
{
  // write time (millis()) + message to logfile ?

  stergeLCD();
  nivelIluminare(25);

  digitalWrite(LED_1, LOW);
  digitalWrite(LED_2, HIGH);

  mutaCursorLaPozitia(0, 0);
  SerialLCD.print(message);
  cursorInvizibil();
  delay(500);
  stergeLCD();

  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  delay(500);
}

Note 1:  I removed the while(1) construct from the Alarm() so the function returns to loop and other alarm conditions can be tested.
Note 2: the Alarm function can now be used by any sensor.

By splitting the main loop in a chain of function calls instead of list of code it becomes more maintainable

Hopes this helpes a bit ..
Rob


Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the reply I understand what to do!
 I do not know but what is the reading of the three buttons (to me, the code above, the three buttons I put in order and is 2-1-3 authentication code, the contact disk) to make out state of alarm as I do? or how can do better, LED lighting and LCD will be lit one at a time, like, 1 LED is illuminated 500 ms and ALARM LCD screen appears, all 500 ms in While LED 2 is off 500 ms, then LED 2 is lit, the LED is off and 1 blank LCD only, I want to do all this without its use, delay (), and in that time I could read the 3 buttons and if the combination of three buttons is correct to Thrace in standby! ?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13742
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I misinterpreted the use of the buttons in your program I guess it was late (just like now) and I didn't dive into it .
My pseudo code was just to show you how you could make your code easier to maintain.

The best way is to build such a program step by step. Take care that one part works before adding the next.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

in your code that you put me above definito state of alarm, as follows:
<code>int Alarm( char * message)
{
  // write time (millis()) + message to logfile ?

  stergeLCD();
  nivelIluminare(25);

  digitalWrite(LED_1, LOW);
  digitalWrite(LED_2, HIGH);

  mutaCursorLaPozitia(0, 0);
  SerialLCD.print(message);
  cursorInvizibil();
  delay(500);
  stergeLCD();

  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  delay(500);
}
</code>

I notice that you left here and delay, but I found that the alarm condition
 may be to you in the following code in this site:
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

and my code would look like this:

<code>
void stare_alarma()
{
unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if ((Stare_LED_1 == HIGH)&&(Stare_LED_2 == LOW))
   
    {
      nivelIluminare(25);  //brightness 25
      mutaCursorLaPozitia(0, 0);  // row one of the LCD
      SerialLCD.print("ALARMA! "); // My message which I want
                                   //to appear on line 1 of LCD

      mutaCursorLaPozitia(1, 0); // row 2 of the LCD
      SerialLCD.print("       MISCARE!"); // My message which I want
                                         //to appear on line 2 of LCD
 
      cursorInvizibil(); //on screen is not displayed any type of cursor
                        // (remains only text)
      Stare_LED_1 = LOW;
      Stare_LED_2 = HIGH;
    }
   
    else {
      stergeLCD(); //delete the entire contents displayed on the LCD screen
      Stare_LED_1 = HIGH;
      Stare_LED_2 = LOW;
    }
    // set the LED with the ledState of the variable:
    digitalWrite(LED_2, Stare_LED_1);
    digitalWrite(LED_1, Stare_LED_2);
  }
}
</code>

so now I showed how I used the code from the site which I put above. now I do not
 use any of a delay (). in this state if I do I still program to turn to while ()
 and inside the while () I would not use delay () or another while () like I used
at first to the contrary would create code and how I You have shown you, that way
I could get out of the state of alarm by pressing the three buttons code?
Logged

Atlanta
Offline Offline
Sr. Member
****
Karma: 1
Posts: 254
Arduino still rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Time to think again and take new ideas that do not use "delay".  When you use delay() your sketch is not listening for buttons, not doing anything but  sleeping.  Do NOT sleep in delay().

Two possible approaches are INTERRUPT and POLLING.

With interrupt your sketch spends it time in a

While (forever) {
}

which  "jumps on itself" forever and waits for an interrupt that you previously set up when a sensor changes or a button is pressed.
http://arduino.cc/en/Reference/AttachInterrupt
then you do whatever the button requires, change a state, check the validity and increment to the next digit, etc.

The other approach is POLLING where you

while (forever) {

Logged

Atlanta
Offline Offline
Sr. Member
****
Karma: 1
Posts: 254
Arduino still rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The other approach is POLLING where you

while (forever) {

 if (Check_Sensors()) {
  Take_Care_Of_Sensors()
  }

 if (Check_Buttons()) {
  Take_Care_Of_Buttons()
  }

 if (A_Timer_Has_Expired()) {
  Take_Care_Of_Expired_Timers()
  }

}

So in this case you do not wait for a button, but rather go out repeatedly  looking for an change and then take care of whatever you find.  A_Timer_Has_Expired means you remember millis() at the start event of your timer for N millis, and every time you check every timer to see if the millis() is N more than the start millis() then you change state, or time out, or whatever.


@robtillaart has some very good advice.


Good luck on your project, you are quickly learning a great deal about programming.
Logged

Sussex UK / CT USA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1028
Forums forever
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Re- "building things a bit at a time", the following might be useful...

http://sheepdogguides.com/arduino/aht1c.htm

Re- a "lock" (similar to burglar alarm), with attention to various things like switch bounce, and avoiding the need for delays...

http://sheepdogguides.com/arduino/aht8locb11.htm

Re- a fully developed story, on three long essays, on an access control solution...

http://sheepdogguides.com/arduino/FA1acpt1.htm

... and lastly, an essay about using state diagrams to help in the programming of an alarm system...

http://sheepdogguides.com/dt2n.htm
Logged

Pages: [1]   Go Up
Jump to: