I tweaked the code to be a little more readable and managable and added some documentation
Code:
/***************************************************
This is our GFX example for the Adafruit ILI9341 Breakout and Shield
----> http://www.adafruit.com/products/1651
Check out the links above for our tutorials and wiring diagrams
These displays use SPI to communicate, 4 or 5 pins are required to
interface (RST is optional)
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
// Code provided by Smoke And Wires http://www.smokeandwires.co.nz
// original write up: https://www.instructables.com/Arduino-Motion-Tracker-From-Alien-Isolation/?fbclid=IwAR2P0H8tUwBisNGaP4C2vccz-fbBOlxxfdm8EIJJEHgAZnKOtgemeBjAZ3M
// updated write up: http://forum.alienslegacy.com/viewtopic.php?f=3&t=19105&p=276520#p276520
/* parts used:
* 2.4" TFT display (display driver and settings will vary based upon display used)
3 pcs PIR sensor (HC-SR501) (this is a 2 or so dollar part)
1pcs ultrasonic distance sensor (HC-SR04) (this is also a cheap sensor)
1pcs piezo buzzer
1pcs 100ohm resistor
*/
// updates by knoxvilles_joker on alienslegacy.com 01/02/2023
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
// For the Adafruit shield, these are the default. All pins are declared to aid in connections
#define TFT_DC 9
#define TFT_CS 10
#define TFT_MOSI 11
#define TFT_CLK 13
#define TFT_RST 12
#define TFT_MISO 8
#define TFT_CS 10
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
// defines pins numbers
const int trigPin = 6;// on ultrasound meter, pins are declared on board
const int echoPin = 5;
const int pirPin = 4;// the IR sensors, with pins on bottom facing you, left is positive, mid is signal, right is negative
const int pirPinL = 3;// units will not work properly if polarity is reversed and pins are not declared on board
const int pirPinR = 2;// left dial determines distance, right dial determines time delay, jumper is to be on right side
const int Spin = 7;// this is for the buzzer
// defines variables
long duration; int distance;
// declaration for display using ili9340c driver board
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
void setup(void) {
//Input from the PIR sensors
pinMode(pirPin, INPUT); pinMode(pirPinL, INPUT); pinMode(pirPinR, INPUT);
// Input from the Ultrasound sensors
pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT);
// Output for the triggerpins
pinMode(Spin, OUTPUT);
//Start the serial communication
Serial.begin(9600);
uint16_t identifier = tft.read16();
//Serial.print(F("LCD driver chip: ")); //You can check the driver of your tft monitor
Serial.println(identifier, HEX); tft.begin(identifier); tft.setRotation(3); txt(); tft.setRotation(0); tft.fillScreen(BLACK);
}
void loop(void) {
table();
// check if motion detected
if (digitalRead(pirPin) == HIGH) { ping(us(), 160); table(); p_s(); digitalWrite(pirPin,LOW); }
//if no motion in front check the left and right sides
else{
if (digitalRead(pirPinL) == HIGH) { sideL(); table(); p_s(); digitalWrite(pirPinL,LOW); }
if (digitalRead(pirPinR) == HIGH) { sideR(); table(); p_s(); digitalWrite(pirPinR,LOW); }
}
}
//create the monitor
void table() { tft.drawLine(10, 10, 240, 160, GREEN); tft.drawLine(240, 160, 10, 310, GREEN); tft.drawCircle(240, 160, 40, GREEN); tft.drawCircle(240, 160, 80, GREEN); tft.drawCircle(240, 160, 140, GREEN); tft.drawCircle(240, 160, 200, GREEN); }
//Starting :)
void txt() {
tft.fillScreen(BLACK);
tft.setCursor(60, 20);
tft.setTextColor(WHITE);
tft.setTextSize(5);
tft.println("B2-LAB");
tft.setCursor(20, 80);
tft.setTextColor(GREEN);
tft.setTextSize(3);
tft.println("MOTION-TRACKER");
tft.setCursor(10,130);
tft.setTextColor(RED);
tft.setTextSize(2);
tft.println("^VERTICAL WHEN OPERATING^");
tft.setCursor(60, 150);
tft.setTextColor(GREEN);
tft.setTextSize(2);
tft.print("Calibrate Sensors");
tft.setCursor(10, 170);
tft.setTextSize(3);
for(int i=0;i<16;i++) { tft.print("."); delay(1000); }
}
//Display the motion
void ping(int dis, int x) { tft.fillCircle(dis, x, 10, GREEN); Serial.println(x); delay(1000); tft.fillCircle(dis, x, 10, BLACK); }
//ultrasound distance device control, Sets the trigPin on HIGH state for 10 micro seconds, Reads the echoPins, returns the sound wave travel time in microseconds, Calculating the distance, displaying properly
int us() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = duration * 0.034 / 2; return (240 - (distance * 0.7)); }
//right PIR sensor event
void sideR() { tft.fillTriangle(240,160,240,120,195,130,GREEN); delay(200); tft.fillTriangle(240,160,240,120,195,130,BLACK); }
//left PIR sensor event
void sideL() { tft.fillTriangle(240,160,240,200,195,190,GREEN); delay(200); tft.fillTriangle(240,160,240,200,195,190,BLACK); }
//Tracking sound effect
void p_s() { digitalWrite(Spin,HIGH); delay(100); digitalWrite(Spin,LOW); }