#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>
#include <SD.h> // If loading BMPs from SD card
// ILI9341 pin definitions
#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// Motor and control pin definitions
const int motorPWMPin = 3;
const int motorDirPin = 4;
const int motorBrakePin = 5;
const int sensorPin = A0; // First sensor for homing
const int secondSensorPin = A1; // Second sensor for homing
const int abortButtonPin = A2;
const int upButtonPin = A3;
const int downButtonPin = A4;
const int homeButtonPin = A5;
const int resetButtonPin = A6;
const int startButtonPin = A7;
const int rotaryPinA = A8;
const int rotaryPinB = A9;
const int rotaryButtonPin = A10;
// Define the messages to be scrolled on the display
String message = "";
String message2 = "System is currently in collision zone.";
// Variables for system state
bool systemActive = false;
bool isAborted = false;
int motorSpeed = 128; // Default speed for PWM control (0 - 255)
int lastStateA = LOW; // Last state of rotary encoder A
// Bitmap image for display (Replace with your BMP file in bytes if needed)
const uint16_t myImage[48* 45] = {
// 'ardunio_image', 240x320px
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x528a, 0x8410, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xb5b6, 0xffdf, 0xffdf, 0x0000,
0x1082, 0xc618, 0xffff, 0xb596, 0x0000, 0x5acb, 0xef5d, 0x6b6d, 0x0000, 0x0000, 0x7bcf, 0xef7d, 0xffff, 0xffff, 0x0000, 0x0000,
0x0861, 0x7bef, 0xb596, 0x0000, 0x0000, 0x0000, 0x0000, 0x4208, 0xce79, 0xffff, 0x5aeb, 0x0020, 0x0000, 0x0000, 0x0000, 0x0000,
0x10a2, 0x10a2, 0x0000, 0x0000, 0x0861, 0x630c, 0x8430, 0x0000, 0x0000, 0x0861, 0x10a2, 0x0020, 0x0000, 0xd6ba, 0xffdf, 0x0000,
0x1082, 0xc618, 0xffff, 0xb596, 0x0000, 0x5acb, 0xef5d, 0x6b6d, 0x0000, 0x0000, 0x0861, 0xd6ba, 0xffff, 0xef5d, 0x0000, 0x0000,
0x0861, 0x7bef, 0xb596, 0x0000, 0x0000, 0x0861, 0x0861, 0x0020, 0x10a2, 0xce59, 0x5aeb, 0x0020, 0x0000, 0x10a2, 0x10a2, 0x10a2,
0xef5d, 0xd69a, 0x0000, 0x18c3, 0x94b2, 0xf79e, 0x9492, 0x0000, 0x1082, 0x9cf3, 0xce59, 0x2965, 0x0000, 0x632c, 0xf7be, 0x0000,
0x1082, 0xc618, 0xffff, 0xb596, 0x0000, 0x5acb, 0xef5d, 0x6b6d, 0x0000, 0x0000, 0x0000, 0xce59, 0xffff, 0x52aa, 0x0000, 0x0000,
0x0861, 0x7bef, 0xb596, 0x0000, 0x1082, 0x8c51, 0x8c71, 0x18e3, 0x0000, 0xbdf7, 0x5aeb, 0x0020, 0x0000, 0xef5d, 0xef5d, 0xef5d,
0xffff, 0xe73c, 0x0000, 0x18e3, 0xa514, 0xffff, 0x9492, 0x0000, 0x1082, 0xad75, 0xffff, 0x39e7, 0x0000, 0x4208, 0xf79e, 0x0000,
0x1082, 0xc618, 0xffff, 0xb596, 0x0000, 0x5acb, 0xef5d, 0x6b6d, 0x0000, 0x10a2, 0x0020, 0x8410, 0xf79e, 0x0841, 0x0000, 0x0000,
0x0861, 0x7bef, 0xb596, 0x0000, 0x1082, 0x9492, 0xdefb, 0x4a49, 0x0000, 0xbdd7, 0x5aeb, 0x0020, 0x0000, 0xc618, 0xc618, 0xc618,
0xffff, 0xe73c, 0x0000, 0x18e3, 0xa514, 0xffff, 0x9492, 0x0000, 0x0861, 0x7bcf, 0xb596, 0x2945, 0x0000, 0x8c71, 0xf7be, 0x0000,
0x1082, 0xc618, 0xffff, 0xb596, 0x0000, 0x5acb, 0xef5d, 0x6b6d, 0x0000, 0x6b4d, 0x2104, 0x10a2, 0xbdf7, 0x0020, 0x18e3, 0x0861,
0x0861, 0x7bef, 0xb596, 0x0000, 0x0861, 0x5aeb, 0x5acb, 0x0861, 0x1082, 0xce59, 0x5aeb, 0x0020, 0x0000, 0x0000, 0x0000, 0x0000,
0xffff, 0xe73c, 0x0000, 0x18e3, 0xa514, 0xffff, 0x9492, 0x0000, 0x0000, 0x0000, 0x0000, 0x18e3, 0x528a, 0xef7d, 0xffdf, 0x0000,
0x1082, 0xc618, 0xffff, 0xb596, 0x0000, 0x5acb, 0xef5d, 0x6b6d, 0x0000, 0x9cf3, 0x39c7, 0x0000, 0x4a69, 0x0000, 0x8c71, 0x0841,
0x0861, 0x7bef, 0xb596, 0x0000, 0x0000, 0x0841, 0x0861, 0x2124, 0x73ae, 0xe73c, 0x5aeb, 0x0020, 0x0000, 0x5aeb, 0x5acb, 0x5acb,
0xffff, 0xe73c, 0x0000, 0x18e3, 0xa514, 0xffff, 0x9492, 0x0000, 0x0841, 0x0841, 0x0000, 0x2965, 0xdedb, 0xffff, 0xffdf, 0x0000,
0x0861, 0x8c51, 0xf79e, 0x9492, 0x0000, 0x630c, 0xef7d, 0x6b6d, 0x0000, 0xa514, 0x6b6d, 0x0861, 0x0000, 0x0000, 0xef5d, 0x0841,
0x0861, 0x7bef, 0xb596, 0x0000, 0x0841, 0x4a69, 0x8430, 0xad55, 0xef5d, 0xffff, 0x5aeb, 0x0020, 0x0000, 0xffff, 0xffdf, 0xffdf,
0xffff, 0xe73c, 0x0000, 0x18e3, 0xa514, 0xffff, 0x9492, 0x0000, 0x1082, 0x5acb, 0x2124, 0x0000, 0x7bef, 0xf7be, 0xffff, 0x2104,
0x0000, 0x2104, 0x738e, 0x31a6, 0x0020, 0x738e, 0xffdf, 0x6b6d, 0x0000, 0xa514, 0xce79, 0x2104, 0x0000, 0x2965, 0xf7be, 0x0841,
0x0861, 0x7bef, 0xb596, 0x0000, 0x1082, 0x94b2, 0xffff, 0xffff, 0xffff, 0xffff, 0x5aeb, 0x0020, 0x0000, 0xffff, 0xffff, 0xffff,
0xffff, 0xe73c, 0x0000, 0x18e3, 0xa514, 0xffff, 0x9492, 0x0000, 0x1082, 0xad55, 0x73ae, 0x1082, 0x1082, 0xbdd7, 0xffff, 0xad55,
0x2104, 0x0000, 0x0000, 0x0841, 0x4228, 0xc638, 0xffff, 0x6b6d, 0x0000, 0xa514, 0xffdf, 0x3186, 0x0000, 0xad75, 0xf7be, 0x0841,
0x0861, 0x7bef, 0xb596, 0x0000, 0x1082, 0x94b2, 0xffff, 0xffff, 0xffff, 0xffff, 0x5aeb, 0x0020, 0x0000, 0xffff, 0xffff, 0xffff,
0xffff, 0xf7be, 0xa514, 0xad55, 0xdefb, 0xffff, 0xd6ba, 0xa514, 0xad55, 0xe71c, 0xdedb, 0xad55, 0xa514, 0xdedb, 0xffff, 0xffff,
0xb5b6, 0xa514, 0xa514, 0xa534, 0xce79, 0xffff, 0xffff, 0xce59, 0xa514, 0xdefb, 0xffff, 0xb5b6, 0xa514, 0xffdf, 0xffdf, 0xa534,
0xa534, 0xd69a, 0xe71c, 0xa514, 0xad55, 0xdedb, 0xffff, 0xffff, 0xffff, 0xffff, 0xc618, 0xa514, 0xa514, 0xffff, 0xffff, 0xffff,
0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419,
0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419,
0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419, 0x6419,
0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237,
0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237,
0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237, 0x0237,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217,
0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217, 0x0217
};
void setup() {
// Initialize serial communication and TFT display
// Initialize serial communication and TFT display
Serial.begin(9600);
Serial.println("System Idle");
tft.begin();
// Adjust width and height as per the image dimensions
tft.setRotation(3); // Adjust for your orientation
tft.fillScreen(ILI9341_WHITE);
tft.setCursor(10, 80);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.println("System Idle");
tft.setCursor(10, 150);
tft.setTextColor(ILI9341_GREEN);
tft.println("Press Start");
drawImage(myImage, 10, 10, 48, 45);
delay(500);
// Configure pins
pinMode(motorPWMPin, OUTPUT);
pinMode(motorDirPin, OUTPUT);
pinMode(motorBrakePin, OUTPUT);
pinMode(sensorPin, INPUT);
pinMode(secondSensorPin, INPUT);
pinMode(abortButtonPin, INPUT_PULLUP);
pinMode(upButtonPin, INPUT_PULLUP);
pinMode(downButtonPin, INPUT_PULLUP);
pinMode(homeButtonPin, INPUT_PULLUP);
pinMode(resetButtonPin, INPUT_PULLUP);
pinMode(startButtonPin, INPUT_PULLUP);
pinMode(rotaryPinA, INPUT_PULLUP);
pinMode(rotaryPinB, INPUT_PULLUP);
pinMode(rotaryButtonPin, INPUT_PULLUP); // Encoder button
}
void loop() {
// Wait for start button on startup
if (!systemActive) {
// Check for start button press
if (digitalRead(startButtonPin) == LOW) {
systemActive = true;
tft.fillScreen(ILI9341_WHITE);
tft.setCursor(10, 80);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.println("System Ready");
tft.setCursor(10, 120);
tft.println("Let's Laser");
tft.setCursor(10, 160);
tft.println("Home the stage");
drawImage(myImage, 10, 10, 48, 45);
delay(1000);
} else {
// Scroll the message
for (int i = 0; i < message.length(); i++) {
tft.setCursor(0, 0);
tft.print(message.substring(i) + " " + message.substring(0, i));
delay(200); // Adjust delay for scroll speed
// Check for start button during scrolling
if (digitalRead(startButtonPin) == LOW) {
systemActive = true;
tft.fillScreen(ILI9341_WHITE);
tft.setCursor(10, 80);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.println("System Ready");
tft.setCursor(10, 120);
tft.println("Let's Laser");
tft.setCursor(10, 160);
tft.println("Home the stage");
drawImage(myImage, 10, 10, 48, 45);
delay(1000);
break;
}
}
}
} else {
// Check for abort button press
if (digitalRead(abortButtonPin) == LOW) {
handleAbort();
Serial.println("System stopped - abort button pressed");
tft.fillScreen(ILI9341_WHITE);
tft.setCursor(10, 80);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.println("System stopped");
drawImage(myImage, 10, 10, 48, 45);
}
// Check if rotary encoder button is pressed to set speed to max (255)
if (digitalRead(rotaryButtonPin) == LOW) {
motorSpeed = 255;
Serial.println("Max speed set by encoder button");
// Update LCD display
tft.fillScreen(ILI9341_WHITE);
tft.setCursor(10, 80);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.println("Speed: MAX ");
tft.setCursor(10, 120);
tft.println("Take Care.....");
drawImage(myImage, 10, 10, 48, 45);
// Optional: short delay to avoid bouncing effects
delay(200);
}
// Start homing sequence
if (digitalRead(homeButtonPin) == LOW) {
if (digitalRead(secondSensorPin) == HIGH) {
for (int i = 0; i < message.length(); i++) {
tft.setCursor(10, 80);
tft.print(message2.substring(i) + " " + message2.substring(0, i));
delay(200); // Adjust delay for scroll speed
Serial.println("Stage in collision zone.");
tft.setCursor(10, 120);
tft.print("Move stage up");
drawImage(myImage, 10, 10, 48, 45);
}
} else {
startHoming();
}
}
// Check for manual movement buttons
if (systemActive) {
if (digitalRead(upButtonPin) == LOW) {
moveMotorUp();
} else if (digitalRead(downButtonPin) == LOW) {
moveMotorDown();
} else {
engageBrake(); // Stop motor if no button is pressed
}
// Adjust speed with rotary encoder
motorSpeed = adjustSpeedWithEncoder();
// Check for reset after abort
if (isAborted && digitalRead(resetButtonPin) == LOW) {
resetSystem();
}
}
}
}
// Homing function
void startHoming() {
Serial.println("Homing started.");
tft.fillScreen(ILI9341_WHITE);
tft.setCursor(10, 80);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.println("Homing...");
drawImage(myImage, 10, 10, 48, 45);
engageBrake();
// Move down until second sensor is triggered
digitalWrite(motorDirPin, LOW); // Down direction
digitalWrite(motorBrakePin, LOW); // Release brake
while (digitalRead(secondSensorPin) == LOW) {
analogWrite(motorPWMPin, motorSpeed);
// Check for abort during homing
if (digitalRead(abortButtonPin) == LOW) {
handleAbort();
return;
}
}
engageBrake();
// Move up until the first sensor is triggered
digitalWrite(motorDirPin, HIGH); // Up direction
digitalWrite(motorBrakePin, LOW); // Release brake
while (digitalRead(sensorPin) == LOW) {
analogWrite(motorPWMPin, motorSpeed);
// Check for abort during homing
if (digitalRead(abortButtonPin) == LOW) {
handleAbort();
return;
}
}
engageBrake();
Serial.println("Homing complete.");
tft.fillScreen(ILI9341_WHITE);
tft.setCursor(10, 80);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.println("Homing Complete");
drawImage(myImage, 10, 10, 48, 45);
delay(500);
}
// Motor control functions
void moveMotorUp() {
digitalWrite(motorDirPin, HIGH); // Up direction
digitalWrite(motorBrakePin, LOW); // Release brake
analogWrite(motorPWMPin, motorSpeed);
tft.fillScreen(ILI9341_WHITE);
tft.setCursor(10, 80);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.println("Moving up");
drawImage(myImage, 10, 10, 48, 45);
delay(2000);
}
void moveMotorDown() {
digitalWrite(motorDirPin, LOW); // Down direction
digitalWrite(motorBrakePin, LOW); // Release brake
analogWrite(motorPWMPin, motorSpeed);
tft.fillScreen(ILI9341_WHITE);
tft.setCursor(10, 80);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.println("Moving down");
drawImage(myImage, 10, 10, 48, 45);
delay(2000);
}
void engageBrake() {
analogWrite(motorPWMPin, 0); // Stop motor
digitalWrite(motorBrakePin, HIGH); // Engage brake
}
int adjustSpeedWithEncoder() {
int currentStateA = digitalRead(rotaryPinA);
int encoderValue = 0;
if (currentStateA == HIGH && lastStateA == LOW) {
if (digitalRead(rotaryPinB) == LOW) {
encoderValue--; // Rotate left
} else {
encoderValue++; // Rotate right
}
}
lastStateA = currentStateA;
// Adjust motor speed based on encoder
motorSpeed = constrain(motorSpeed + encoderValue, 0, 255);
return motorSpeed;
}
void handleAbort() {
isAborted = true;
engageBrake();
tft.fillScreen(ILI9341_WHITE);
tft.setCursor(10, 80);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.println("System Aborted");
drawImage(myImage, 0, 0, 40, 38);
delay(500);
}
void resetSystem() {
isAborted = false;
systemActive = false;
tft.fillScreen(ILI9341_WHITE);
tft.setCursor(10, 80);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.println("System Reset");
//drawImage(myImage, 10, 10, 40, 38);
delay(500);
}
void drawImage(const uint16_t *image, int x, int y, int width, int height) {
int index = 0;
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
tft.drawPixel(x + i, y + j, image[index++]);
}
}
}