ECR MONITOR FOR CATERPILLAR POWERPACK

Good day my dear friends! Today I would like to share with you my last project – CAN J1939 monitor for ship’s power pack based on Caterpillar 3512B engine installed in 40 ft container. Monitor installed in engine control room, wire length to powerpack 60 meters. I also created and connect second device, which allows to make remote independent monitoring of fuel oil level in F.O. tank, which installed in the same container near CAT engine.

Unfraternally I don’t have time for explanation of all creation process. If you have questions or you don’t understand some parts of source code, you can ask me by e-mail.

Plan of works, video, photos, and source code below.

STAGES OF WORK:

  1. Determination of the possibility of connecting to the bus for transmitting the operating parameters of D / G sensors (RS-232, RS-485, CAN, Other).
  2. Ordering missing equipment.
  3. Installation of the necessary equipment, connection to the data transmission bus.
  4. Establishing system codes for software parsing of the required parameters.
  5. Writing a program for the monitoring and control module.
  6. Connection of the finished device (F/D) “on site”, testing.
  7. Laying the signal cable in the Engine Control Room.
  8. Connecting F/D in the Engine Control Room.
  9. Checking the operation of the F/D “at a distance”, elimination of possible problems and electrical interference.
  10. Installation of the display into the central control panel.
  11. Final check.
  12. Delivery of equipment.

List of required equipment:
Display TFT 3.5’ 480×320 px.
Control boards on ATmega2560 and ATmega328P microcontrollers (Arduino)
Interface converter CAN/SPI
Shielded cable
Power adapter for control board 12V / 0.5A – 1 pc.

There may be errors and flaws in the source code. Do not judge strictly! Work continues.

SHORT VIDEO

CATERPILLAR MONITORING DEVICE CODE

//////////////////////////////////////////////////////////////
///////////////  Caterpillar Can-Bus Monitor   ///////////////
///////////////         Protocol: J1939        ///////////////
///////////////  Engine Model: GEN-SET 3512B   ///////////////
///////////////    Oleksandr Cheban-Lianka     ///////////////
///////////////      www.chebanlianka.com      ///////////////
///////////////   oleksandr@chebanlianka.com   ///////////////
//////////////////////////////////////////////////////////////

#include <SPI.h>
#include <MCUFRIEND_kbv.h>
#include "Adafruit_GFX.h"
#include <mcp2515.h>
#include <can.h>
#include <lib.h>
#define LCD_RD A0
#define LCD_WR A1
#define LCD_CD A2
#define LCD_CS A3
#define LCD_RESET A4
#define BME_CS 10
#define BME_MOSI 11
#define BME_MISO 12
#define BME_SCK 13

// Назначаем цвета

#define BLACK 0x0000
#define NAVY 0x000F
#define DARKGREEN 0x03E0
#define DARKCYAN 0x03EF
#define MAROON 0x7800
#define PURPLE 0x780F
#define OLIVE 0x7BE0
#define LIGHTGREY 0xC618
#define DARKGREY 0x7BEF
#define GREEN 0x07E0
#define CYAN 0x07FF
#define RED 0xF800
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define ORANGE 0xFD20
#define GREENYELLOW 0xAFE5
#define PINK 0xF81F
#define BGRD 0x686400
//#define BLUE 0x001F
#define BLUE 0x0000FF
#define DBLUE 0x006F
#define GREY 0xC618
#define DGREY 0x7BEF

// Устанавливаем модули

MCUFRIEND_kbv tft;
MCP2515 mcp2515(10);

// Устанавливаем глобальные переменные

boolean buzzerEnabled = false; // Для Бузера
boolean resetButton = true;

boolean singleNodataSwitch = true;

boolean commonInAlarm = false;
boolean commonInNormal = false;
boolean commonStatus = true;

boolean leakageInAlarm = false;
boolean leakageInNormal = false;
boolean leakageStatus = true;

boolean ventInAlarm = false;
boolean ventInNormal = false;
boolean ventStatus = true;

boolean folowInAlarm = false;
boolean folowInNormal = false;
boolean folowStatus = true;

boolean fohighInAlarm = false;
boolean fohighInNormal = false;
boolean fohighStatus = true;

int canid;
int data[8];

uint32_t pgnID = 0;
uint32_t nodataDelayTimer;
//uint32_t delayTimer500;
//uint32_t delayTimer1000;

const int SPI_CS_PIN = 10;
const int INT_PIN = 2;

struct can_frame canMsg;

static const uint8_t logoblack[] PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x01, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xf0, 0x00, 0x01, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf0, 0x00, 0x03, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xf8, 0x00, 0x03, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xfc, 0x00, 0x07, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xfc, 0x00, 0x07, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xfe, 0x00, 0x0f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0x00, 0x1f, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0x80, 0x1f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0x80, 0x3f, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xc0, 0x3f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xe0, 0x7f, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x01, 0x80, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x03, 0xe0, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x0f, 0xe0, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x3f, 0xf8, 0x00, 0x00,
  0x00, 0x00, 0x10, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xf8, 0x00, 0x00,
  0x00, 0x00, 0x3c, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x83, 0xff, 0xfc, 0x00, 0x00,
  0x00, 0x00, 0x7f, 0x80, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0xff, 0xff, 0x00, 0x00,
  0x00, 0x00, 0xff, 0xe0, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
  0x00, 0x01, 0xff, 0xf8, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00,
  0x00, 0x01, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00,
  0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00,
  0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00,
  0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xd0, 0x2b, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00,
  0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00,
  0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
  0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
  0x00, 0x1f, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xfe, 0x00, 0x00,
  0x00, 0x0f, 0xff, 0xff, 0xff, 0xf8, 0x40, 0x01, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xfc, 0x00, 0x00,
  0x00, 0x07, 0xff, 0xff, 0xff, 0xe0, 0xf1, 0xe3, 0xc7, 0x87, 0x87, 0xff, 0xff, 0xfc, 0x00, 0x00,
  0x00, 0x03, 0xff, 0xff, 0xff, 0xc0, 0xe1, 0xe3, 0xc3, 0xc7, 0x83, 0xff, 0xff, 0xf8, 0x00, 0x00,
  0x00, 0x03, 0xff, 0xff, 0xff, 0x81, 0xf1, 0xe3, 0xc7, 0x87, 0x81, 0xff, 0xff, 0xf8, 0x00, 0x00,
  0x00, 0x01, 0xff, 0xff, 0xfe, 0x00, 0xe1, 0xe3, 0xc3, 0x87, 0x80, 0x7f, 0xff, 0xf0, 0x00, 0x00,
  0x00, 0x00, 0xff, 0xff, 0xfc, 0x00, 0xf1, 0xe3, 0xc7, 0x87, 0x80, 0x3f, 0xff, 0xe0, 0x00, 0x00,
  0x00, 0x00, 0x7f, 0xff, 0xf8, 0x00, 0xe1, 0xe3, 0xc3, 0x87, 0x80, 0x1f, 0xff, 0xf0, 0x00, 0x00,
  0x00, 0x00, 0x7f, 0xff, 0xf0, 0x01, 0xe1, 0xc3, 0x87, 0xc7, 0x80, 0x0f, 0xff, 0xf0, 0x00, 0x00,
  0x00, 0x00, 0x3f, 0xff, 0xf0, 0x00, 0xa0, 0xa0, 0x82, 0x81, 0x00, 0x0f, 0xff, 0xf8, 0x00, 0x00,
  0x00, 0x00, 0x1f, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xf8, 0x00, 0x00,
  0x00, 0x00, 0x3f, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xfc, 0x00, 0x00,
  0x00, 0x00, 0x3f, 0xff, 0x80, 0x0a, 0xad, 0xaa, 0xaa, 0xaa, 0xa0, 0x01, 0xff, 0xff, 0xd4, 0x00,
  0x00, 0x00, 0x3f, 0xff, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x01, 0xff, 0xff, 0xff, 0xf8,
  0x00, 0x00, 0x7f, 0xff, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xf8,
  0x00, 0x00, 0x7f, 0xfe, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x7f, 0xff, 0xff, 0xfc,
  0x00, 0x00, 0x7f, 0xfe, 0x00, 0x1d, 0x24, 0x49, 0x29, 0x24, 0xf8, 0x10, 0x7f, 0xff, 0xff, 0xfc,
  0x00, 0x01, 0xff, 0xfc, 0xff, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x78, 0xff, 0x3f, 0xff, 0xff, 0xfc,
  0x00, 0x1f, 0xff, 0xfc, 0xff, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x78, 0xfe, 0x3f, 0xff, 0xff, 0xfc,
  0x05, 0xff, 0xff, 0xf8, 0xff, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x79, 0xff, 0x1f, 0xff, 0xff, 0xfe,
  0x3f, 0xff, 0xff, 0xf8, 0xfd, 0x3c, 0x00, 0x00, 0x00, 0x40, 0x78, 0x54, 0x1f, 0xff, 0xff, 0xfe,
  0x7f, 0xff, 0xff, 0xf8, 0x00, 0x1e, 0x00, 0x00, 0x01, 0xf0, 0x78, 0x00, 0x0f, 0xff, 0xff, 0xfe,
  0x7f, 0xff, 0xff, 0xf0, 0x00, 0x3c, 0x00, 0x00, 0x03, 0xf0, 0x78, 0x00, 0x0f, 0xff, 0xff, 0xfe,
  0x7f, 0xff, 0xff, 0xf0, 0x00, 0x1c, 0x00, 0x00, 0x03, 0xf8, 0x78, 0x00, 0x0f, 0xff, 0xff, 0xfe,
  0x7f, 0xff, 0xff, 0xf0, 0xaa, 0x3c, 0x00, 0x00, 0x07, 0xf8, 0x78, 0xb6, 0x0f, 0xff, 0xff, 0xff,
  0x7f, 0xff, 0xff, 0xe0, 0xff, 0x3c, 0x00, 0x00, 0x03, 0xf8, 0x78, 0xff, 0x07, 0xff, 0xff, 0xff,
  0x7f, 0xff, 0xff, 0xe0, 0xff, 0x1c, 0x00, 0x00, 0x01, 0xf0, 0x78, 0xfe, 0x07, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xe0, 0xff, 0x3e, 0x00, 0x00, 0x00, 0xc0, 0x78, 0xff, 0x07, 0xff, 0xff, 0xfe,
  0xff, 0xff, 0xff, 0xe0, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, 0x07, 0xff, 0xff, 0xf8,
  0xff, 0xff, 0xff, 0xe0, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, 0x07, 0xff, 0xff, 0xe0,
  0xff, 0xff, 0xff, 0xc0, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, 0x07, 0xff, 0xff, 0x80,
  0xff, 0xff, 0xff, 0xe0, 0xb5, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x78, 0x48, 0x03, 0xff, 0xfc, 0x00,
  0xff, 0xff, 0xff, 0xc0, 0xff, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x78, 0xff, 0x07, 0xff, 0xf4, 0x00,
  0xff, 0xff, 0xff, 0xe0, 0xff, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x78, 0xfe, 0x03, 0xff, 0xe0, 0x00,
  0x1f, 0xff, 0xff, 0xc0, 0xfe, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x70, 0xff, 0x07, 0xff, 0xe0, 0x00,
  0x07, 0xff, 0xff, 0xe0, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x78, 0xaa, 0x03, 0xff, 0xe0, 0x00,
  0x00, 0xff, 0xff, 0xe0, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, 0x07, 0xff, 0xc0, 0x00,
  0x00, 0x1f, 0xff, 0xc0, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, 0x03, 0xff, 0xe0, 0x00,
  0x00, 0x07, 0xff, 0xe0, 0x24, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, 0x07, 0xff, 0xc0, 0x00,
  0x00, 0x03, 0xff, 0xe0, 0xff, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x78, 0xff, 0x07, 0xff, 0xe0, 0x00,
  0x00, 0x03, 0xff, 0xe0, 0xff, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x78, 0xfe, 0x07, 0xff, 0xe0, 0x00,
  0x00, 0x03, 0xff, 0xe0, 0xff, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x78, 0xff, 0x07, 0xff, 0xf0, 0x00,
  0x00, 0x01, 0xff, 0xf0, 0xaa, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x78, 0xfa, 0x07, 0xff, 0xfc, 0x00,
  0x00, 0x03, 0xff, 0xf0, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x68, 0x00, 0x0f, 0xff, 0xfe, 0x00,
  0x00, 0x01, 0xff, 0xf0, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, 0x0f, 0xff, 0xff, 0x80,
  0x00, 0x01, 0xff, 0xf0, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, 0x0f, 0xff, 0xff, 0x80,
  0x00, 0x01, 0xff, 0xf8, 0xaa, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x78, 0xf6, 0x1f, 0xff, 0xff, 0xe0,
  0x00, 0x01, 0xff, 0xf8, 0xff, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x78, 0xff, 0x1f, 0xff, 0xff, 0xf8,
  0x00, 0x03, 0xff, 0xf8, 0xff, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x78, 0xfe, 0x1f, 0xff, 0xff, 0xfc,
  0x00, 0x07, 0xff, 0xfc, 0xff, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x78, 0xfe, 0x3f, 0xff, 0xff, 0xfc,
  0x00, 0x1f, 0xff, 0xfe, 0x00, 0x1c, 0x40, 0x00, 0x00, 0x00, 0x78, 0x00, 0x3f, 0xff, 0xff, 0xfc,
  0x00, 0x1f, 0xff, 0xfe, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x7f, 0xff, 0xff, 0xfc,
  0x00, 0x7f, 0xff, 0xfe, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x7f, 0xff, 0xff, 0xf8,
  0x00, 0xff, 0xff, 0xff, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0xff, 0xff, 0xff, 0xf8,
  0x01, 0xff, 0xff, 0xff, 0x80, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xa0, 0x01, 0xff, 0xff, 0xff, 0xf8,
  0x03, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xf0,
  0x07, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xf0,
  0x07, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xe0,
  0x07, 0xff, 0xff, 0xff, 0xf0, 0x00, 0xe1, 0xe1, 0xc3, 0x87, 0x00, 0x07, 0xff, 0xff, 0xff, 0xe0,
  0x03, 0xff, 0xff, 0xff, 0xf8, 0x01, 0xe1, 0xc3, 0xc3, 0x87, 0x80, 0x1f, 0xff, 0xff, 0xff, 0xe0,
  0x03, 0xff, 0xff, 0xff, 0xfc, 0x00, 0xe1, 0xe1, 0xc7, 0x87, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xc0,
  0x01, 0xff, 0xff, 0xff, 0xfc, 0x01, 0xe1, 0xe3, 0xc3, 0x87, 0x80, 0x7f, 0xff, 0xe0, 0x0b, 0xc0,
  0x01, 0xff, 0xff, 0xff, 0xff, 0x01, 0xe1, 0xc1, 0xc7, 0x87, 0x80, 0xff, 0xff, 0xc0, 0x00, 0x00,
  0x00, 0xff, 0xff, 0xff, 0xff, 0x80, 0xe1, 0xe3, 0xc3, 0xc7, 0x01, 0xff, 0xff, 0x80, 0x00, 0x00,
  0x00, 0xff, 0xff, 0xff, 0xff, 0xe1, 0xe1, 0xe1, 0xc7, 0x87, 0x83, 0xff, 0xff, 0x00, 0x00, 0x00,
  0x00, 0x7f, 0xff, 0xff, 0xff, 0xf0, 0xe0, 0xa3, 0xc3, 0x87, 0x0f, 0xff, 0xff, 0x00, 0x00, 0x00,
  0x00, 0x7f, 0xf0, 0x3f, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xfe, 0x00, 0x00, 0x00,
  0x00, 0x3e, 0x80, 0x1f, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xfe, 0x00, 0x00, 0x00,
  0x00, 0x10, 0x00, 0x1f, 0xff, 0xff, 0x80, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xfa, 0xaf, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe1, 0xff, 0xff, 0xf0, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x7f, 0xff, 0xe0, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xfe, 0x00, 0x7f, 0xff, 0x80, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x0f, 0xff, 0xfe, 0x01, 0xff, 0xff, 0xfc, 0x00, 0x1f, 0xff, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x0f, 0xff, 0xfc, 0x00, 0x3f, 0xff, 0xfc, 0x00, 0x0f, 0xfe, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x1f, 0xff, 0xf8, 0x00, 0x3f, 0xff, 0xf8, 0x00, 0x03, 0xf8, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x0f, 0xff, 0xf0, 0x00, 0x3f, 0xff, 0xfc, 0x00, 0x01, 0xf0, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x03, 0xff, 0xe0, 0x00, 0x1f, 0xff, 0xf8, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x01, 0xff, 0xc0, 0x00, 0x1f, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x7f, 0x80, 0x00, 0x1f, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x0f, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x0f, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xfa, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

////////////////////////////////////////////////////////////////////////////
////////////////////////////PROGRAM SETUP///////////////////////////////////
////////////////////////////////////////////////////////////////////////////

void setup() {

  ////////////////////////////////////////////////////////////////////////////
  ////////////////////////ALARM SYSTEM SETUP BEGIN////////////////////////////
  ////////////////////////////////////////////////////////////////////////////

  pinMode(34, OUTPUT); // Buzzer Output
  pinMode(22, INPUT_PULLUP); // Buzzer OFF Button
  pinMode(24, INPUT_PULLUP); // Common Alarm Input
  pinMode(26, INPUT_PULLUP); // Leakage Alarm Input
  pinMode(28, INPUT_PULLUP); // Vent Alarm Input
  pinMode(30, INPUT_PULLUP); // F.O. Level Low Input
  pinMode(32, INPUT_PULLUP); // F.O. Level High Input

  ////////////////////////////////////////////////////////////////////////////
  ////////////////////////ALARM SYSTEM SETUP END//////////////////////////////
  ////////////////////////////////////////////////////////////////////////////

  Serial.begin(115200);
  while (!Serial);
  SPI.begin();

  // Устанавливаем настройки CAN модуля
  mcp2515.reset();
  mcp2515.setBitrate(CAN_250KBPS, MCP_16MHZ);

  mcp2515.setConfigMode();

  mcp2515.setFilterMask(MCP2515::MASK0, true, 0x00FF0000); // Если после нули, принимает данные от всего, кроме того что ниже! Если FF, принимает только указанные ниже ID.
  mcp2515.setFilter(MCP2515::RXF0, true, 0x00FE0000);
  mcp2515.setFilter(MCP2515::RXF1, true, 0x00FE0000);

  mcp2515.setFilterMask(MCP2515::MASK1, true, 0x00FFFF00); // Если после нули, принимает данные от всего, кроме того что ниже! Если FF, принимает только указанные ниже ID.
  mcp2515.setFilter(MCP2515::RXF2, true, 0x00F00300); // Load At Current Speed
  mcp2515.setFilter(MCP2515::RXF3, true, 0x00F00400); // L.O. Pressure, F.O. Pressure
  mcp2515.setFilter(MCP2515::RXF4, true, 0x00FE0000);
  mcp2515.setFilter(MCP2515::RXF5, true, 0x00FE0000);

  mcp2515.setNormalMode();

  // Получаем и печатаем в монитор порта информацию о дисплее
  Serial.println();
  uint16_t ID = tft.readID();
  Serial.print("TFT DISPLAY ID = 0x");
  Serial.println(ID, HEX);
  Serial.print("DISPLAY Height = ");
  Serial.println(tft.height());
  Serial.print("DISPLAY Width = ");
  Serial.println(tft.width());

  // Печатаем заголовок CAN-шины
  Serial.println("------CAN-BUS Read OK!------");
  Serial.println("Can ID ----- DLC  ----- DATA");

  ////////////////////////////////////////////////////////////////////////////
  //////////////////////////////Hello Message/////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////

  tft.begin(ID);
  tft.setRotation(1); //Поворачиваем экран 0 = 0°, 1 = 90°, 2 = 180°, 3 = 270°
  tft.fillScreen(BLACK);

  tft.drawBitmap(180, 10, logoblack, 128, 128, 0xFFFF);

  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.setCursor(60, 155);
  tft.print("www.chebanlianka.com");

  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.setCursor(53, 190);
  tft.print("///////////////////////////////////////////////////////////////");
  tft.setCursor(53, 200);
  tft.print("///////////////   Caterpillar Can-Bus Monitor   ///////////////");
  tft.setCursor(53, 210);
  tft.print("///////////////          Protocol: J1939        ///////////////");
  tft.setCursor(53, 220);
  tft.print("///////////////  Engine Model: GEN-SET 3512B    ///////////////");
  tft.setCursor(53, 230);
  tft.print("/////////////// Author: Oleksandr Cheban-Lianka ///////////////");
  tft.setCursor(53, 240);
  tft.print("///////////////    oleksandr@chebanlianka.com   ///////////////");
  tft.setCursor(53, 250);
  tft.print("///////////////////////////////////////////////////////////////");

  tft.setCursor(160, 275);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("loading...");

  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.setCursor(350, 311);
  ////////////////////////////////////////////////////////////////////////////
  tft.print("fw version 2021.09.27");////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////
  delay (10000);

  ////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////Start Screen/////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////

  tft.begin(ID);
  tft.setRotation(1); //Поворачиваем экран 0 = 0°, 1 = 90°, 2 = 180°, 3 = 270°
  tft.setTextSize(2);
  tft.setTextColor(WHITE, BLACK);
  tft.invertDisplay(false);
  tft.setCursor(0, 0);
  tft.fillScreen(BGRD);

}

////////////////////////////////////////////////////////////////////////////
//////////////////////////////START LOOP////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

void loop() {

  if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK)
  {
    nodataDelayTimer = millis();   //Сбрасываем таймер при наличии сигнала
    Serial.print(canMsg.can_id, HEX); // Печатаем в порт CAN ID
    Serial.print(" ");
    unsigned long pngID = canMsg.can_id;

    // Reduce ID To 4 digits (HEX xxxx)

    pngID = pngID << 8;
    pngID = pngID >> 16;
    pgnID = pngID;
    Serial.print(pngID, HEX);
    Serial.print(" ");
    int dataLen = canMsg.can_dlc;

    // print DLC

    Serial.print(dataLen, HEX);
    Serial.print(" ");

    for (int i = 0; i < canMsg.can_dlc; i++)
    {

      // print the data

      data[i] = canMsg.data[i];
      Serial.print(data[i], HEX);
      Serial.print(" ");
    }
    Serial.println();
  }

  ////////////////////////////////////////////////////////////////////////////
  ///////////////////////SWITCH TO NODATA SCREEN//////////////////////////////
  ////////////////////////////////////////////////////////////////////////////
 
    if (millis() - nodataDelayTimer >= 5000) {  //Если счётчик таймера больше пяти секунд, переключаем на nodataScreen
      nodataScreen();
      singleNodataSwitch = true;       //Переключтель одиночной итерации
      commonStatus = true;             //Переключаем состояние алармов чтобы после появления сигнала заново их опросить (нужно, если аларм в сработке а CAN-сигнала нет)
      leakageStatus = true;            //Переключаем состояние алармов чтобы после появления сигнала заново их опросить (нужно, если аларм в сработке а CAN-сигнала нет)
      ventStatus = true;               //Переключаем состояние алармов чтобы после появления сигнала заново их опросить (нужно, если аларм в сработке а CAN-сигнала нет)
      folowStatus = true;              //Переключаем состояние алармов чтобы после появления сигнала заново их опросить (нужно, если аларм в сработке а CAN-сигнала нет)
      fohighStatus = true;             //Переключаем состояние алармов чтобы после появления сигнала заново их опросить (нужно, если аларм в сработке а CAN-сигнала нет)
    }
 
  if (millis() - nodataDelayTimer < 5000 && singleNodataSwitch == true) {   //Если счётчик таймера меньше пяти секунд, переключаем на dataScreen
    dataScreen();
    singleNodataSwitch = false;      //Переключаем "одиночный цикл" при наличии сигнала
      }

if (millis() - nodataDelayTimer < 5000) {  //Проверяем состояние алармов
    alarmLoop();
      }

//void alarmLoop() {

  ////////////////////////////////////////////////////////////////////////////
  //////////////////////////MAIN PROGRAM CODE/////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////

  //////////////////////////////////
  ////////Temp Sensor Code//////////
  //////////////////////////////////

  if (pgnID == 0xfeee) {
    int coolt = data[0];
    float cooltemp = ((-40) + coolt);
    tft.setCursor(20, 33);
    tft.setTextColor(WHITE, DBLUE);
    tft.setTextSize(4);

    if (cooltemp > 100) cooltemp = 0;
    if (cooltemp < -10) {
      tft.print("");
      tft.print(String(cooltemp, 0));
    }
    else if (cooltemp < 0) {
      tft.print(" ");
      tft.print(String(cooltemp, 0));
    }
    else if (cooltemp < 10) {
      tft.print(" ");
      tft.print(String(cooltemp, 0));
    }
    else if (cooltemp > 10) {
      tft.print(" ");
      tft.print(String(cooltemp, 0));
    }
    else {
      tft.print("");
      tft.print(String(cooltemp, 0));
    }
  }

  //////////////////////////////////
  ////////Engine Speed Code/////////
  //////////////////////////////////

  if (pgnID == 0xf004) {

    //millis() - delayTimer500 >=500 &&
    //delayTimer500 = millis();   //Сбрасываем таймер

    unsigned int r1 = data[3];
    unsigned int r2 = data[4];
    float rpm = ((r1) + (r2 * 256)) * 0.125;
    tft.setTextColor(WHITE, DBLUE);
    tft.setTextSize(4);
    tft.setCursor(160, 33);

    if (rpm > 10000) rpm = 0;
    if (rpm > 6000) rpm = 0;
    
    if (rpm < 10) {
      tft.print(" ");
      tft.print(String(rpm, 0));
      tft.print(" ");
    }
    else if (rpm < 100) {
      tft.print("  ");
      tft.print(String(rpm, 0));
    }
    else if (rpm < 1000) {
      tft.print(" ");
      tft.print(String(rpm, 0));
    }
    else {
      tft.print("");
      tft.print(String(rpm, 0));
    }
  }

  //////////////////////////////////
  /////Fuel, Oil Pressure Code//////
  //////////////////////////////////

  if (pgnID == 0xfeef) {

    unsigned int foilp = data[0];
    unsigned int loilp = data[3];
    float loilpressure = (loilp * 4);
    float foilpressure = (foilp * 4);
    tft.setTextColor(WHITE, DBLUE);
    tft.setTextSize(4);
    tft.setCursor(365, 33);

    if (loilpressure > 999) loilpressure = 0;
    if (loilpressure < 10) {
      tft.print("");
      tft.print(String(loilpressure, 0));
      tft.print(" ");
    }
    else if (loilpressure < 100) {
      tft.print("");
      tft.print(String(loilpressure, 0));
    }
    else {
      tft.print("");
      tft.print(String(loilpressure, 0));
    }

    tft.setCursor(365, 112);

    if (foilpressure > 999) foilpressure = 0;
    if (foilpressure < 10) {
      tft.print("");
      tft.print(String(foilpressure, 0));
      tft.print(" ");
    }
    else if (foilpressure < 100) {
      tft.print("");
      tft.print(String(foilpressure, 0));
    }
    else {
      tft.print("");
      tft.print(String(foilpressure, 0));
    }
  }

  //////////////////////////////////
  //////Boost Pressure Code/////////
  //////////////////////////////////

  if (pgnID == 0xfef6) {
    unsigned int bstp = data[1];
    float boostpressure = (bstp * 2);
    tft.setTextColor(WHITE, DBLUE);
    tft.setCursor(15, 112);
    tft.setTextSize(4);

    if (boostpressure > 999) boostpressure = 0;
    if (boostpressure < 10) {
      tft.print(" ");
      tft.print(String(boostpressure, 0));
      tft.print(" ");
    }
    else if (boostpressure < 100) {
      tft.print("  ");
      tft.print(String(boostpressure, 0));
    }
    else {
      tft.print(" ");
      tft.print(String(boostpressure, 0));
    }
  }

  //////////////////////////////////
  ////// Engine Power Code /////////
  //////////////////////////////////

  if (pgnID == 0xf003) {

    //millis() - delayTimer1000 >=1000 &&
    //delayTimer1000 = millis();   //Сбрасываем таймер
    unsigned int p1 = data[2];
    float rpower = ((p1 * 0.01)*1125);
    tft.setTextColor(WHITE, DBLUE);
    tft.setTextSize(4);
    tft.setCursor(160, 112);
    if (rpower > 9999) rpower = 0;
    if (rpower < 10) {
      tft.print(" ");
      tft.print(String(rpower, 0));
      tft.print("  ");
    }
    else if (rpower < 100) {
      tft.print("  ");
      tft.print(String(rpower, 0));
      tft.print(" ");
    }
    else if (rpower < 1000) {
      tft.print(" ");
      tft.print(String(rpower, 0));
      tft.print(" ");
    }
    else {
      tft.print(" ");
      tft.print(String(rpower, 0));
    }
  }

  //////////////////////////////////
  //////////Battery Code////////////
  //////////////////////////////////

  if (pgnID == 0xfef7) {
    unsigned int b1 = data[4];
    unsigned int b2 = data[5];
    float battery = ((b1) + (b2 * 256)) * 0.05;
    tft.setTextColor(WHITE, DBLUE);
    tft.setTextSize(4);
    tft.setCursor(18, 193);

    if (battery > 40) battery = 0;
    if (battery < 10) {
      tft.print(" ");
      tft.print(String(battery, 1));
    }
    else if (battery < 1) {
      tft.print("  ");
      tft.print(String(battery, 1));
    }
    else {
      tft.print("");
      tft.print(String(battery, 1));
    }
  }

  //////////////////////////////////
  //////Engine Hours Code/////////
  //////////////////////////////////

  if (pgnID == 0xfee5) {
    unsigned int h1 = data[0];
    unsigned int h2 = data[1];
    unsigned int h3 = data[2];
    unsigned int h4 = data[3];
    float hours = (((h1) + (h2 * 256) + (h3) + (h4))) * 0.05;
    tft.setTextColor(WHITE, DBLUE);
    tft.setTextSize(4);
    tft.setCursor(155, 193);

    if (hours > 99999) hours = 0;
    if (hours < 10) {
      tft.print("  ");
      tft.print(String(hours, 2));
      tft.print(" ");
    }
    else if (hours < 100) {
      tft.print("  ");
      tft.print(String(hours, 2));
    }
    else if (hours < 1000) {
      tft.print(" ");
      tft.print(String(hours, 2));
    }
    else {
      tft.print("");
      tft.print(String(hours, 2));
    }
  }

  //////////////////////////////////
  //////////Fuel Rate Code//////////
  //////////////////////////////////

  if (pgnID == 0xfef2) {
    unsigned int f1 = data[0];
    unsigned int f2 = data[1];
    float flow = ((f1) + (f2 * 256)) * 0.05;
    tft.setTextColor(WHITE, DBLUE);
    tft.setTextSize(3);
    tft.setCursor(350, 196);

    if (flow > 999) flow = 0;
    if (flow < 10) {
      tft.print("  ");
      tft.print(String(flow, 1));
    }
    else if (flow < 100) {
      tft.print(" ");
      tft.print(String(flow, 1));
    }
    else {
      tft.print("");
      tft.print(String(flow, 1));
    }

  }

}
////////////////////////////////////////////////////////////////////////////
///////////////////////////////END LOOP/////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
//////////////////////// ALARM SYSTEM LOOP BEGIN ////////////////////////////
/////////////////////////////////////////////////////////////////////////////

void alarmLoop() {

  ///////////////////// Common Alarm Begin ///////////////////////////
  commonInAlarm = digitalRead(24);
  if (commonInAlarm == false && commonStatus == true) {
    tft.fillRect(10, 260, 110, 40, RED);
    tft.setCursor(31, 265);
    tft.setTextColor(WHITE);  tft.setTextSize(2);
    tft.print("COMMON");
    tft.setCursor(38, 282);
    tft.setTextColor(WHITE);  tft.setTextSize(2);
    tft.print("ALARM");
    if (buzzerEnabled == false) buzzerEnabled = true;
  }
  if (commonInAlarm == false)  commonStatus = false;

  commonInNormal = digitalRead(24);
  if (commonInNormal == true && commonStatus == false) {
    tft.fillRect(10, 260, 110, 40, BLACK);
    tft.setCursor(31, 265);
    tft.setTextColor(WHITE);  tft.setTextSize(2);
    tft.print("COMMON");
    tft.setCursor(38, 282);
    tft.setTextColor(WHITE);  tft.setTextSize(2);
    tft.print("ALARM");
  }
  if (commonInNormal == true)  commonStatus = true;
  ///////////////////// Common Alarm End ///////////////////////////

  ///////////////////// Leakage Alarm Begin ////////////////////////
  leakageInAlarm = digitalRead(26);
  if (leakageInAlarm == false && leakageStatus == true) {
    tft.fillRect(130, 260, 110, 40, RED);
    tft.setCursor(144, 265);
    tft.setTextColor(WHITE);  tft.setTextSize(2);
    tft.print("LEAKAGE");
    tft.setCursor(157, 282);
    tft.setTextColor(WHITE);  tft.setTextSize(2);
    tft.print("ALARM");
    if (buzzerEnabled == false) buzzerEnabled = true;
  }
  if (leakageInAlarm == false)  leakageStatus = false;

  leakageInNormal = digitalRead(26);
  if (leakageInNormal == true && leakageStatus == false) {
    tft.fillRect(130, 260, 110, 40, BLACK);
    tft.setCursor(144, 265);
    tft.setTextColor(WHITE);  tft.setTextSize(2);
    tft.print("LEAKAGE");
    tft.setCursor(157, 282);
    tft.setTextColor(WHITE);  tft.setTextSize(2);
    tft.print("ALARM");
  }
  if (leakageInNormal == true)  leakageStatus = true;
  ///////////////////// Leakage Alarm End ////////////////////////

  //////////////// Ventilator Alarm Begin ////////////////////////
  ventInAlarm = digitalRead(28);
  if (ventInAlarm == false && ventStatus == true) {
    tft.fillRect(250, 260, 110, 40, RED);
    tft.setCursor(284, 265);
    tft.setTextColor(WHITE);  tft.setTextSize(2);
    tft.print("VENT");
    tft.setCursor(277, 282);
    tft.setTextColor(WHITE);  tft.setTextSize(2);
    tft.print("ALARM");
    if (buzzerEnabled == false) buzzerEnabled = true;
  }
  if (ventInAlarm == false)  ventStatus = false;

  ventInNormal = digitalRead(28);
  if (ventInNormal == true && ventStatus == false) {
    tft.fillRect(250, 260, 110, 40, BLACK);
    tft.setCursor(284, 265);
    tft.setTextColor(WHITE);  tft.setTextSize(2);
    tft.print("VENT");
    tft.setCursor(277, 282);
    tft.setTextColor(WHITE);  tft.setTextSize(2);
    tft.print("ALARM");
  }
  if (ventInNormal == true)  ventStatus = true;
  //////////////// Ventilator Alarm End ////////////////////////

  ///////////// F.O. Level Low Alarm Begin /////////////////////
  folowInAlarm = digitalRead(30);
  if (folowInAlarm == false && folowStatus == true) {
    tft.fillRect(370, 260, 45, 40, RED);
    tft.setCursor(382, 262);
    tft.setTextColor(WHITE);  tft.setTextSize(1);
    tft.print("FUEL");
    tft.setCursor(379, 276);
    tft.setTextColor(WHITE);  tft.setTextSize(1);
    tft.print("LEVEL");
    tft.setCursor(384, 290);
    tft.setTextColor(WHITE);  tft.setTextSize(1);
    tft.print("LOW");
    if (buzzerEnabled == false) buzzerEnabled = true;
  }
  if (folowInAlarm == false)  folowStatus = false;

  folowInNormal = digitalRead(30);
  if (folowInNormal == true && folowStatus == false) {
    tft.fillRect(370, 260, 45, 40, BLACK);
    tft.setCursor(382, 262);
    tft.setTextColor(WHITE);  tft.setTextSize(1);
    tft.print("FUEL");
    tft.setCursor(379, 276);
    tft.setTextColor(WHITE);  tft.setTextSize(1);
    tft.print("LEVEL");
    tft.setCursor(384, 290);
    tft.setTextColor(WHITE);  tft.setTextSize(1);
    tft.print("LOW");
  }
  if (folowInNormal == true)  folowStatus = true;
  ///////////// F.O. Level Low Alarm End ////////////////////////

  ///////////// F.O. Level High Alarm Begin /////////////////////
  fohighInAlarm = digitalRead(32);
  if (fohighInAlarm == false && fohighStatus == true) {
    tft.fillRect(425, 260, 45, 40, RED);
    tft.setCursor(437, 262);
    tft.setTextColor(WHITE);  tft.setTextSize(1);
    tft.print("FUEL");
    tft.setCursor(433, 276);
    tft.setTextColor(WHITE);  tft.setTextSize(1);
    tft.print("LEVEL");
    tft.setCursor(436, 290);
    tft.setTextColor(WHITE);  tft.setTextSize(1);
    tft.print("HIGH");
    if (buzzerEnabled == false) buzzerEnabled = true;
  }
  if (fohighInAlarm == false)  fohighStatus = false;

  fohighInNormal = digitalRead(32);
  if (fohighInNormal == true && fohighStatus == false) {
    tft.fillRect(425, 260, 45, 40, BLACK);
    tft.setCursor(437, 262);
    tft.setTextColor(WHITE);  tft.setTextSize(1);
    tft.print("FUEL");
    tft.setCursor(433, 276);
    tft.setTextColor(WHITE);  tft.setTextSize(1);
    tft.print("LEVEL");
    tft.setCursor(436, 290);
    tft.setTextColor(WHITE);  tft.setTextSize(1);
    tft.print("HIGH");
  }
  if (fohighInNormal == true)  fohighStatus = true;
  ////////////////// F.O. Level High Alarm End ////////////////////////////

  ////////////////// Buzzer Reset Button Begin ////////////////////////////
  resetButton = digitalRead(22);
  if (buzzerEnabled == true) {
    digitalWrite(34, HIGH);
  }
  if (buzzerEnabled == true && resetButton == false) buzzerEnabled = false;
  if (buzzerEnabled == false) {
    digitalWrite(34, LOW);
  }
  //////////////////// Buzzer Reset Button End ////////////////////////////
}
///////////////////////////////////////////////////////////////////////////
//////////////////////////ALARM SYSTEM LOOP END////////////////////////////
///////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////
///////////////////////////NODATA SCREEN BEGIN//////////////////////////////
////////////////////////////////////////////////////////////////////////////
void nodataScreen() {

  tft.fillScreen(BGRD);
 // fohighStatus == true;
  //Order 1
  tft.fillRect(0, 0, 480, 10, RED);
  tft.setCursor(61, 1);
  tft.setTextColor(WHITE);  tft.setTextSize(1);
  tft.print("DIESEL GENERATOR ZEPPELIN CONTAINER GEN-SET 3512B DITA MONITOR");

  //Order 2
  tft.fillRect(0, 310, 480, 10, RED);
  tft.setCursor(182, 311);
  tft.setTextColor(WHITE);  tft.setTextSize(1);
  tft.print("www.chebanlianka.com");

  //Order 3
  tft.drawRect(13, 18, 124, 74, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(143, 18, 194, 74, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(343, 18, 124, 74, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(13, 98, 124, 74, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(143, 98, 194, 74, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(343, 98, 124, 74, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(13, 178, 124, 74, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(143, 178, 194, 74, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(343, 178, 124, 74, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(8, 258, 114, 44, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(128, 258, 114, 44, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(248, 258, 114, 44, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(368, 258, 49, 44, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(423, 258, 49, 44, BLACK); // x, y, ширина, высота, цвет

  //Order 5
  tft.fillRect(15, 20, 120, 50, DBLUE);
  tft.fillRect(145, 20, 190, 50, DBLUE);
  tft.fillRect(345, 20, 120, 50, DBLUE);
  tft.fillRect(15, 100, 120, 50, DBLUE);
  tft.fillRect(145, 100, 190, 50, DBLUE);
  tft.fillRect(345, 100, 120, 50, DBLUE);
  tft.fillRect(15, 180, 120, 50, DBLUE);
  tft.fillRect(145, 180, 190, 50, DBLUE);
  tft.fillRect(345, 180, 120, 50, DBLUE);

  //Order 6
  tft.fillRect(15, 70, 120, 20, DGREY);
  tft.fillRect(145, 70, 190, 20, DGREY);
  tft.fillRect(345, 70, 120, 20, DGREY);
  tft.fillRect(15, 150, 120, 20, DGREY);
  tft.fillRect(145, 150, 190, 20, DGREY);
  tft.fillRect(345, 150, 120, 20, DGREY);
  tft.fillRect(15, 230, 120, 20, DGREY);
  tft.fillRect(145, 230, 190, 20, DGREY);
  tft.fillRect(345, 230, 120, 20, DGREY);

  //Order 4
  for (uint16_t a = 0; a < 2; a++)
  {
    tft.drawFastHLine(15, 70 + a, 120, BLACK);
    tft.drawFastHLine(145, 70 + a, 190, BLACK);
    tft.drawFastHLine(345, 70 + a, 120, BLACK);
    tft.drawFastHLine(15, 150 + a, 120, BLACK);
    tft.drawFastHLine(145, 150 + a, 190, BLACK);
    tft.drawFastHLine(345, 150 + a, 120, BLACK);
    tft.drawFastHLine(15, 230 + a, 120, BLACK);
    tft.drawFastHLine(145, 230 + a, 190, BLACK);
    tft.drawFastHLine(345, 230 + a, 120, BLACK);
  }

  ////////////////////////////////////////////////////////////////////////////
  //////////////////////////////Sensors Name//////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////

  ///////////////////////////////////////////
  tft.setCursor(112, 33);
  tft.setTextColor(WHITE);  tft.setTextSize(4);
  tft.print("C");
  tft.setCursor(34, 74);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("COOLANT");
  //////////////////////////////////////////
  tft.setCursor(260, 33);
  tft.setTextColor(WHITE);  tft.setTextSize(4);
  tft.print("rpm");
  tft.setCursor(170, 74);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("ENGINE SPEED");
  ////////////////////////////////////////////
  tft.setCursor(445, 23);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("k");
  tft.setCursor(445, 40);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("P");
  tft.setCursor(445, 52);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("a");
  tft.setCursor(352, 74);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("OIL PRESS");
  ///////////////////////////////////////
  tft.setCursor(115, 103);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("k");
  tft.setCursor(115, 120);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("P");
  tft.setCursor(115, 132);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("a");
  tft.setCursor(24, 154);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("BOOST PR.");
  ////////////////////////////////////////
  //tft.setCursor(280, 112);
  //tft.setTextColor(WHITE);  tft.setTextSize(4);
  //tft.print("kW");
  tft.setCursor(171, 154);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("ENGINE POWER");
  //////////////////////////////////
  tft.setCursor(445, 103);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("k");
  tft.setCursor(445, 120);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("P");
  tft.setCursor(445, 132);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("a");
  tft.setCursor(346, 154);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("FUEL PRESS");
  ////////////////////////////////////
  tft.setCursor(112, 193);
  tft.setTextColor(WHITE);  tft.setTextSize(4);
  tft.print("V");
  tft.setCursor(34, 234);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("BATTERY");
  ///////////////////////////////////
  tft.setCursor(173, 234);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("ENGINE HOURS");
  ////////////////////////////////////
  tft.setCursor(445, 182);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("L");
  tft.setCursor(445, 197);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("P");
  tft.setCursor(445, 213);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("H");
  tft.setCursor(353, 234);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("FUEL RATE");

  ////////////////////////////////////////////////////////////////////////////
  //////////////////////////////ZERO NUMBERS//////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////

  ///////////Temp/////////////
  tft.setCursor(57, 33);
  tft.setTextColor(WHITE);
  tft.setTextSize(4);
  tft.print(0);
  ////////Engine Speed/////////
  tft.setTextColor(WHITE);
  tft.setTextSize(4);
  tft.setCursor(200, 33);
  tft.println(0);
  ////////Oil Pressure/////////
  tft.setTextColor(WHITE);
  tft.setCursor(385, 33);
  tft.setTextSize(4);
  tft.print(0);
  //////Boost Pressure/////////
  tft.setTextColor(WHITE);
  tft.setCursor(57, 112);
  tft.setTextSize(4);
  tft.print(0);
  //////Engine Power/////////
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.setCursor(152, 200);
  tft.print("DG SWITCHED OFF");
  ///////Fuel Pressure/////////
  tft.setTextColor(WHITE);
  tft.setCursor(385, 112);
  tft.setTextSize(4);
  tft.print(0);
  //////////Battery////////////
  tft.setTextColor(WHITE);
  tft.setTextSize(4);
  tft.setCursor(57, 193);
  tft.println(0);
  //////////Fuel Rate///////////
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.setCursor(390, 196);
  tft.println(0);

  ///////////////////// Common Alarm Begin ///////////////////////////
  tft.fillRect(10, 260, 110, 40, RED);
  tft.setCursor(31, 265);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("COMMON");
  tft.setCursor(38, 282);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("ALARM");
  ///////////////////// Common Alarm End ///////////////////////////

  ///////////////////// Leakage Alarm Begin ////////////////////////
  tft.fillRect(130, 260, 110, 40, RED);
  tft.setCursor(144, 265);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("LEAKAGE");
  tft.setCursor(157, 282);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("ALARM");
  ///////////////////// Leakage Alarm End ////////////////////////

  //////////////// Ventilator Alarm Begin ////////////////////////
  tft.fillRect(250, 260, 110, 40, RED);
  tft.setCursor(284, 265);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("VENT");
  tft.setCursor(277, 282);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("ALARM");
  //////////////// Ventilator Alarm End ////////////////////////

  ///////////// F.O. Level Low Alarm Begin /////////////////////
  tft.fillRect(370, 260, 45, 40, RED);
  tft.setCursor(382, 262);
  tft.setTextColor(WHITE);  tft.setTextSize(1);
  tft.print("FUEL");
  tft.setCursor(379, 276);
  tft.setTextColor(WHITE);  tft.setTextSize(1);
  tft.print("LEVEL");
  tft.setCursor(384, 290);
  tft.setTextColor(WHITE);  tft.setTextSize(1);
  tft.print("LOW");
  ///////////// F.O. Level Low Alarm End ////////////////////////

  ///////////// F.O. Level High Alarm Begin /////////////////////
  tft.fillRect(425, 260, 45, 40, RED);
  tft.setCursor(437, 262);
  tft.setTextColor(WHITE);  tft.setTextSize(1);
  tft.print("FUEL");
  tft.setCursor(433, 276);
  tft.setTextColor(WHITE);  tft.setTextSize(1);
  tft.print("LEVEL");
  tft.setCursor(436, 290);
  tft.setTextColor(WHITE);  tft.setTextSize(1);
  tft.print("HIGH");
  ////////////////// F.O. Level High Alarm End ////////////////////////////

  while (!mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
    tft.setCursor(160, 112);
    tft.setTextSize(4);
    tft.print("No Data");
    tft.setTextColor(GREENYELLOW, DBLUE);
    delay(100);
    tft.setCursor(160, 112);
    tft.setTextSize(4);
    tft.print("No Data");
    tft.setTextColor(MAGENTA, DBLUE);
    delay(100);
  }
  ////////////////////////////////////////////////////////////////////////////
  ////////////////////////////NODATA SCREEN END///////////////////////////////
  ////////////////////////////////////////////////////////////////////////////
}

////////////////////////////////////////////////////////////////////////////
/////////////////////////// DATA SCREEN BEGIN //////////////////////////////
////////////////////////////////////////////////////////////////////////////

void dataScreen() {

  tft.fillScreen(BGRD);
//  fohighStatus == true;

  //Order 1
  tft.fillRect(0, 0, 480, 10, RED);
  tft.setCursor(61, 1);
  tft.setTextColor(WHITE);  tft.setTextSize(1);
  tft.print("DIESEL GENERATOR ZEPPELIN CONTAINER GEN-SET 3512B DITA MONITOR");
  //Order 2
  tft.fillRect(0, 310, 480, 10, RED);
  tft.setCursor(182, 311);
  tft.setTextColor(WHITE);  tft.setTextSize(1);
  tft.print("www.chebanlianka.com");
  //Order 3
  tft.drawRect(13, 18, 124, 74, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(143, 18, 194, 74, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(343, 18, 124, 74, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(13, 98, 124, 74, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(143, 98, 194, 74, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(343, 98, 124, 74, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(13, 178, 124, 74, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(143, 178, 194, 74, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(343, 178, 124, 74, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(8, 258, 114, 44, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(128, 258, 114, 44, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(248, 258, 114, 44, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(368, 258, 49, 44, BLACK); // x, y, ширина, высота, цвет
  tft.drawRect(423, 258, 49, 44, BLACK); // x, y, ширина, высота, цвет
  //Order 5
  tft.fillRect(15, 20, 120, 50, DBLUE);
  tft.fillRect(145, 20, 190, 50, DBLUE);
  tft.fillRect(345, 20, 120, 50, DBLUE);
  tft.fillRect(15, 100, 120, 50, DBLUE);
  tft.fillRect(145, 100, 190, 50, DBLUE);
  tft.fillRect(345, 100, 120, 50, DBLUE);
  tft.fillRect(15, 180, 120, 50, DBLUE);
  tft.fillRect(145, 180, 190, 50, DBLUE);
  tft.fillRect(345, 180, 120, 50, DBLUE);
  //Order 6
  tft.fillRect(15, 70, 120, 20, DGREY);
  tft.fillRect(145, 70, 190, 20, DGREY);
  tft.fillRect(345, 70, 120, 20, DGREY);
  tft.fillRect(15, 150, 120, 20, DGREY);
  tft.fillRect(145, 150, 190, 20, DGREY);
  tft.fillRect(345, 150, 120, 20, DGREY);
  tft.fillRect(15, 230, 120, 20, DGREY);
  tft.fillRect(145, 230, 190, 20, DGREY);
  tft.fillRect(345, 230, 120, 20, DGREY);
  //Order 4
  for (uint16_t a = 0; a < 2; a++) {
    tft.drawFastHLine(15, 70 + a, 120, BLACK);
    tft.drawFastHLine(145, 70 + a, 190, BLACK);
    tft.drawFastHLine(345, 70 + a, 120, BLACK);
    tft.drawFastHLine(15, 150 + a, 120, BLACK);
    tft.drawFastHLine(145, 150 + a, 190, BLACK);
    tft.drawFastHLine(345, 150 + a, 120, BLACK);
    tft.drawFastHLine(15, 230 + a, 120, BLACK);
    tft.drawFastHLine(145, 230 + a, 190, BLACK);
    tft.drawFastHLine(345, 230 + a, 120, BLACK);
  }

  ////////////////////////////////////////////////////////////////////////////
  //////////////////////////////Sensors Name//////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////

  ///////////////////////////////////////////
  tft.setCursor(95, 33);
  tft.setTextColor(WHITE);  tft.setTextSize(4);
  tft.print("C");
  tft.setCursor(34, 74);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("COOLANT");
  //////////////////////////////////////////
  tft.setCursor(260, 33);
  tft.setTextColor(WHITE);  tft.setTextSize(4);
  tft.print("rpm");
  tft.setCursor(170, 74);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("ENGINE SPEED");
  ////////////////////////////////////////////
  tft.setCursor(445, 23);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("k");
  tft.setCursor(445, 40);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("P");
  tft.setCursor(445, 52);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("a");
  tft.setCursor(352, 74);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("OIL PRESS");
  ///////////////////////////////////////
  tft.setCursor(115, 103);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("k");
  tft.setCursor(115, 120);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("P");
  tft.setCursor(115, 132);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("a");
  tft.setCursor(24, 154);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("BOOST PR.");
  ////////////////////////////////////////
  tft.setCursor(280, 112);
  tft.setTextColor(WHITE);  tft.setTextSize(4);
  tft.print("kW");
  tft.setCursor(171, 154);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("ENGINE POWER");
  //////////////////////////////////
  tft.setCursor(445, 103);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("k");
  tft.setCursor(445, 120);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("P");
  tft.setCursor(445, 132);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("a");
  tft.setCursor(346, 154);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("FUEL PRESS");
  ////////////////////////////////////
  tft.setCursor(113, 193);
  tft.setTextColor(WHITE);  tft.setTextSize(4);
  tft.print("V");

  tft.setCursor(34, 234);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("BATTERY");
  ///////////////////////////////////
  tft.setCursor(173, 234);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("ENGINE HOURS");
  ////////////////////////////////////
  tft.setCursor(445, 182);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("L");
  tft.setCursor(445, 197);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("P");
  tft.setCursor(445, 213);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("H");

  tft.setCursor(353, 234);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("FUEL RATE");
  
  ////////////////////////////////////////////////////////////////////////////
  ////////////////////////////ALARMSCREENS BEGIN//////////////////////////////
  ////////////////////////////////////////////////////////////////////////////

  ///////////////////// Common Alarm Begin ///////////////////////////
  tft.fillRect(10, 260, 110, 40, BLACK);
  tft.setCursor(31, 265);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("COMMON");
  tft.setCursor(38, 282);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("ALARM");
  ///////////////////// Common Alarm End ///////////////////////////
  ///////////////////// Leakage Alarm Begin ////////////////////////
  tft.fillRect(130, 260, 110, 40, BLACK);
  tft.setCursor(144, 265);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("LEAKAGE");
  tft.setCursor(157, 282);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("ALARM");
  ///////////////////// Leakage Alarm End ////////////////////////
  //////////////// Ventilator Alarm Begin ////////////////////////
  tft.fillRect(250, 260, 110, 40, BLACK);
  tft.setCursor(284, 265);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("VENT");
  tft.setCursor(277, 282);
  tft.setTextColor(WHITE);  tft.setTextSize(2);
  tft.print("ALARM");
  //////////////// Ventilator Alarm End ////////////////////////
  ///////////// F.O. Level Low Alarm Begin /////////////////////
  tft.fillRect(370, 260, 45, 40, BLACK);
  tft.setCursor(382, 262);
  tft.setTextColor(WHITE);  tft.setTextSize(1);
  tft.print("FUEL");
  tft.setCursor(379, 276);
  tft.setTextColor(WHITE);  tft.setTextSize(1);
  tft.print("LEVEL");
  tft.setCursor(384, 290);
  tft.setTextColor(WHITE);  tft.setTextSize(1);
  tft.print("LOW");
  ///////////// F.O. Level Low Alarm End ////////////////////////
  ///////////// F.O. Level High Alarm Begin /////////////////////
  tft.fillRect(425, 260, 45, 40, BLACK);
  tft.setCursor(437, 262);
  tft.setTextColor(WHITE);  tft.setTextSize(1);
  tft.print("FUEL");
  tft.setCursor(433, 276);
  tft.setTextColor(WHITE);  tft.setTextSize(1);
  tft.print("LEVEL");
  tft.setCursor(436, 290);
  tft.setTextColor(WHITE);  tft.setTextSize(1);
  tft.print("HIGH");
  ////////////////// F.O. Level High Alarm End ////////////////////////////

  ////////////////////////////////////////////////////////////////////////////
  ////////////////////////////ALARMSCREENS END//////////////////////////////
  ////////////////////////////////////////////////////////////////////////////
}
  ////////////////////////////////////////////////////////////////////////////
  //////////////////////////// DATA SCREEN END ///////////////////////////////
  ////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////
/////////////////////////////PROGRAM END////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////
///////////////  Caterpillar Can-Bus Monitor   ///////////////
///////////////         Protocol: J1939        ///////////////
///////////////  Engine Model: GEN-SET 3512B   ///////////////
///////////////    Oleksandr Cheban-Lianka     ///////////////
///////////////      www.chebanlianka.com      ///////////////
///////////////   oleksandr@chebanlianka.com   ///////////////
//////////////////////////////////////////////////////////////


TANK LEVEL MONITORING DEVICE CODE

boolean buzzerEnabled = true;

void setup() {
   pinMode(A2, OUTPUT); // Buzzer Output
   digitalWrite(A2, LOW);
   pinMode(A3, OUTPUT); // Red Led - F.O. Level High
   digitalWrite(A3, LOW);
   pinMode(A4, OUTPUT); // Green Led - Power
   digitalWrite(A4, HIGH);
   pinMode(A5, OUTPUT); // Yellow Led F.O. Level Low
   digitalWrite(A5, LOW);
   pinMode(A0, INPUT_PULLUP); // F.O. Level Low Input
   pinMode(A1, INPUT_PULLUP); // F.O. Level High Input
   pinMode(11, INPUT_PULLUP); // Buzzer OFF Button
}

void loop() { 
      
// Reading input status

  boolean folow = digitalRead(A0);
  boolean fohigh = digitalRead(A1);
  boolean resetbutton = digitalRead(11);
   
  if (resetbutton && folow && fohigh && !buzzerEnabled) {buzzerEnabled = !buzzerEnabled;}    
        
  if (!resetbutton && (!folow or !fohigh)) {buzzerEnabled = !buzzerEnabled;}

//Alarm F.O. Level LOW
        digitalRead(A0);
      if (!folow) { 
        digitalWrite(A5, HIGH); 
      if (buzzerEnabled) {digitalWrite(A2, HIGH);} //Buzzer Alarm F.O. Level LOW
        delay (1000);
        digitalWrite(A5, LOW);
      if (buzzerEnabled) {digitalWrite(A2, LOW);} //Buzzer Alarm F.O. Level LOW
        delay (1000);
      }
       else {
        digitalWrite(A5, LOW);
        digitalWrite(A2, LOW); 
      }

//LED Alarm F.O. Level High
        digitalRead(A1);
       if (!fohigh) { 
        digitalWrite(A3, HIGH); 
       if (buzzerEnabled) {digitalWrite(A2, HIGH);} //Buzzer Alarm F.O. Level High
        delay (1000);
        digitalWrite(A3, LOW);
       if (buzzerEnabled) {digitalWrite(A2, LOW);} //Buzzer Alarm F.O. Level High
        delay (1000);
      }
       else {
        digitalWrite(A3, LOW); 
        digitalWrite(A2, LOW);
      }
}

Leave a Reply