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.