// Including the required Arduino libraries
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <LiquidCrystal.h>
#include <stdio.h>
// Uncomment according to your hardware type
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 2
#define DATA_PIN 11 // or MOSI
#define CS_PIN 12 // or SS
#define CLK_PIN 13 // or SCK
// buttons pin initialization
const int b1 = 5;
const int b2 = 4;
const int b3 = 3;
const int b4 = 2;
// sensors pin initialization
int flamePin = A1;
int mq2Pin = A0;
// floor & flat setup pin initialization
int floorPin = A6;
int flatPin = A7;
// Trigger and Reset external pins initialization
int trigEXT = A2 ;
int rstEXT = A3;
// variables to storage sensors/ flat / floor data
int flameValue = 0;
int mq2Value = 0;
int floorValue = 0;
int flatValue = 0;
int flatNumber = 0;
int floorNumber = 0;
// buffer to storage the data to be printed in the LCD
char bufferLCD[16];
// control variable for the menus
int menu = 0;
// SPI hardware interface
MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(A4, A5, 9, 8, 7, 6);
void setup()
{
Serial.begin(9600);
// buttons setup, INPUT
pinMode(b1, INPUT);
pinMode(b2, INPUT);
pinMode(b3, INPUT);
pinMode(b4, INPUT);
// flame sensor setup
pinMode(flamePin, INPUT);
//floor and flat pins setup
pinMode(mq2Pin, INPUT);
pinMode(floorPin, INPUT);
pinMode(flatPin, INPUT);
//External Trigger and Rst setup
pinMode(trigEXT, OUTPUT);
pinMode(rstEXT, OUTPUT);
// Intialize the object
mx.begin();
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// turn-off all the Matrix LEDs
for(int y =0; y<16; y++)
{
for(int x =0; x<8; x++)
{
mx.setPoint(x,y,0);
}
}
}
void loop()
{
switch(menu)
{
case 0:
mainMenu();
break;
case 1:
//TASK1
task01();
break;
case 2:
//TASK2
task02();
break;
case 3:
//TASK3
task03();
break;
case 4:
//TASK4
task04();
break;
}
}
/**************DO NOT EDIT THIS FUNCTION *********************/
// Function to control the LEDs of the Matrix
// r = ROW , c = COLUMN, statusLED -> 0 = OFF; 1 = ON
void setMatrix(int r, int c, int statusLed)
{
/*
if(r<8)
{
r = 7 - r;
c+=8;
}
else
{
r = 15 - r;
}*/
c = 7-c;
mx.setPoint(c,r,statusLed);
}
/****************************************************************/
void mainMenu()
{
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("CENTRAL ALARM");
lcd.setCursor(1, 1);
lcd.print("TEST SYS MODE");
do
{
if(digitalRead(b1) == LOW)
{
menu = 1;
}
if(digitalRead(b2) == LOW)
{
menu = 2;
}
if(digitalRead(b3) == LOW)
{
menu = 3;
}
if(digitalRead(b4) == LOW)
{
menu = 4;
}
}while(menu==0);
}
void task01()
{
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("FLOOR & FLAT");
do
{
/* INITIALIZE THE ANALOG READING CODE IN THE SPACE BELOW */
// Return the analog values converted to the variables floorValue and flatValue
/********************* YOUR CODE HERE*********************/
floorValue = analogRead(floorPin);
flatValue = analogRead(flatPin);
if(floorValue < 100)
{
floorNumber = 0;
}
else if(floorValue >= 100 && floorValue <= 199){
floorNumber = 1;
}
else if(floorValue >= 200 && floorValue <= 299){
floorNumber = 2;
}
else if(floorValue >= 300 && floorValue <= 399){
floorNumber = 3;
}
else if(floorValue >= 400 && floorValue <= 499){
floorNumber = 4;
}
else if(floorValue >= 500 && floorValue <= 599){
floorNumber = 5;
}
else if(floorValue >= 600 && floorValue <= 699){
floorNumber = 6;
}
else if(floorValue >= 700 && floorValue <= 799){
floorNumber = 7;
}
else if(floorValue >= 800 && floorValue <= 899){
floorNumber = 8;
}
else{
floorNumber = 10;
}
flatNumber = flatValue / 100;
/********************************************************/
//sprintf(bufferLCD,"Fr:%0.4d Ft:%0.4d",floorValue,flatValue);
if((floorNumber < 9) && (flatNumber >= 1 && flatNumber <= 4)){
lcd.setCursor(0, 1);
lcd.print("Floor:");
lcd.setCursor(6, 1);
if(floorNumber == 0){
lcd.print("G");
}
else{
lcd.print(floorNumber);
}
lcd.print(" Flat:");
lcd.setCursor(13, 1);
if(floorNumber == 0){
lcd.print("---");
}
else{
lcd.print(floorNumber);
lcd.print("0");
lcd.print(flatNumber);
}
}
else{
lcd.setCursor(0, 1);
lcd.print(" ERRROR! ");
}
if(digitalRead(b4) == LOW)
{
menu = 0;
}
}while(menu==1);
}
void task02()
{
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("SENSORS TEST");
do
{
/* INITIALIZE THE SENSORS READING CODE IN THE SPACE BELOW */
// Return the sensors signal converted to the variables mq2Value and flameValue
/********************* YOUR CODE HERE*********************/
mq2Value = analogRead(mq2Pin);
flameValue = analogRead(flamePin);
//low value means smoke
//high value means no smoke
if(mq2Value < 100)
{
mq2Value = 0;
}
else if(mq2Value >= 100 && mq2Value <= 199){
mq2Value = 1;
}
else if(mq2Value >= 200 && mq2Value <= 299){
mq2Value = 2;
}
else if(mq2Value >= 300 && mq2Value <= 399){
mq2Value = 3;
}
else if(mq2Value >= 400 && mq2Value <= 499){
mq2Value = 4;
}
else if(mq2Value >= 500 && mq2Value <= 599){
mq2Value = 5;
}
else if(mq2Value >= 600 && mq2Value <= 699){
mq2Value = 6;
}
else if(mq2Value >= 700 && mq2Value <= 799){
mq2Value = 7;
}
else if(mq2Value >= 800 && mq2Value <= 899){
mq2Value = 8;
}
else{
mq2Value = 10;
}
//flatNumber = flatValue / 100;
/********************************************************/
sprintf(bufferLCD,"MQ2:%0.4d FLAME:%d",mq2Value,flameValue);
lcd.setCursor(0, 1);
lcd.print(bufferLCD);
}while(menu==2);
}
void task03()
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print("TRIG & RST EXT");
lcd.setCursor(0,1);
lcd.print("B1:TRIG B2:RST");
do
{
/********************* YOUR CODE HERE*********************/
/********************************************************/
}while(menu==3);
}
void task04()
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print("MATRIX DISPLAY");
lcd.setCursor(1,1);
lcd.print("TESTING MODE");
/********************* YOUR CODE BELOW*********************/
for(int r = 0; r < 16; r++)
{
for(int c = 0; c < 8; c++)
{
setMatrix(r,c,1);
delay(100);
}
}
do
{
if(digitalRead(b4) == LOW)
{
menu = 0;
delay(500);
}
}while(menu==4);
}