roblems with uart buffer capacity teensy 2 + +

Status
Not open for further replies.

JOXTOR

New member
I have to read a SMS stored in SIM of a modem and I need more than 64 Bytes has teensy2 + +. How can I increase it?. Do not use any library for serial port, use the UART of the device itself.
Deputy program.
Thank you.
#include <Keypad.h>
#include <EEPROM.h>
#include <LiquidCrystal.h>
#include <Wire.h>
#include "RTClib.h"
#define DS1307_ADDRESS 0x68
//#define RX_BUFFER_SIZE 128
//static volatile uint8_t rx_buffer[RX_BUFFER_SIZE];
//static volatile uint8_t rx_buffer_head = 0;
//static volatile uint8_t rx_buffer_tail = 0;




DateTime now;
RTC_DS1307 RTC;
int SMS;
char customKey;
int CodiPass = 0;
int CodiPassOK = 0;
int CodiSet = 0;
int CodiSetOK = 0;
int keyN,n,keyNsc,keyNsc1,keyNsc2 ;
int key[8];
int keySClock[14];
int keySCodi1[6];
int keySCodi2[6];
int key1,key2;
int keyAct;
int Akey[6];
int Skey[6];

int DelArrancada = 3;
int activacioAL = 0;
int ALactivada;
int Aux1=0;
int Aux2=0;
int Aux3=0;
int Aux4=0;
int Aux5=0;
int Aux6=0;
int Aux7=0;
int Aux8=0;
int Aux9=0;
int Aux10=0;
int Aux11=0;
int Aux12=0;
int Aux14=0;
int Aux15=0;
int Aux16=0;
int Aux17=0;
int Aux18=0;
int Aux19=0;
int Aux20=0;
int Aux21=0;
int Aux22=0;
int Aux23=0;
int Aux24=0;

int ImpulsDia =0;
int PinImpSMS = 26;
int AuxImpSMS1;
int AuxImpSMS2;
int ImpSMS;
int ValImpSMS;
int PinSyncClock = 25;

int EEalarmaON = 1;
int EEactivacioAL = 2;
int EEOnGSM = 16;
int x, x1, x2, x3;
int PinSensorZ1 = 7;
int StaSensorZ1 = 0;
int PinSensorZ2 = 8;
int StaSensorZ2 = 0;
int OUTalarma = 0;
int SirenaState = 0;
long previousMillisSirena;
const int PinOUTalarma = 6;
int AlarmaZona1 = 0;
int AlarmaZona2 = 0;
unsigned long DelDispAlarm = 60000;
unsigned long DelSendSMS =40000;

int StartGSMpin = 4;
int OnGSM = 0;
int DecIncoming = 0;
int GSM_OK = 0;
int StatusGSMled = 5;
int StatusGSMbyte = 0;
int steep = 0;
int steep1 = 0;
int GSMpwr = 0;
char incomingSMSByte;
int DecIncomingSMS;
String incomingSMS;
int condiciowhile =0;
int hora1; int hora2;
const int ledPin = 9;
int ledState = LOW;
int ledGSMState = LOW;
long previousMillis = 0;
unsigned long previousMillisLedGSM;
unsigned long previousMillisLedStatus;

const int LCDledPin = 34;
const int OutBuzzerPin = 27;// tiene que ser 27 no 24
int BuzzerState = LOW;
unsigned long previousMillisBuzzer ;

//Timers
unsigned long TimerA;
int TimerC = 0;
unsigned long TimerB;
unsigned long TimerD;
unsigned long TimerE;
unsigned long TimerF;
unsigned long TimerG;
unsigned long TimerH;
unsigned long TimerJ;
unsigned long TimerK;
unsigned long TimerL;
unsigned long TimerImpSMS0;
unsigned long TimerImpSMS1;
unsigned long TimerStopSirena;
unsigned long TimerRebreSMS;
int MenuAct = 0;
int MenuSet = 0;
int MenuCodi = 0;
int MenuSetCodi = 0;
// Pantalla
int RS = 32;
int En = 33;
int D4 = 31;
int D5 = 30;
int D6 = 29;
int D7 = 28;

// RTC


int i = 0;
static uint8_t bin2bcd (uint8_t val) { return val + 6 * (val / 10); } //necesari per al canvi de RTC


// definir Uart (pins 2--Rx, 3--Tx)

HardwareSerial Uart = HardwareSerial();

//Definir Teclat, pins i teclas

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns


char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {13, 12, 11, 10}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {17, 16, 15, 14}; //connect to the column pinouts of the keypad

//Inicialitzem llibreria

Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
LiquidCrystal lcd(RS, En, D4, D5, D6, D7);

void setup(){
Serial.begin(9600);
Uart.begin(19200); // GSM
Wire.begin();
RTC.begin();
pinMode(ledPin, OUTPUT);
pinMode(LCDledPin, OUTPUT);
pinMode(OutBuzzerPin, OUTPUT);
pinMode(PinSensorZ1, INPUT);
pinMode(PinSensorZ2, INPUT);
pinMode(PinOUTalarma, OUTPUT);
pinMode(StartGSMpin, OUTPUT);
pinMode(StatusGSMled, OUTPUT);
pinMode(PinImpSMS, INPUT);
pinMode (PinSyncClock, OUTPUT);
lcd.begin (16,2);
digitalWrite(StartGSMpin, HIGH);
delay (2000);
digitalWrite(StartGSMpin, LOW);
TimerK = millis();
TimerImpSMS0 = millis();
TimerImpSMS1 = millis();
digitalWrite(LCDledPin, HIGH);
do {
lcd.setCursor (0,0);
lcd.print ("ARRENCANT ");
lcd.setCursor(0,1);
if (DelArrancada-(((millis()-TimerK)/1000)) == 9){
lcd.setCursor(0,1);
lcd.print(5-((millis()-TimerK)/1000));
lcd.setCursor(1,1);
lcd.print (" ");
}
lcd.print(DelArrancada-((millis()-TimerK)/1000));
} while (((millis()-TimerK)/1000) < DelArrancada);
TimerE = millis();

}

void loop(){

//Llegir EEprom - inicialitzar estat alarma al arrancar

if ( x == 0 ){
ALactivada = EEPROM.read(EEalarmaON);
activacioAL = EEPROM.read(EEactivacioAL);
if (ALactivada > 1 || activacioAL >1 ) {
ALactivada = 1;
activacioAL = 1 ;
}
OnGSM = EEPROM.read(EEOnGSM);

for(n = 3; n < 9; n++){
Akey[n-3] = EEPROM.read(n);
}

for(n = 10; n < 16; n++){
Skey[n-10] = EEPROM.read(n);
}
GSMcontrol();
x = 1;
}


// Menus..

switch (MenuAct) {
case 0 :
lcd.setCursor(0,0);
lcd.print ("VigiSyS_v1 ");
if ((millis()- TimerF) > 1000){
lcd.setCursor(0,1);
now = RTC.now();

if (now.hour() <10){
lcd.print(0);
lcd.print(now.hour());
} else{
lcd.print(now.hour());
}
lcd.print(":");
if (now.minute() <10){
lcd.print(0);
lcd.print(now.minute());
} else{
lcd.print(now.minute());
}
lcd.print(" ");
if (now.day() <10){
lcd.print(0);
lcd.print(now.day());
} else{
lcd.print(now.day());
}
lcd.print("/");
if (now.month() <10){
lcd.print(0);
lcd.print(now.month());
} else{
lcd.print(now.month());
}
lcd.print("/");
lcd.print(now.year());
TimerF = millis();

}
llegirTeclat();



if (keyAct) {
Serial.println(keyAct);

}

if (keyAct !=0 ){ //&& Aux7 == 1 veure si es important dintre del if
MenuAct = 1;
}
break;


case 1 :
lcd.setCursor (0,0);
lcd.print ("A-AL B-SET D-ESC");
lcd.setCursor(0,1);
lcd.print(" ");
CodiSetOK = 0;

llegirTeclat();


if (keyAct){
key1 = (keyAct);
if ((key1) == 65) {
MenuAct = 2;
}
if ((key1) == 66) {
CodiSet = 1;
EntrarValidarCodi ();

if ((CodiSetOK) == 1) {
CodiSet = 0;
CodiSetOK = 0;
MenuAct = 3;
}
}
if ((key1) == 67) {
MenuAct = 4;
}
if ((key1) == 68) {
MenuAct = 0;
}
}
break;

case 2 :

for (int n=0; n <=7; n++){
key[n] = 42;
Aux1=1;
}

lcd.setCursor (0,0);
lcd.print ("'A'- entrar Codi");
lcd.setCursor(0,1);
lcd.print ("Alarma ");

if ( key[0] == 42 ){
lcd.setCursor(8,1);
if ((ALactivada) == 1) {
lcd.print ("activada");
}
if ((ALactivada) == 0) {
lcd.print ("NOactivada");
}
}

llegirTeclat();
if (keyAct > 1){
key1 = (keyAct);



if ((key1) == 68 && MenuAct ==2) {
MenuAct = 1;
break;
}






// Conexio-desconexio alarma
CodiPass = 1;

EntrarValidarCodi ();
if (CodiPassOK == 1 && activacioAL == 0 ){
activacioAL = 1;
for (int n=0; n <=7; n++){
key[n] = 42;
Aux1=1;
}
delay(10);
CodiPassOK = 0;
}
if (CodiPassOK == 1 && activacioAL == 1){
activacioAL = 0;
for (int n=0; n <=7; n++){
key[n] = 42;
Aux1=1;
}
delay (10);
CodiPassOK = 0;
}
}

//Timer per retardar activacio alarma

if (Aux2 == 0) {
TimerB = millis();
Aux2 = 1;
}

if (Aux2 == 1) {

if ((millis()- TimerB) > 1000 && activacioAL == 1 && ALactivada == 0) {
TimerC++; // tiempo en segundos desde activacion alarma
Serial.println(TimerC);
Aux2 = 0;

}
}
if (TimerC > 60 && activacioAL == 1 && ALactivada == 0) {
ALactivada = 1;
x1 = 1;
}
if ( x1 == 1 ){
//Serial.println("Escribir EEprom");
EEPROM.write(EEalarmaON, ALactivada);
EEPROM.write(EEactivacioAL, activacioAL);
//Serial.println("Alarma activa ");
x1 = 0;
}

if (activacioAL == 0 && ALactivada == 1) {
ALactivada = 0;
TimerC = 0;
x2 = 1;
}
if ( x2 == 1 ){
//Serial.println("Alarma desactivada ");
//Serial.println("Escribir EEprom");
EEPROM.write(EEalarmaON, ALactivada);
EEPROM.write(EEactivacioAL, activacioAL);
x2 = 0;
}


break;


case 3 :
CodiSet = 0;
CodiSetOK = 0;
if (MenuSet == 0) {
lcd.setCursor (0,0);
lcd.print ("SET: C-BOR *-GSM");
lcd.setCursor(0,1);
lcd.print ("A-CLOCK B-CODI ");
}
if (MenuSet == 0) {
llegirTeclat();
if (keyAct){
key1 = (keyAct);
if ((key1) == 65) {
MenuSet = 1;
}
if ((key1) == 66) {
MenuSet = 2;
}
if ((key1) == 68) {
MenuAct = 1;
MenuSet = 0;
}
if ((key1) == 42) {
MenuSet = 3;
}
}
}
switch (MenuSet) {
case 1 : // set clock


if (Aux8 == 0){
lcd.clear ();
lcd.print ("Entri : hhmmss ");
lcd.setCursor(0,1);
lcd.print ("Entri : DDMMAA ");
delay (2000);
Aux8 = 1;


}

llegirTeclat();
key1 = (keyAct);
if ((keyAct)&& keyNsc<= 13) {
keySClock[keyNsc] = ((key1)- 48);
lcd.setCursor (0,0);
lcd.print (keySClock[0]);lcd.print(keySClock[1]);lcd.print (" : ");lcd.print(keySClock[2]);lcd.print(keySClock[3]);lcd.print (" : ");lcd.print(keySClock[4]);lcd.print(keySClock[5]);lcd.print(" ");
lcd.setCursor (0,1);
lcd.print (keySClock[6]);lcd.print (keySClock[7]);lcd.print (" / ");lcd.print (keySClock[8]);lcd.print (keySClock[9]);lcd.print (" / ");lcd.print (keySClock[10]);lcd.print (keySClock[11]);lcd.print (keySClock[12]);lcd.print (keySClock[13]);lcd.print(" ");
keyNsc++;

}


if (keyNsc >= 14) {


int adjS;
int adjMi;
int adjH;
int adjD;
int adjM;
int adjY;
adjH = keySClock[0]*10;
adjH = adjH + keySClock[1];
adjMi = keySClock[2]*10;
adjMi = adjMi + keySClock[3];
adjS = keySClock[4]*10;
adjS = adjS + keySClock[5];
adjD = keySClock[6]*10;
adjD = adjD + keySClock[7];
adjM = keySClock[8]*10;
adjM = adjM + keySClock[9];
adjY = keySClock[10]*1000;
adjY = adjY + keySClock[11]*100;
adjY = adjY + keySClock[12]*10;
adjY = adjY + keySClock[13];
lcd.clear ();
lcd.print ("Dades noves :");
delay (2000);
lcd.clear ();
lcd.print (adjH);lcd.print (" : ");lcd.print (adjMi);lcd.print (" : ");lcd.print (adjS);
lcd.setCursor (0,1);
lcd.print (adjD);lcd.print (" / ");lcd.print (adjM);lcd.print (" / ");lcd.print (adjY);
delay (10000);
int canvi =0;
do {
lcd.setCursor (0,0);
lcd.print ("Gravar dades ");
lcd.setCursor (0,1);
lcd.print ("A-Gravar D-ESC ");
llegirTeclat();
if (keyAct){
key1 = (keyAct);
if ((key1) == 65) {
lcd.setCursor (0,0);
lcd.print ("Canvi Hora/data ");
lcd.setCursor (0,1);
lcd.print ("Esperi ... ");
adjustRTC(adjS, adjMi, adjH, adjD, adjM, adjY);
delay (5000);
canvi = 1;
}
if ((key1) == 68) {
canvi = 1;
lcd.clear ();
lcd.print ("Canvi anulat ");
delay (2000);
}
}
}while (canvi == 0);

for ( i =0 ; i <= 13; i++){
keySClock = 0;
}
keyNsc =0;
MenuSet = 0;
Aux8 = 0;
Aux9 = 0;

}
break;

case 2 : // canvi codis usuari i set.
if (MenuSetCodi == 0) {
lcd.setCursor(0,0);
lcd.print ("Canvi Codi ");
lcd.setCursor(0,1);
lcd.print ("A-PASS B-SET");
}

llegirTeclat();
if (keyAct){
key1 = (keyAct);
if ((key1) == 65) {
Aux14=0;
MenuSetCodi = 1;
}
if ((key1) == 66) {
MenuSetCodi = 2;
}
if ((key1) == 68) {
MenuSetCodi = 0;
MenuSet = 0;
}
}
switch (MenuSetCodi) {
case 1 :

if (Aux10 == 0){
if (Aux14 == 0){
lcd.setCursor(0,0);
lcd.print ("Act. Codi PASS: ");
lcd.setCursor(0,1);
lcd.print (Akey[0]);lcd.print (Akey[1]);lcd.print (Akey[2]);lcd.print (Akey[3]);lcd.print (Akey[4]);lcd.print (Akey[5]);lcd.print (" ");
delay (2000);
Aux14 = 1;
}
lcd.setCursor(0,0);
lcd.print ("Nou Codi PASS: ");
lcd.setCursor(0,1);
lcd.print (" ");


llegirTeclat();
key1 = (keyAct);
}
if ((keyAct)&& Aux10 ==0) {
keySCodi1[keyNsc1] = ((key1)- 48);
lcd.setCursor (0,1);
lcd.print (keySCodi1[0]);lcd.print(keySCodi1[1]);lcd.print(keySCodi1[2]);lcd.print(keySCodi1[3]);lcd.print(keySCodi1[4]);lcd.print(keySCodi1[5]);
keyNsc1++;
}

if (keyNsc1 >5 && Aux10 ==0) {
delay (2000);
lcd.setCursor(0,0);
lcd.print ("Torni entrar cod");
//Serial.print ("479 ");Serial.print (Aux10);Serial.println (Aux11);
delay (50);
lcd.setCursor(0,1);
lcd.print (" ");
Aux10 = 1;
}
while (keyNsc2 < 6 && Aux10 ==1){
llegirTeclat();
key2 = (keyAct);
if ((keyAct) && Aux10 == 1) {
keySCodi2[keyNsc2] = ((key2)- 48);
keyNsc2++;
}
}
if (keyNsc2 >5) {
lcd.setCursor (0,1);
lcd.print (keySCodi2[0]);lcd.print(keySCodi2[1]);lcd.print(keySCodi2[2]);lcd.print(keySCodi2[3]);lcd.print(keySCodi2[4]);lcd.print(keySCodi2[5]);
delay (2000);
//Serial.print ("488");Serial.println (Aux10);
Aux11 = 1;
}
if ((Aux10 == 1) && (Aux11 == 1) && (keySCodi1[0]==keySCodi2[0]) && (keySCodi1[1]==keySCodi2[1]) && (keySCodi1[2]==keySCodi2[2]) && (keySCodi1[3]==keySCodi2[3]) && (keySCodi1[4]==keySCodi2[4]) && (keySCodi1[5]==keySCodi2[5])) {
for(n = 3; n < 9; n++){
EEPROM.write(n, keySCodi1[n-3]);
}

lcd.setCursor(0,0);
lcd.print ("Codi canviat ");
lcd.setCursor(0,1);
lcd.print (" ");
Aux12 = 1;
x = 0;
delay (2000);
}
else if (Aux12 == 0 && Aux10 == 1 && Aux11 == 1){
lcd.setCursor(0,0);
lcd.print ("Codis diferents ");
lcd.setCursor(0,1);
lcd.print (" ");
Aux12 = 1;
delay (2000);
}
if (Aux12 == 1){
Aux10 = 0;
Aux11 = 0;
keyNsc1 = 0;
keyNsc2 = 0;
for ( i =0 ; i < 5; i++){
keySCodi1 = 0;
keySCodi2 = 0;
// Serial.println ("510");
}
Aux12 = 0;
MenuSetCodi = 0;
}


break;
case 2 :

if (Aux10 == 0){
if (Aux14 == 0){
lcd.setCursor(0,0);
lcd.print ("Act. Codi SET: ");
lcd.setCursor(0,1);
lcd.print (Skey[0]);lcd.print (Skey[1]);lcd.print (Skey[2]);lcd.print (Skey[3]);lcd.print (Skey[4]);lcd.print (Skey[5]);lcd.print (" ");
delay (2000);
Aux14 = 1;
}
lcd.setCursor(0,0);
lcd.print ("Nou Codi SET: ");
lcd.setCursor(0,1);
lcd.print (" ");


llegirTeclat();
key1 = (keyAct);
}
if ((keyAct)&& Aux10 ==0) {
keySCodi1[keyNsc1] = ((key1)- 48);
lcd.setCursor (0,1);
lcd.print (keySCodi1[0]);lcd.print(keySCodi1[1]);lcd.print(keySCodi1[2]);lcd.print(keySCodi1[3]);lcd.print(keySCodi1[4]);lcd.print(keySCodi1[5]);
keyNsc1++;
}

if (keyNsc1 >5 && Aux10 ==0) {
delay (2000);
lcd.setCursor(0,0);
lcd.print ("Torni entrar cod");
//Serial.print ("584 ");Serial.print (Aux10);Serial.println (Aux11);
delay (50);
lcd.setCursor(0,1);
lcd.print (" ");
Aux10 = 1;
}
while (keyNsc2 < 6 && Aux10 ==1){
llegirTeclat();
key2 = (keyAct);
if ((keyAct) && Aux10 == 1) {
keySCodi2[keyNsc2] = ((key2)- 48);
keyNsc2++;
}
}
if (keyNsc2 >5) {
lcd.setCursor (0,1);
lcd.print (keySCodi2[0]);lcd.print(keySCodi2[1]);lcd.print(keySCodi2[2]);lcd.print(keySCodi2[3]);lcd.print(keySCodi2[4]);lcd.print(keySCodi2[5]);
delay (2000);
//Serial.print ("488");Serial.println (Aux10);
Aux11 = 1;
}
if ((Aux10 == 1) && (Aux11 == 1) && (keySCodi1[0]==keySCodi2[0]) && (keySCodi1[1]==keySCodi2[1]) && (keySCodi1[2]==keySCodi2[2]) && (keySCodi1[3]==keySCodi2[3]) && (keySCodi1[4]==keySCodi2[4]) && (keySCodi1[5]==keySCodi2[5])) {
for(n = 10; n < 16; n++){
EEPROM.write(n, keySCodi1[n-10]);
}

lcd.setCursor(0,0);
lcd.print ("Codi canviat ");
lcd.setCursor(0,1);
lcd.print (" ");
Aux12 = 1;
x = 0;
delay (2000);
}
else if (Aux12 == 0 && Aux10 == 1 && Aux11 == 1){
lcd.setCursor(0,0);
lcd.print ("Codis diferents ");
lcd.setCursor(0,1);
lcd.print (" ");
Aux12 = 1;
delay (2000);
}
if (Aux12 == 1){
Aux10 = 0;
Aux11 = 0;
keyNsc1 = 0;
keyNsc2 = 0;
for ( i =0 ; i < 5; i++){
keySCodi1 = 0;
keySCodi2 = 0;
//Serial.println (" 634");
}
Aux12 = 0;
MenuSetCodi = 0;
}


break;
}//Final switch MenuCodi
break;
case 3 :
lcd.setCursor(0,0);
lcd.print ("Activar GSM - A ");
lcd.setCursor(0,1);
lcd.print ("Deactivar GSM- D");

llegirTeclat();
if (keyAct){
key1 = (keyAct);
if ((key1) == 65) {
OnGSM = 1;
MenuSet = 0;
}
if ((key1) == 68) {
OnGSM = 0;
steep = 0;
MenuSet = 0;
}
GSMcontrol();
}
EEPROM.write(16, OnGSM);

break;

}//Final switch MenuSet
}//final switch MenuAct

// Control led estat alarma. Intermitent activanse/disparada , Fix Activada

if (activacioAL == 1 && ALactivada == 0){
const long intervalLedStatus = 800;
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > intervalLedStatus) {
previousMillis = currentMillis;

if (ledState == LOW){
ledState = HIGH;
BuzzerState = HIGH;
}
else{
ledState = LOW;
BuzzerState = LOW;


}
}
}
if (OUTalarma == 1 || AlarmaZona1 == 1 || AlarmaZona1 == 2 ){
const long intervalLedStatus = 300;
unsigned long currentMillisLedStatus = millis();

if(currentMillisLedStatus - previousMillisLedStatus > intervalLedStatus) {
previousMillisLedStatus = currentMillisLedStatus;

if (ledState == LOW){
ledState = HIGH;

}
else{
ledState = LOW;



}
}
}
if (activacioAL == 1 && ALactivada == 1 && OUTalarma == 0){
ledState = HIGH;
}
if (activacioAL == 0 && ALactivada == 0){
ledState = LOW;
}
digitalWrite(ledPin, ledState);

// Control led estat GSM. Intermitent fallo, Fix connectat

if (StatusGSMbyte == 0) ledGSMState = LOW ;
if (StatusGSMbyte == 1) ledGSMState = HIGH;
if (StatusGSMbyte == 2) {
const long intervalLedGSM = 1000;
unsigned long currentMillisLedGSM = millis();

if(currentMillisLedGSM - previousMillisLedGSM > intervalLedGSM) {
previousMillisLedGSM = currentMillisLedGSM;
if (ledGSMState == LOW){
ledGSMState = HIGH;
}
else{
ledGSMState = LOW;
}
}
}
digitalWrite(StatusGSMled, ledGSMState);

// Control BUZZER



if ((AlarmaZona1 == 1 || AlarmaZona2 == 1) && (OUTalarma == 0)){
const long intervalBuzz = 300;
unsigned long currentMillisBuzzer = millis();

if(currentMillisBuzzer - previousMillisBuzzer > intervalBuzz) {
previousMillisBuzzer = currentMillisBuzzer;

if (BuzzerState == LOW){
BuzzerState = HIGH;

}
else{
BuzzerState = LOW;


}
}
}
if ( (ALactivada == 1 && OUTalarma == 1) || Aux6 == 1){
BuzzerState = LOW;
}
if ( (ALactivada == 1 && AlarmaZona1 == 0 && AlarmaZona2 == 0) ||(activacioAL == 0 && ALactivada == 0)){
BuzzerState = LOW;
}
digitalWrite(OutBuzzerPin, BuzzerState);

// LCD Display


if ((keyAct)){
TimerE = millis();
digitalWrite(LCDledPin, HIGH);
Aux7 = 1;
}
else if ((millis() - TimerE) > 120000){
digitalWrite(LCDledPin, LOW);
Aux7 = 0;
MenuAct = 0;
}

// Control disparo alarma

StaSensorZ1 = digitalRead (PinSensorZ1);
StaSensorZ2 = digitalRead (PinSensorZ2);

if (ALactivada == 1){
if (StaSensorZ1 == 0) {
if (Aux15 == 0 && AlarmaZona1 == 0) {
TimerG = millis();
Aux16 = Aux16 + 1;
Aux15 = 1;
}
TimerD = millis();
Aux17 = 0;
}
if (((millis() - TimerG) > 3000) ){
Aux15 = 0;
}
if (StaSensorZ2 == 0) {
if (Aux18 == 0 && AlarmaZona2 == 0) {
TimerH = millis();
Aux19 = Aux19 + 1;
Aux18 = 1;
}
TimerD = millis();
Aux17 = 0;
}
if (((millis() - TimerH) > 3000) ){
Aux18 = 0;
}
if (StaSensorZ1 == 1 && StaSensorZ2 == 1) {
if (Aux17 == 0) {
TimerD = millis();
Aux17 = 1;
}
}
if (((millis() - TimerD) > 30000) ){
Aux15 = 0;
Aux16 = 0;
Aux17 = 0;
Aux18 = 0;
Aux19 = 0;
}

if (Aux16 > 2){
AlarmaZona1 = 1;
}

if (Aux19 > 2){
AlarmaZona2 = 1;
}

if ((AlarmaZona1 ==1 || AlarmaZona2 == 1) && Aux20 == 0){
TimerJ = millis();
Aux20 = 1;
}
}else {
AlarmaZona1 = 0;
AlarmaZona2 = 0;
OUTalarma = 0;
Aux15 = 0;
Aux16 = 0;
Aux17 = 0;
Aux18 = 0;
Aux19 = 0;
Aux20 = 0;
Aux21 = 0;
Aux22 = 0;
TimerJ = millis();
}
if (AlarmaZona1 == 0 && AlarmaZona2 == 0 || OUTalarma == 1){
TimerJ = millis();
}

if((millis() - TimerJ) > DelDispAlarm){
OUTalarma = 1;
// Serial.println (AlarmaZona1);
// Serial.println (AlarmaZona2);
// Serial.println (Aux21);
//Serial.println (Aux22);
}
// Flip-flop sirena alarma disparada

if (OUTalarma == 1 && steep1 < 10){
const long intervalSirena = 30000;


unsigned long currentMillisSirena = millis();
if(currentMillisSirena - previousMillisSirena > intervalSirena) {
previousMillisSirena = currentMillisSirena;

if (SirenaState == HIGH){
SirenaState = LOW;
steep1 = steep1 + 1;
Serial.print ("steep1 "); Serial.println (steep1);
}
else{
SirenaState = HIGH;


}
}
}
if ( steep1 < 10) {
TimerStopSirena = millis ();
}
if ((millis () - TimerStopSirena) > 600000) steep1 = 0;


if (OUTalarma == 0 || steep1 > 9) SirenaState = LOW;

digitalWrite(PinOUTalarma, SirenaState);
// Serial.print ("Aux16 "); Serial.println (Aux16); Serial.print (" SensorZ1 "); Serial.println (StaSensorZ1);Serial.print (" SensorZ2 "); Serial.println (StaSensorZ2); Serial.print (" AlarmaZona1 ");
//Serial.println (AlarmaZona1);Serial.print (" AlarmaZona2 "); Serial.println (AlarmaZona2);Serial.print (" OUTalarma "); Serial.println (OUTalarma); Serial.println((millis() - TimerJ));Serial.print (" BuzzerState "); Serial.println (BuzzerState);

if (AlarmaZona1 == 1 && OUTalarma == 1 && Aux21 ==0){
//SMS = "Alarma zona 1\r";
SMS = 1;
//Serial.println (SMS);
SendSMS();
delay (5000);
Aux21 = 1;
}

if (AlarmaZona2 == 1 && OUTalarma == 1 && Aux22 == 0){
//char SMS[] = "Alarma zona 2\r";
SMS = 2;
// Serial.println (SMS);
SendSMS();
delay (5000);
Aux22 = 1;
}
// enviar trucada avis.

if (OUTalarma == 0 || ((millis() - TimerL) > (DelDispAlarm + 1800000))) {
TimerL = millis();
Aux23 = 0;
}
if (OUTalarma == 1 && ((millis() - TimerL) > (DelDispAlarm + 5000)) && Aux23 == 0){
Aux23 = 1;
Serial.println ((millis() - TimerL));
SendCall ();
}


// per enviar SMS control

now = RTC.now();
if (((now.day () == 8) || (now.day () == 18) || (now.day () == 28)) && (now.hour() == 12) && (now.minute() == 0) && (now.second() == 0)){
SMS = 3;
//Serial.println (SMS);
SendSMS();
delay (5000);
}

// Entrega impulsos dia semana a una hora establecida ""now.dayOfWeek""

now = RTC.now();
ImpulsDia = (now.dayOfWeek ())+1;
//Serial.println (ImpulsDia);
if ( (now.hour() == 3) && (now.minute() == 55) && (now.second() < 2)){
for (int n=0; n < ImpulsDia; n++){
digitalWrite(PinSyncClock, HIGH);
delay (500);
digitalWrite(PinSyncClock, LOW);
delay (500);
}
}


//Control GSM

now = RTC.now();
hora1 = now.hour();
if (hora1 != hora2){
TimerE = millis();
digitalWrite(LCDledPin, HIGH);
GSMcontrol();
hora2 = hora1;
}


// Control impulsos SMS

int ValPinImpSMS = digitalRead (PinImpSMS);

if (ValPinImpSMS == 0) {
TimerImpSMS1 = millis();
ValImpSMS = 0;
}

if (ValPinImpSMS == 1) TimerImpSMS0 = millis();

if (millis() - TimerImpSMS1 > 300 ){
ValImpSMS = 1;
}

if (ValImpSMS == 1 && AuxImpSMS1 == 0){
AuxImpSMS1 = 1;
AuxImpSMS2 = 0;
ImpSMS = ImpSMS+1;
}

if ((millis() - TimerImpSMS0) > 200) AuxImpSMS1 = 0;

if (((millis() - TimerImpSMS0) > 5000) && (AuxImpSMS2 == 0) && ImpSMS > 0 ){
AuxImpSMS2 = 1;

SMS = ImpSMS+10;
//SendSMS();
ImpSMS = 0;
}
if ((millis() - TimerImpSMS0) > 10000) TimerImpSMS0 = millis() ;
if ((millis() - TimerImpSMS1) > 10000) TimerImpSMS1 = millis() ;
// Serial.print ("Ton = "); Serial.println (millis() - TimerImpSMS1);
//Serial.print ("Toff = "); Serial.println (millis() - TimerImpSMS0);
//Serial.print ("ValImp = "); Serial.println (ValImpSMS);
//Serial.print ("Imp = "); Serial.println (ImpSMS);
//Serial.print ("AuxImpSMS1 = "); Serial.println (AuxImpSMS1);

// Serial.print("GSMpwr: ");
//Serial.println(GSMpwr);



//Uart.print("AT+CMGF=1;\r");
delay(200);
//Uart.print("AT+CNMI=3,3,0,0;\r");
delay(200);

//if(Uart.available() >0)
//Aux24=0;
if (((millis()-TimerRebreSMS)>30000)){
RebreSMS ();
}


}// Fin loop



// LLegir tecla
void llegirTeclat() {
customKey = customKeypad.getKey();
keyAct = customKey;


if (keyAct){
Serial.print ("teclejat "); Serial.print (keyAct);Serial.print (" .. ");Serial.println (customKey);
TimerE = millis();
}


}

// Captura i validacio codi
void EntrarValidarCodi (){
//Serial.println ("458");
//Serial.println (keyN);
CodiPassOK = 0;
CodiSetOK = 0;
for (int n=0; n <=7; n++){
key[n] = 42;
}
while (keyN < 6) {
BuzzerState = LOW;
digitalWrite(OutBuzzerPin, BuzzerState);
// Temps maxim per introduir codi
if (keyN == 0 && Aux5 == 0){
TimerA = millis();
Aux5 = 1;
}

if (millis() - TimerA > 7000) {
keyN = 0;
lcd.setCursor(0, 0);
lcd.print("Temps max. ");
for (int n=0; n <=7; n++){
key[n] = 42;
}
delay (1000);
lcd.setCursor(0, 0);
lcd.print("Torni teclejar ");
delay (2000);

Aux4 = 0;
Aux5 = 0;
Aux6 = 0;
break;
}

llegirTeclat();
lcd.setCursor(0, 0);
lcd.print("Entri Codi : ");
if (Aux6 == 0) {
lcd.setCursor(0, 1);
lcd.print(" ");
Aux6 = 1;
}

if (keyAct){
lcd.clear ();

key[keyN] = (keyAct)-48;
if ((keyAct) > 47 && (keyAct) < 57 && (keyN) == 0){
lcd.setCursor(0, 1);
lcd.print(key[0]);
}
if ((keyAct) > 47 && (keyAct) < 57 && (keyN) == 1 ){
lcd.setCursor(1, 1);
lcd.print(key[1]);
}
if ((keyAct) > 47 && (keyAct) < 57 && (keyN) == 2 ){
lcd.setCursor(2, 1);
lcd.print(key[2]);
}
if ((keyAct) > 47 && (keyAct) < 57 && (keyN) == 3){
lcd.setCursor(3, 1);
lcd.print(key[3]);
}
if ((keyAct) > 47 && (keyAct) < 57 && (keyN) == 4 ){
lcd.setCursor(4, 1);
lcd.print(key[4]);
}
if ((keyAct) > 47 && (keyAct) < 57 && (keyN) == 5 ){
lcd.setCursor(5, 1);
lcd.print(key[5]);
}

keyN++;
if (keyN > 5) {
Aux6 = 0;
}
}

}





//Serial.println ("554");

if (key[0] == Akey[0] && key[1] == Akey[1] && key[2] == Akey[2] && key[3] == Akey[3] && key[4] == Akey[4] && key[5] == Akey[5] && CodiPass == 1) {
CodiPassOK = 1;
CodiPass = 0;
}
else {
CodiPassOK = 0;
CodiPass = 0;
}
if (key[0] == Skey[0] && key[1] == Skey[1] && key[2] == Skey[2] && key[3] == Skey[3] && key[4] == Skey[4] && key[5] == Skey[5] && CodiSet == 1) {
CodiSetOK = 1;
CodiSet = 0;
}
else {
CodiSetOK = 0;
CodiSet = 0;
}
for (int n=0; n <=7; n++){
key[n] = 42;
}


// Verificacio Codi entrat

if ((keyN == 6) && (CodiPassOK == 0) && (CodiSetOK == 0 )) {
lcd.setCursor(0, 0);
lcd.print("Codi no O.K. ");
delay (1000);
lcd.setCursor(0, 0);
lcd.print("Torni teclejar ");
delay (1000);
lcd.clear();
Aux4 = 0;
Aux5 = 0;
Aux6 = 0;
keyN = 0;
}
if ((keyN == 6) && ((CodiPassOK == 1) || (CodiSetOK == 1) )) {
lcd.setCursor(0, 0);
lcd.print("Codi O.K. ");
delay (1000);
lcd.clear();
Aux4 = 0;
Aux5 = 0;
Aux6 = 0;
keyN = 0;
}
}

//Escriure nova hora-data a RTC

void adjustRTC(int adjS, int adjMi, int adjH, int adjD, int adjM, int adjY){
int r = 0;
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(r);
Wire.write(bin2bcd(adjS));
Wire.write(bin2bcd(adjMi));
Wire.write(bin2bcd(adjH));
Wire.write(bin2bcd(0));
Wire.write(bin2bcd(adjD));
Wire.write(bin2bcd(adjM));
Wire.write(bin2bcd(adjY - 2000));
Wire.write(r);
Wire.endTransmission();
}

// enviar SMS

void SendSMS(){

Uart.print("\r");
delay(1000);
Uart.print("AT+CMGF=1\r");
delay(1000);




Uart.print("AT+CMGS=\"659486701\"\r");


delay(1000);
switch (SMS) {
case 1:
Uart.print("Alarma zona 1\r");
break;
case 2:
Uart.print("Alarma zona 2\r");
break;
case 3:
Uart.print("GSM Alarma OK.\r");
break;
case 11:
Uart.print("Nivell minim Gasoil.\r");
break;
} // fin switch
delay(1000);
Uart.print(26,BYTE); //igual a Ctrl+Z

BuzzerState = HIGH;
digitalWrite(OutBuzzerPin, BuzzerState);
delay(2000);
BuzzerState = LOW;
digitalWrite(OutBuzzerPin, BuzzerState);
}


// enviar trucada

void SendCall(){

Uart.print("ATD659486701;\r");
delay (60000);
Uart.print("ATH\r");
delay (5000);
Uart.print("ATD606180268;\r");
delay (60000);
Uart.print("ATH\r");
delay (5000);
BuzzerState = HIGH;
digitalWrite(OutBuzzerPin, BuzzerState);
delay(1000);
BuzzerState = LOW;
digitalWrite(OutBuzzerPin, BuzzerState);
}


//Rebre SMS

void RebreSMS (){
TimerRebreSMS = millis ();
//Serial.println ("RebreSMS");
//if (Uart.available() ==0) Aux24=0;
//
if (Aux24 == 0){
Serial.println ("RebreSMS");
incomingSMSByte = 0;
Uart.write("AT+CMGF=1\r");
delay(1000);
// Uart.print("AT+CPMS=SM,SM,SM;\r");
//delay(1000);
Uart.clear();
Uart.print("AT+CMGR=1\r");
Uart.flush();
delay(1000);
Aux24 = 1;
Serial.println ("ConfigRebreSMS");
Serial.println (Uart.available());
delay (0);
}
char incomingSMSByte;
String incomingSMS;
//int LongPort = Uart.available();
//Serial.println(LongPort);
delay (0);

while (Uart.available() > 1) {
//Uart.flush();
incomingSMSByte = Uart.read();
// Serial.print(incomingSMSByte);
if (incomingSMSByte == '#'){
delay (50);
while (incomingSMSByte != '*'){
incomingSMSByte = Uart.read();
delay (50);
//Serial.println(incomingSMSByte);
incomingSMS.concat(incomingSMSByte);
}
}
// Uart.print("UART received:");
//Uart.println(incomingByte, DEC);

}
Serial.print("RECIBIDO: ");Serial.println(incomingSMS);
/*
//if(Uart.available() >0) {



//incomingSMS = "";
//while(Uart.available()) {
//int Port = (Uart.available());
// Serial.print ("Port: ");
//Serial.println (Port);
//incomingSMSByte = Uart.read();
//delay (0);
//Serial.print ("incomingSMSByte: ");
//Serial.print (incomingSMSByte);
//if (incomingSMSByte ) { //== '#'
delay(0);
//while(Uart.available() > 0) {
incomingSMSByte = Uart.read();
Serial.print ("incomingSMSByte: ");
Serial.print (incomingSMSByte);
// DecIncomingSMS = incomingSMSByte;
//if ((DecIncomingSMS > 47) && (DecIncomingSMS < 58)){
incomingSMS.concat(incomingSMSByte);
//Serial.println ("SMS rebut: ");
//Serial.print ("DecInco ");
//Serial.println (DecIncomingSMS);
//Serial.print ("inco ");
//Serial.println (incomingSMS);
//}
//if (DecIncoming == 42) {
//Uart.println("AT+CMGD=1,4"); // Borrar tots SMS
//Aux24 = 0;
//}
// }
// }
//}

/*if (incomingSMS == "1250"){
BuzzerState = HIGH;
digitalWrite(OutBuzzerPin, BuzzerState);
delay(1000);
BuzzerState = LOW;
digitalWrite(OutBuzzerPin, BuzzerState);
}


//}
Serial.print ("inco ");
Serial.println (incomingSMS);
*/
}





// Control tarja GSM

void GSMcontrol() {


Serial.print ("OnGSM : ");Serial.println (OnGSM);
if(steep < 4) {
do
{

Uart.print("ATE0\r");
delay(1000);
Uart.clear();
lcd.setCursor(0,0);
lcd.print (" CONTROL GSM ");
lcd.setCursor(0,1);
lcd.print (" ");
char incomingByte;
String incomingString = "";
Uart.print("AT\r"); //+COPS?
delay(1500);
while(Uart.available() > 0) {
incomingByte = Uart.read();
DecIncoming = incomingByte;
if ((DecIncoming > 64) && (DecIncoming < 91)){
incomingString.concat(incomingByte);
}
}

delay(10);
if (incomingString == "OK") { //COPS
Serial.print("GSM on: ");
Serial.println(incomingString);
GSMpwr = 1;
}else if (incomingString != "OK") {
GSMpwr = 0;
}
Serial.print("GSMpwr: ");
Serial.println(GSMpwr);
incomingString = "";
Uart.clear();
Uart.print("AT+COPS?\r"); //+COPS?
//Serial.print("AT+COPS?\r: ");
delay(2000);
while(Uart.available() > 0) {
incomingByte = Uart.read();
DecIncoming = incomingByte;
if ((DecIncoming == 71) || (DecIncoming == 73) || (DecIncoming == 89) || (DecIncoming == 103) || (DecIncoming == 105) || (DecIncoming == 121)){
incomingString.concat(incomingByte);

}
}
Serial.print("AT+COPS?\r: ");Serial.println(incomingString);
delay(10);
if ((incomingString == "YIG")||(incomingString == "yig")||(incomingString == "Yig")) { //COPS
Serial.print("GSM con red ");
Serial.println(incomingString);
GSM_OK = 1;
}else if ((incomingString != "YIG") && (incomingString != "yig") && (incomingString != "Yig")) {
Serial.println(incomingString);
Serial.println("GSM sin red: ");
GSM_OK = 0;
}
Serial.print("GSM_OK: ");Serial.println(GSM_OK);
Serial.print("steep error: ");
Serial.println(steep);

switch (GSM_OK){
case 1:
if(OnGSM == 1){
StatusGSMbyte = 1;
break;
}else if (OnGSM == 0 && GSMpwr == 1){
digitalWrite(StartGSMpin, HIGH);
delay (2000);
digitalWrite(StartGSMpin, LOW);
delay (10);

}
break;
case 0:
Serial.print("GSMpwr: ");
Serial.println(GSMpwr);
if(OnGSM == 0 && GSMpwr == 0){
StatusGSMbyte = 0;

}else if (OnGSM == 1){
digitalWrite(StartGSMpin, HIGH);
delay (2000);
digitalWrite(StartGSMpin, LOW);
delay (15000);
steep = steep+1;
break;
}
if((OnGSM == 0 && GSMpwr == 1) ) {
digitalWrite(StartGSMpin, HIGH);
delay (2000);
digitalWrite(StartGSMpin, LOW);
delay (10);
break;
}

break;



}

condiciowhile = (((GSM_OK != 1 && OnGSM == 1) || (GSM_OK != 0 && OnGSM == 0))&& steep < 4) ;
Serial.print("condiciowhile: ");
Serial.println(condiciowhile);
} while (condiciowhile);

}


// aviso fallo en GSM

if (steep > 3) StatusGSMbyte = 2;

// Led status GSM

switch (StatusGSMbyte){

case 0:
digitalWrite(StatusGSMled, LOW);
lcd.setCursor(0,0);
lcd.print (" GSM ");
lcd.setCursor(0,1);
lcd.print (" PARAT ");
delay (2000);
break;

case 1:
digitalWrite(StatusGSMled, HIGH);
lcd.setCursor(0,0);
lcd.print (" GSM CONNECTAT ");
lcd.setCursor(0,1);
lcd.print (" A OPERADOR ");
delay (2000);
break;

case 2:
lcd.setCursor(0,0);
lcd.print (" GSM ");
lcd.setCursor(0,1);
lcd.print (" ERROR CONEXIO ");
delay (5000);
Serial.print("StatusGSMbyte error GSM: ");
Serial.println(StatusGSMbyte);

break;
}

}
 
Status
Not open for further replies.
Back
Top