Help with logic

Hi guys,

I'm doing a project of Alarm System in arduino mega with TFT LCD panel, PIR sensor, buzzer and led and i'm having problem with the logic.

#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#define MINPRESSURE 200
#define MAXPRESSURE 1000

const int XP = 6, XM = A2, YP = A1, YM = 7; //ID=0x9341
const int TS_LEFT = 907, TS_RT = 136, TS_TOP = 942, TS_BOT = 139;

const int buzzer = A14;
const int PIR = A13;
const int led = A15;
const int aux;

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 100);

Adafruit_GFX_Button on_btn, off_btn;

int pixel_x, pixel_y;     //Touch_getXY() updates global vars
bool Touch_getXY(void)
{
    TSPoint p = ts.getPoint();
    pinMode(YP, OUTPUT);      //restore shared pins
    pinMode(XM, OUTPUT);
    digitalWrite(YP, HIGH);   //because TFT control pins
    digitalWrite(XM, HIGH);
    bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
    if (pressed) {
        pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
        pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
    }
    return pressed;
}

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

void setup(void)
{
    Serial.begin(9600);
    uint16_t ID = tft.readID();
    Serial.print("TFT ID = 0x");
    Serial.println(ID, HEX);
    Serial.println("Calibrate for your Touch Panel");
    if (ID == 0xD3D3) ID = 0x9486; // write-only shield
    tft.begin(ID);
    tft.setRotation(0);            //PORTRAIT
    tft.fillScreen(BLACK);
    on_btn.initButton(&tft,  80, 330, 130, 40, WHITE, CYAN, BLACK, "ARMAR", 2);
    off_btn.initButton(&tft, 240, 330, 130, 40, WHITE, CYAN, BLACK, "DESARM.", 2);
    on_btn.drawButton(false);
    off_btn.drawButton(false);
//    tft.fillRect(60, 100, 180, 100, RED);

  // declarando sensor como entrada
  pinMode(PIR, INPUT);
  //Definindo o pino buzzer como de saída.
  pinMode(buzzer,OUTPUT);
  // Colocamos o pino 12 do Arduino como OUTPUT (saída)
  pinMode(led, OUTPUT);
}

void loop(void)
{
    bool down = Touch_getXY();

    // leitura do sensor
    int leitura = digitalRead(PIR);
    //delay(100);
      
    on_btn.press(down && on_btn.contains(pixel_x, pixel_y));
    off_btn.press(down && off_btn.contains(pixel_x, pixel_y));
    if (on_btn.justReleased())
        on_btn.drawButton();
    if (off_btn.justReleased())
        off_btn.drawButton();
    if (on_btn.justPressed()) {
        on_btn.drawButton(true);
        tft.fillRect(60, 100, 180, 100, GREEN);
        delay (100);
        tft.fillRect(60, 100, 180, 100, BLACK);
        int aux = 98;
    }
    if (off_btn.justPressed()) {
        off_btn.drawButton(true);
        tft.fillRect(60, 100, 180, 100, RED);
        delay (100);
        tft.fillRect(60, 100, 180, 100, BLACK);
        int aux = 99;
    }
    if (aux == 98){
    if (leitura == HIGH) {
    //Ligando o buzzer com uma frequencia de 1500 hz.
    tone(buzzer,1500);   
    delay(10);
    noTone(buzzer);
    delay(10);
    tone(buzzer,1800);
    delay(10);
    noTone(buzzer); 
    // Ativamos o pino 12 (colocando 5v nele)  
    digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(100);                       // wait for a second
    digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
    delay(100);                       // wait for a second
    }
    } else if (aux == 99){
    noTone (buzzer);
    digitalWrite(led, LOW);
    }
}

I'm trying to use an aux variable to activate the other modules but in the way that I put in there, doesn't work.

This project has copy paste a lot and I'm beginner with arduino programming.

Can anyone help, please?

Thanks in advance.

      int aux = 99;

}
    if (aux == 98){

Every time you say "int" you are saying "please create a new variable of type int"...

The first line I quoted is inside a {} block. That means it is only visible inside that block.

The last line I quoted is referring to another variable which has the same name but different visibility.

the Above !!
and

tone(buzzer,1500);   
    delay(10);
    noTone(buzzer);
    delay(10);
    tone(buzzer,1800);
    delay(10);
    noTone(buzzer);

these beeps are very short btw..

delay(100);                      // wait for a secondyou are only waiting for 0.1 Seconds

This project has copy paste a lot and I'm beginner with arduino programming.

I guess you have to start somewhere but starting with complex code you don't understand seems like a recipe for quickly becoming disillusioned. I suggest you start with the examples in the IDE, on this forum and elsewhere until you have learned enough to understand the code.

Please also realise that delay(); is very bad and should not be used. Ever. I know it's in some of the examples and I wish it wasn't as it teaches people new to programming that it's OK to use; it's not. If delay solves a problem then it's hiding something else much worse.

Enjoy.

PerryBebbington:
Please also realise that delay(); is very bad and should not be used. Ever. I know it's in some of the examples and I wish it wasn't as it teaches people new to programming that it's OK to use; it's not. If delay solves a problem then it's hiding something else much worse.

I do wish people wouldn't keep saying this. There are many places where delay() is perfectly usable and for someone new to programming it's a lot easier to understand than the initially illogical looking convolutions needed to use millis() etc. I have at least a dozen (fairly simple admittedly) projects working for people that use delay() exclusively.

Sometimes delay() is all you need and I suspect that telling people they MUST NOT EVER use it has caused quite a few newcomers to simply throw their hands up in despair and give up.

Steve

I know it's in some of the examples and I wish it wasn't as it teaches people new to programming that it's OK to use; it's not.

I Agree with Steve ! Not only that, but i wish to add that in certain situations delay() is the best option, though it may result in 'blocking' code that does not mean that doing more things at the same time is always better.
Though saying that for many examples there could & should be versions that make use of millis() rather than delay(), i think is true, and for this section

if (aux == 98){
    if (leitura == HIGH) {
    //Ligando o buzzer com uma frequencia de 1500 hz.
    tone(buzzer,1500);   
    delay(10);
    noTone(buzzer);
    delay(10);
    tone(buzzer,1800);
    delay(10);
    noTone(buzzer);
    // Ativamos o pino 12 (colocando 5v nele) 
    digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(100);                       // wait for a second
    digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
    delay(100);                       // wait for a second
    }

a millis() based solution would be better, so the led and the sirene can happen at the same time

starting with complex code you don't understand seems like a recipe for quickly becoming disillusioned.

The code is not hugely complex in my opinion, consists of 2 main parts, the button presses and the the resulting sound en light effect.

I do wish people wouldn't keep saying this.

Hi Steve,

I respectfully disagree. As I see it, what happens is new folk come here, probably try blink without delay, then start writing stuff with delay(); all over the place, then find it doesn't work very well and is unresponsive and just not very good. By this time they have invested a lot of time and effort into learning what they have learnt only to discover they have gone down a blind ally and they need to learn something different, something they could have learnt from the start. I agree there are places to use delay();, the one that comes to mind is sometimes it's useful in setup(); to give things time to power up or whatever. In that situation 500ms once is not going to be noticed.

I have considered saying 'don't use delay(); except in some very limited circumstances', but I am concerned that if I do that I'll be sending a confusing message. I'm afraid I'm going to continue to suggest people don't use delay, but I am also going to think about your comment and see if I can come up with a more nuanced way of saying it.

Thank you for the feedback. :slight_smile: ++Karma;

The code is not hugely complex in my opinion, consists of 2 main parts, the button presses and the the resulting sound en light effect.

daniloke said:

This project has copy paste a lot and I'm beginner with arduino programming.

I took that to mean the code was too complex for his current level of knowledge. If I am correct in that then it is complex for him. Obviously only daniloke knows if I am right about that any only daniloke knows if he finds my help helpful or unhelpful. Part of the problem of providing advice on a forum like this is you don't know for sure if it's the right advice for the person you are giving it to, you can't immediately judge their response and change what you say accordingly. All you can do is offer what you think is good advice and hope it helps.

Also to you thank you for the feedback :slight_smile: . ++Karma;

PerryBebbington:
By this time they have invested a lot of time and effort into learning what they have learnt only to discover they have gone down a blind ally and they need to learn something different, something they could have learnt from the start.

I think that's where we (respectfully) disagree. I see it as they have invested some time in learning a lot of useful things (not just delay()), probably had fun with simple projects and now found out that there is more they need to learn in order to do more advanced projects. That seems perfectly reasonable to me and I imagine it seems reasonable to a newish programmer, after they've had some success with straightforward projects, to find that they need to learn additional techniques to handle more complex requirements.

Steve

As I see it, what happens is new folk come here, probably try blink without delay, then start writing stuff with delay();

You know only just now did i find the blink without delay in the IDE examples .. hehe
But we all wrote 'with' delay() until we came to a point where we wanted something that couldn't really work well with delay() and then had to make the transition. In the mean 'I' learned quite a lot of other stuff.

something they could have learnt from the start

Well i think i wanted to first know about digitalRead & Write and i just wasn't ready for millis() based timing, and that goes for most novices.
All in All there should be more millis() based examples in the IDE, but somehow the way i would use millis() to create a fade is not the same as what most people would do.
Anyway back to the OP.
The section that i suspect you copied must be mainly about the buttons on the TFT-touch, that is non-blocking in nature as far as i can tell. Now here is an example of how to use millis(). It's a comple sketch you can use to test the hardware.

int ledState = LOW;
unsigned long previousMillis = 0;
const unsigned long interval = 1000;
const unsigned long sirenCycle = 2000;  // the time for the sirene to reach it's initial point.
const unsigned long howLongFor = 30000;  // 30sec
const int buzzer = A14;
const int led = A15;

void setup() {
  pinMode(buzzer,OUTPUT);
  pinMode(led, OUTPUT);
  unsigned long starting = millis();
  while (millis() - starting < howLongFor) {
    
    unsigned long currentMillis = millis();       // ths section is from Blink without delay example
    if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
      if (ledState == LOW)   ledState = HIGH;
      else ledState = LOW;
      digitalWrite(led, ledState);
    }
    
    unsigned int inCycle = currentMillis % sirenCycle;    // this is how i would use millis() & modulo
    if (inCycle > sirenCycle / 2) {                   // iow if it is on the way back
      inCycle = sirenCycle - inCycle;
    }
    unsigned int freq = map (inCycle, 0, sirenCycle / 2, 1500, 1800 ); // use the map function to 
    tone(buzzer, freq);
  }
}

void loop() {
  noTone(buzzer);
}

Dava and Steve,

Thank you both for your comments. I am trying to think about a better way to say 'don't use delay'. You both make good points.

You do know the moderators will have us all for hijacking the discussion :confused:
(Apologies to the moderators and to daniloke)

MorganS:
Every time you say "int" you are saying "please create a new variable of type int"...

The first line I quoted is inside a {} block. That means it is only visible inside that block.

The last line I quoted is referring to another variable which has the same name but different visibility.

Thanks for the response MorganS. Do you have any tip that I can use in this project do activate the buzzer and led with the button funcionality?

Thanks again!

Deva_Rishi:
the Above !!
and

tone(buzzer,1500);   

delay(10);
   noTone(buzzer);
   delay(10);
   tone(buzzer,1800);
   delay(10);
   noTone(buzzer);



these beeps are very short btw..



delay(100);                       // wait for a second



you are only waiting for 0.1 Seconds

I'm still working on that delays Deva_Rishi. I was make some tests with the delays to see how can I use them in the code.

I'm using the delays in this scenario to make a sound of a siren but I'm shure that exists some other way to do this hehe..

Thanks for the help!

Deva_Rishi:
You know only just now did i find the blink without delay in the IDE examples .. hehe
But we all wrote 'with' delay() until we came to a point where we wanted something that couldn't really work well with delay() and then had to make the transition. In the mean 'I' learned quite a lot of other stuff. Well i think i wanted to first know about digitalRead & Write and i just wasn't ready for millis() based timing, and that goes for most novices.
All in All there should be more millis() based examples in the IDE, but somehow the way i would use millis() to create a fade is not the same as what most people would do.
Anyway back to the OP.
The section that i suspect you copied must be mainly about the buttons on the TFT-touch, that is non-blocking in nature as far as i can tell. Now here is an example of how to use millis(). It's a comple sketch you can use to test the hardware.

int ledState = LOW;

unsigned long previousMillis = 0;
const unsigned long interval = 1000;
const unsigned long sirenCycle = 2000;  // the time for the sirene to reach it's initial point.
const unsigned long howLongFor = 30000;  // 30sec
const int buzzer = A14;
const int led = A15;

void setup() {
 pinMode(buzzer,OUTPUT);
 pinMode(led, OUTPUT);
 unsigned long starting = millis();
 while (millis() - starting < howLongFor) {
   
   unsigned long currentMillis = millis();       // ths section is from Blink without delay example
   if (currentMillis - previousMillis >= interval) {
     previousMillis = currentMillis;
     if (ledState == LOW)   ledState = HIGH;
     else ledState = LOW;
     digitalWrite(led, ledState);
   }
   
   unsigned int inCycle = currentMillis % sirenCycle;    // this is how i would use millis() & modulo
   if (inCycle > sirenCycle / 2) {                   // iow if it is on the way back
     inCycle = sirenCycle - inCycle;
   }
   unsigned int freq = map (inCycle, 0, sirenCycle / 2, 1500, 1800 ); // use the map function to
   tone(buzzer, freq);
 }
}

void loop() {
 noTone(buzzer);
}

Deva_Rishi, thanks for the help. I've tested on hardware and works fine. The most part of the code I could understand, I'm only had problems to understand the part of the buzzer (the symbology mess up with my reasoning hehe)

I'm having a hard time to make the button activate and deactivate the modules. Can you help me with that?

Thanks a bunch!

I'm only had problems to understand the part of the buzzer (the symbology mess up with my reasoning hehe)

Yeah sorry, but you get the blinking LED. the check to see if a certain amount of time has passed and then executed a toggle of the led status, the siren is in a way something similar, but since there are no individual steps, well there are or could be, 300 step 1 way and 300 steps back, rather then waiting for elapsed time to take the next step, we measure the whole cycle, and calculate at which step we should be. But Ok doesn't matter so much.

I'm having a hard time to make the button activate and deactivate the modules. Can you help me with that?

Yeah i figured as much, but it is as Morgan said, it is the 'int' that just shouldn't be there to begin with,
the sketch i posted earlier can be converted to a function which is called if the alarm is ringing. I took your code added it removed the other delay() thing (because you have started with a situation in which delay() does not work, if you want to press button and get a response, well during delay() that can only be done with interrupts and using millis() is definitely something you should start with before that)
I had to make it more readable for me, so i moved a few thing around a bit, put all the include's together, all the #defines etc.

Then the issues remaining , one i don't have the button class in my GFX, it should be there but it isn't so i can't verify if it compiles, there may be typos and other small errors, i had to modify some variable names into #defines, which i (and others) tend to write in Caps. so do the honors, compile.
and second, how do you want it to work ? i set it up now (leaving your aux variable as a state-machine counting down from 99) that when the alarm is on and the sensor is triggered it rings and the led flashes until it is turned off. You could set it up so it stops when either it is turned off or the sensor is no longer triggered, you could also let the LED blink if it is turned on (armed) but only the siren rings when the sensor is triggered (though for that the Alarmringing() function should be split into to separate ones) etc. anyway have a look.

#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <TouchScreen.h>

#define MINPRESSURE 200
#define MAXPRESSURE 1000

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

#define INTERVAL = 1000;
#define SIRENCYCLE = 2000; 

const int XP = 6, XM = A2, YP = A1, YM = 7; //ID=0x9341
const int TS_LEFT = 907, TS_RT = 136, TS_TOP = 942, TS_BOT = 139;

const int buzzer = A14;
const int PIR = A13;
const int led = A15;
const bool aux = 98;

MCUFRIEND_kbv tft;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 100);
Adafruit_GFX_Button on_btn, off_btn;

int pixel_x, pixel_y;     //Touch_getXY() updates global vars

void setup()  {
  Serial.begin(9600);
  uint16_t ID = tft.readID();
  Serial.print("TFT ID = 0x");
  Serial.println(ID, HEX);
  Serial.println("Calibrate for your Touch Panel");
  if (ID == 0xD3D3) ID = 0x9486; // write-only shield
  tft.begin(ID);
  tft.setRotation(0);            //PORTRAIT
  tft.fillScreen(BLACK);
  on_btn.initButton(&tft,  80, 330, 130, 40, WHITE, CYAN, BLACK, "ARMAR", 2);
  off_btn.initButton(&tft, 240, 330, 130, 40, WHITE, CYAN, BLACK, "DESARM.", 2);
  on_btn.drawButton(false);
  off_btn.drawButton(false);
  pinMode(PIR, INPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(led, OUTPUT);
}

void loop() {
  bool down = Touch_getXY();
  int leitura = digitalRead(PIR);
  on_btn.press(down && on_btn.contains(pixel_x, pixel_y));
  off_btn.press(down && off_btn.contains(pixel_x, pixel_y));
  if (on_btn.justReleased())          on_btn.drawButton();
  if (off_btn.justReleased())         off_btn.drawButton();
  
  if (on_btn.justPressed()) {
    on_btn.drawButton(true);
    tft.fillRect(60, 100, 180, 100, GREEN);
    delay (100);
    tft.fillRect(60, 100, 180, 100, BLACK);
    aux = 98;
  }
  if (off_btn.justPressed()) {
    off_btn.drawButton(true);
    tft.fillRect(60, 100, 180, 100, RED);
    delay (100);
    tft.fillRect(60, 100, 180, 100, BLACK);
    aux = 99;
    StopAlarm();
  }
  if ((aux == 98) && (leitura == HIGH)) aux = 97; 
  if  (aux == 97) AlarmRinging();
}

void StopAlarm() {
  noTone (buzzer);
  digitalWrite(led, LOW);
}

void AlarmRinging() {
  static int ledState = LOW;                // these variables are static & local now 
  static uint32_t previousMillis = millis();  // which means they hold their value and are not destroyed 
                                              // when the function ends (but not accessable outside the function)
 
  uint32_t g currentMillis = millis();       // ths section is from Blink without delay example
  if (currentMillis - previousMillis >= INTERVAL) {
    previousMillis = currentMillis;
    if (ledState == LOW)   ledState = HIGH;
    else ledState = LOW;
    digitalWrite(led, ledState);
  }

  uint16_t inCycle = currentMillis % SIRENCYCLE;    // this is how i would use millis() & modulo
  if (inCycle > SIRENCYCLE / 2) {                   // iow if it is on the way back
    inCycle = SIRENCYCLE - inCycle;
  }
  uint16_t freq = map (inCycle, 0, SIRENCYCLE / 2, 1500, 1800 ); // use the map function to
  tone(buzzer, freq);
}

bool Touch_getXY() {
  TSPoint p = ts.getPoint();
  pinMode(YP, OUTPUT);      //restore shared pins
  pinMode(XM, OUTPUT);
  digitalWrite(YP, HIGH);   //because TFT control pins
  digitalWrite(XM, HIGH);
  bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
  if (pressed) {
    pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
    pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
  }
  return pressed;
}

Deva_Rishi:
Yeah sorry, but you get the blinking LED. the check to see if a certain amount of time has passed and then executed a toggle of the led status, the siren is in a way something similar, but since there are no individual steps, well there are or could be, 300 step 1 way and 300 steps back, rather then waiting for elapsed time to take the next step, we measure the whole cycle, and calculate at which step we should be. But Ok doesn't matter so much.Yeah i figured as much, but it is as Morgan said, it is the 'int' that just shouldn't be there to begin with,
the sketch i posted earlier can be converted to a function which is called if the alarm is ringing. I took your code added it removed the other delay() thing (because you have started with a situation in which delay() does not work, if you want to press button and get a response, well during delay() that can only be done with interrupts and using millis() is definitely something you should start with before that)
I had to make it more readable for me, so i moved a few thing around a bit, put all the include's together, all the #defines etc.

Then the issues remaining , one i don't have the button class in my GFX, it should be there but it isn't so i can't verify if it compiles, there may be typos and other small errors, i had to modify some variable names into #defines, which i (and others) tend to write in Caps. so do the honors, compile.
and second, how do you want it to work ? i set it up now (leaving your aux variable as a state-machine counting down from 99) that when the alarm is on and the sensor is triggered it rings and the led flashes until it is turned off. You could set it up so it stops when either it is turned off or the sensor is no longer triggered, you could also let the LED blink if it is turned on (armed) but only the siren rings when the sensor is triggered (though for that the Alarmringing() function should be split into to separate ones) etc. anyway have a look.

Deva, thanks for the reply! I've compiled here and get some errors, look:

Arduino: 1.8.9 (Windows Store 1.8.21.0) (Windows 10), Placa:"Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Users\jakk_\Documents\Arduino\Test\Test.ino: In function 'void loop()':

Test:66:9: error: assignment of read-only variable 'aux'

     aux = 98;

         ^

Test:73:9: error: assignment of read-only variable 'aux'

     aux = 99;

         ^

Test:76:45: error: assignment of read-only variable 'aux'

   if ((aux == 98) && (leitura == HIGH)) aux = 97;

                                             ^

C:\Users\jakk_\Documents\Arduino\Test\Test.ino: In function 'void AlarmRinging()':

Test:90:14: error: expected initializer before 'currentMillis'

   uint32_t g currentMillis = millis();       // ths section is from Blink without delay example

              ^

Test:91:7: error: 'currentMillis' was not declared in this scope

   if (currentMillis - previousMillis >= INTERVAL) {

       ^

Test:17:18: error: expected primary-expression before '=' token

 #define INTERVAL = 1000;

                  ^

C:\Users\jakk_\Documents\Arduino\Test\Test.ino:91:41: note: in expansion of macro 'INTERVAL'

   if (currentMillis - previousMillis >= INTERVAL) {

                                         ^

Test:17:24: error: expected ')' before ';' token

 #define INTERVAL = 1000;

                        ^

C:\Users\jakk_\Documents\Arduino\Test\Test.ino:91:41: note: in expansion of macro 'INTERVAL'

   if (currentMillis - previousMillis >= INTERVAL) {

                                         ^

Test:91:49: error: expected primary-expression before ')' token

   if (currentMillis - previousMillis >= INTERVAL) {

                                                 ^

Test:98:22: error: 'currentMillis' was not declared in this scope

   uint16_t inCycle = currentMillis % SIRENCYCLE;    // this is how i would use millis() & modulo

                      ^

Test:18:20: error: expected primary-expression before '=' token

 #define SIRENCYCLE = 2000;

                    ^

C:\Users\jakk_\Documents\Arduino\Test\Test.ino:98:38: note: in expansion of macro 'SIRENCYCLE'

   uint16_t inCycle = currentMillis % SIRENCYCLE;    // this is how i would use millis() & modulo

                                      ^

Test:18:20: error: expected primary-expression before '=' token

 #define SIRENCYCLE = 2000;

                    ^

C:\Users\jakk_\Documents\Arduino\Test\Test.ino:99:17: note: in expansion of macro 'SIRENCYCLE'

   if (inCycle > SIRENCYCLE / 2) {                   // iow if it is on the way back

                 ^

Test:18:26: error: expected ')' before ';' token

 #define SIRENCYCLE = 2000;

                          ^

C:\Users\jakk_\Documents\Arduino\Test\Test.ino:99:17: note: in expansion of macro 'SIRENCYCLE'

   if (inCycle > SIRENCYCLE / 2) {                   // iow if it is on the way back

                 ^

Test:99:28: error: expected primary-expression before '/' token

   if (inCycle > SIRENCYCLE / 2) {                   // iow if it is on the way back

                            ^

Test:18:20: error: expected primary-expression before '=' token

 #define SIRENCYCLE = 2000;

                    ^

C:\Users\jakk_\Documents\Arduino\Test\Test.ino:102:36: note: in expansion of macro 'SIRENCYCLE'

   uint16_t freq = map (inCycle, 0, SIRENCYCLE / 2, 1500, 1800 ); // use the map function to

                                    ^

Test:102:47: error: expected primary-expression before '/' token

   uint16_t freq = map (inCycle, 0, SIRENCYCLE / 2, 1500, 1800 ); // use the map function to

                                               ^

Usando a biblioteca Adafruit_GFX na versão 1.0.2 na pasta: C:\Users\jakk_\Documents\Arduino\libraries\Adafruit_GFX 
Usando a biblioteca MCUFRIEND_kbv na versão 2.9.8 na pasta: C:\Users\jakk_\Documents\Arduino\libraries\MCUFRIEND_kbv 
Usando biblioteca TouchScreen na pasta: C:\Users\jakk_\Documents\Arduino\libraries\TouchScreen (legacy)
exit status 1
assignment of read-only variable 'aux'

I putted in here the libs used in the project if you want to.

About the code format, thanks for organize it. It's much better now. I confess that it was mess up a little hehe.

It should work like you said, when the alarm is armed by the button, should activate the sensor and when triggered, blink the leds and rings until the alarm button desarm has pressed.

Again, thanks a bunch for the help! :slight_smile:

Adafruit_GFX.zip (10.6 KB)

MCUFRIEND_kbv-master.zip (883 KB)

TouchScreen.zip (4.77 KB)

const int aux;

The keyword const means you promised not to change the value of aux. The error says you broke that promise.

You should probably remove "const".

yep all typos

#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <TouchScreen.h>

#define MINPRESSURE 200
#define MAXPRESSURE 1000

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

#define INTERVAL 1000;  // removing the '=' should take care of the last few errors
#define SIRENCYCLE 2000;

const int XP = 6, XM = A2, YP = A1, YM = 7; //ID=0x9341
const int TS_LEFT = 907, TS_RT = 136, TS_TOP = 942, TS_BOT = 139;

const int buzzer = A14;
const int PIR = A13;
const int led = A15;
int aux = 98;  // this should not be 'const' and in fact also not 'bool' but 'int' or 'uint8_t' or something 

MCUFRIEND_kbv tft;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 100);
Adafruit_GFX_Button on_btn, off_btn;

int pixel_x, pixel_y;     //Touch_getXY() updates global vars

void setup()  {
  Serial.begin(9600);
  uint16_t ID = tft.readID();
  Serial.print("TFT ID = 0x");
  Serial.println(ID, HEX);
  Serial.println("Calibrate for your Touch Panel");
  if (ID == 0xD3D3) ID = 0x9486; // write-only shield
  tft.begin(ID);
  tft.setRotation(0);            //PORTRAIT
  tft.fillScreen(BLACK);
  on_btn.initButton(&tft,  80, 330, 130, 40, WHITE, CYAN, BLACK, "ARMAR", 2);
  off_btn.initButton(&tft, 240, 330, 130, 40, WHITE, CYAN, BLACK, "DESARM.", 2);
  on_btn.drawButton(false);
  off_btn.drawButton(false);
  pinMode(PIR, INPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(led, OUTPUT);
}

void loop() {
  bool down = Touch_getXY();
  int leitura = digitalRead(PIR);
  on_btn.press(down && on_btn.contains(pixel_x, pixel_y));
  off_btn.press(down && off_btn.contains(pixel_x, pixel_y));
  if (on_btn.justReleased())          on_btn.drawButton();
  if (off_btn.justReleased())         off_btn.drawButton();
 
  if (on_btn.justPressed()) {
    on_btn.drawButton(true);
    tft.fillRect(60, 100, 180, 100, GREEN);
    delay (100);
    tft.fillRect(60, 100, 180, 100, BLACK);
    aux = 98;
  }
  if (off_btn.justPressed()) {
    off_btn.drawButton(true);
    tft.fillRect(60, 100, 180, 100, RED);
    delay (100);
    tft.fillRect(60, 100, 180, 100, BLACK);
    aux = 99;
    StopAlarm();
  }
  if ((aux == 98) && (leitura == HIGH)) aux = 97;
  if  (aux == 97) AlarmRinging();
}

void StopAlarm() {
  noTone (buzzer);
  digitalWrite(led, LOW);
}

void AlarmRinging() {
  static int ledState = LOW;                // these variables are static & local now
  static uint32_t previousMillis = millis();  // which means they hold their value and are not destroyed
                                              // when the function ends (but not accessable outside the function)
 
  uint32_t currentMillis = millis();       // ths section is from Blink without delay example  
                                                      // the 'g' should not have been there
  if (currentMillis - previousMillis >= INTERVAL) {
    previousMillis = currentMillis;
    if (ledState == LOW)   ledState = HIGH;
    else ledState = LOW;
    digitalWrite(led, ledState);
  }

  uint16_t inCycle = currentMillis % SIRENCYCLE;    // this is how i would use millis() & modulo
  if (inCycle > SIRENCYCLE / 2) {                   // iow if it is on the way back
    inCycle = SIRENCYCLE - inCycle;
  }
  uint16_t freq = map (inCycle, 0, SIRENCYCLE / 2, 1500, 1800 ); // use the map function to
  tone(buzzer, freq);
}

bool Touch_getXY() {
  TSPoint p = ts.getPoint();
  pinMode(YP, OUTPUT);      //restore shared pins
  pinMode(XM, OUTPUT);
  digitalWrite(YP, HIGH);   //because TFT control pins
  digitalWrite(XM, HIGH);
  bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
  if (pressed) {
    pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
    pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
  }
  return pressed;
}

removed the errors for you, though some you should have been able to find as well.
note : i changed 'unsigned long' to uint32_t which in fact is a different way of writing the same thing (but shorter), an unsigned 32 bit integer.

Deva_Rishi:
yep all typos

#include <Adafruit_GFX.h>

#include <MCUFRIEND_kbv.h>
#include <TouchScreen.h>

#define MINPRESSURE 200
#define MAXPRESSURE 1000

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

#define INTERVAL 1000;  // removing the '=' should take care of the last few errors
#define SIRENCYCLE 2000;

const int XP = 6, XM = A2, YP = A1, YM = 7; //ID=0x9341
const int TS_LEFT = 907, TS_RT = 136, TS_TOP = 942, TS_BOT = 139;

const int buzzer = A14;
const int PIR = A13;
const int led = A15;
int aux = 98;  // this should not be 'const' and in fact also not 'bool' but 'int' or 'uint8_t' or something

MCUFRIEND_kbv tft;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 100);
Adafruit_GFX_Button on_btn, off_btn;

int pixel_x, pixel_y;     //Touch_getXY() updates global vars

void setup()  {
 Serial.begin(9600);
 uint16_t ID = tft.readID();
 Serial.print("TFT ID = 0x");
 Serial.println(ID, HEX);
 Serial.println("Calibrate for your Touch Panel");
 if (ID == 0xD3D3) ID = 0x9486; // write-only shield
 tft.begin(ID);
 tft.setRotation(0);            //PORTRAIT
 tft.fillScreen(BLACK);
 on_btn.initButton(&tft,  80, 330, 130, 40, WHITE, CYAN, BLACK, "ARMAR", 2);
 off_btn.initButton(&tft, 240, 330, 130, 40, WHITE, CYAN, BLACK, "DESARM.", 2);
 on_btn.drawButton(false);
 off_btn.drawButton(false);
 pinMode(PIR, INPUT);
 pinMode(buzzer, OUTPUT);
 pinMode(led, OUTPUT);
}

void loop() {
 bool down = Touch_getXY();
 int leitura = digitalRead(PIR);
 on_btn.press(down && on_btn.contains(pixel_x, pixel_y));
 off_btn.press(down && off_btn.contains(pixel_x, pixel_y));
 if (on_btn.justReleased())          on_btn.drawButton();
 if (off_btn.justReleased())         off_btn.drawButton();

if (on_btn.justPressed()) {
   on_btn.drawButton(true);
   tft.fillRect(60, 100, 180, 100, GREEN);
   delay (100);
   tft.fillRect(60, 100, 180, 100, BLACK);
   aux = 98;
 }
 if (off_btn.justPressed()) {
   off_btn.drawButton(true);
   tft.fillRect(60, 100, 180, 100, RED);
   delay (100);
   tft.fillRect(60, 100, 180, 100, BLACK);
   aux = 99;
   StopAlarm();
 }
 if ((aux == 98) && (leitura == HIGH)) aux = 97;
 if  (aux == 97) AlarmRinging();
}

void StopAlarm() {
 noTone (buzzer);
 digitalWrite(led, LOW);
}

void AlarmRinging() {
 static int ledState = LOW;                // these variables are static & local now
 static uint32_t previousMillis = millis();  // which means they hold their value and are not destroyed
                                             // when the function ends (but not accessable outside the function)

uint32_t currentMillis = millis();       // ths section is from Blink without delay example  
                                                     // the 'g' should not have been there
 if (currentMillis - previousMillis >= INTERVAL) {
   previousMillis = currentMillis;
   if (ledState == LOW)   ledState = HIGH;
   else ledState = LOW;
   digitalWrite(led, ledState);
 }

uint16_t inCycle = currentMillis % SIRENCYCLE;    // this is how i would use millis() & modulo
 if (inCycle > SIRENCYCLE / 2) {                   // iow if it is on the way back
   inCycle = SIRENCYCLE - inCycle;
 }
 uint16_t freq = map (inCycle, 0, SIRENCYCLE / 2, 1500, 1800 ); // use the map function to
 tone(buzzer, freq);
}

bool Touch_getXY() {
 TSPoint p = ts.getPoint();
 pinMode(YP, OUTPUT);      //restore shared pins
 pinMode(XM, OUTPUT);
 digitalWrite(YP, HIGH);   //because TFT control pins
 digitalWrite(XM, HIGH);
 bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
 if (pressed) {
   pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
   pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
 }
 return pressed;
}



removed the errors for you, though some you should have been able to find as well.
note : i changed 'unsigned long' to uint32_t which in fact is a different way of writing the same thing (but shorter), an unsigned 32 bit integer.

Deva, thanks for the reply. I had some problems with internet and couldn't reply here. I fix some other issues that appears when I compiled the code but I've been able to make work exacly as you said it would. Thank you so much!

I've only change the defines INTERVAL and SIRENCYCLE to the values direcly on the code because with the defines been called on the code, an error is shown:

C:\Users\jakk_\Documents\Arduino\testeteste\testeteste.ino: In function 'void AlarmRinging()':

testeteste:17:22: error: expected ')' before ';' token

 #define INTERVAL 1000;  // removing the '=' should take care of the last few errors

                      ^

C:\Users\jakk_\Documents\Arduino\testeteste\testeteste.ino:92:41: note: in expansion of macro 'INTERVAL'

   if (currentMillis - previousMillis >= INTERVAL) {

                                         ^

testeteste:92:49: error: expected primary-expression before ')' token

   if (currentMillis - previousMillis >= INTERVAL) {

                                                 ^

testeteste:18:24: error: expected ')' before ';' token

 #define SIRENCYCLE 2000;

                        ^

C:\Users\jakk_\Documents\Arduino\testeteste\testeteste.ino:100:17: note: in expansion of macro 'SIRENCYCLE'

   if (inCycle > SIRENCYCLE / 2) {                   // iow if it is on the way back

                 ^

testeteste:100:28: error: expected primary-expression before '/' token

   if (inCycle > SIRENCYCLE / 2) {                   // iow if it is on the way back

                            ^

testeteste:18:24: error: expected ')' before ';' token

 #define SIRENCYCLE 2000;

                        ^

C:\Users\jakk_\Documents\Arduino\testeteste\testeteste.ino:103:36: note: in expansion of macro 'SIRENCYCLE'

   uint16_t freq = map (inCycle, 0, SIRENCYCLE / 2, 1500, 1800 ); // use the map function to

                                    ^

testeteste:103:47: error: expected primary-expression before '/' token

   uint16_t freq = map (inCycle, 0, SIRENCYCLE / 2, 1500, 1800 ); // use the map function to

                                               ^

Usando a biblioteca Adafruit_GFX na versão 1.0.2 na pasta: C:\Users\jakk_\Documents\Arduino\libraries\Adafruit_GFX 
Usando a biblioteca MCUFRIEND_kbv na versão 2.9.8 na pasta: C:\Users\jakk_\Documents\Arduino\libraries\MCUFRIEND_kbv 
Usando biblioteca TouchScreen na pasta: C:\Users\jakk_\Documents\Arduino\libraries\TouchScreen (legacy)
exit status 1
expected ')' before ';' token

I think that the code don't identifies the defines. Maybe I should change to int or other declaration?

Remove the semicolon