#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Keypad.h>
#include <SPI.h>
// Define pins for the TFT display
#define TFT_CS 15
#define TFT_RST 2
#define TFT_DC 4
#define TFT_SCK 18
#define TFT_MOSI 23
#define TFT_MISO 19
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {13, 12, 14, 27}; // Define row pins
byte colPins[COLS] = {26, 25, 33, 32}; // Define column pins
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// Define LED pin
#define LED_PIN 2
// Variables to track the state
enum Action {
ACTION_NONE,
ACTION_FOREVER_LOOP,
ACTION_LED_ON,
ACTION_LED_OFF,
ACTION_DELAY
};
Action selectedAction = ACTION_NONE;
String blockSequence = "";
int delayValue = 1000; // Default delay value
void setup() {
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
pinMode(LED_PIN, OUTPUT); // Use defined LED_PIN
displayMenu();
}
void loop() {
char key = keypad.getKey();
if (key) {
switch (key) {
case '1':
selectedAction = ACTION_FOREVER_LOOP;
addBlockToSequence("Forever Loop");
break;
case '2':
displayLEDSwitchMenu();
break;
case '3':
displayDelayInput();
break;
case '*':
executeSequence();
break;
case '#':
if (selectedAction == ACTION_LED_ON || selectedAction == ACTION_LED_OFF) {
addBlockToSequence(selectedAction == ACTION_LED_ON ? "LED On" : "LED Off");
displayMenu();
} else if (selectedAction == ACTION_DELAY) {
addBlockToSequence("Delay " + String(delayValue) + " ms");
displayMenu();
}
break;
}
// Handle numeric input for delay value
if (selectedAction == ACTION_DELAY && isDigit(key)) {
delayValue = delayValue * 10 + (key - '0'); // Append digit to delay value
displayDelayInput();
}
}
}
void displayMenu() {
tft.fillScreen(ILI9341_BLACK);
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(10, 10);
tft.print("Menu:");
tft.setTextSize(2);
tft.setCursor(10, 50);
tft.print("1. Forever Loop");
tft.setCursor(10, 90);
tft.print("2. LED On/Off");
tft.setCursor(10, 130);
tft.print("3. Delay");
// Display selected blocks on the right side
displayBlocks();
}
void addBlockToSequence(String block) {
blockSequence += block + "\n";
displayBlocks();
}
void displayBlocks() {
tft.fillRect(160, 0, 160, 240, ILI9341_BLACK);
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(160, 10);
tft.print("Selected:");
tft.setTextSize(1);
int cursorY = 30;
String line = "";
for (int i = 0; i < blockSequence.length(); i++) {
char c = blockSequence[i];
if (c == '\n') {
drawBlock(line, cursorY);
cursorY += 40;
line = "";
} else {
line += c;
}
}
}
void drawBlock(String block, int y) {
tft.drawRect(160, y, 150, 30, ILI9341_WHITE);
tft.setCursor(165, y + 5);
tft.print(block);
}
void blinkLED() {
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
}
void executeSequence() {
tft.fillScreen(ILI9341_BLACK);
tft.setTextSize(2);
tft.setTextColor(ILI9341_GREEN);
tft.setCursor(10, 10);
tft.print("Executing...");
String line = "";
for (int i = 0; i < blockSequence.length(); i++) {
char c = blockSequence[i];
if (c == '\n') {
executeBlock(line);
line = "";
} else {
line += c;
}
}
}
void executeBlock(String block) {
if (block == "Forever Loop") {
for (int i = 0; i < 5; i++) {
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
delay(1000);
}
} else if (block == "LED On") {
digitalWrite(LED_PIN, HIGH);
} else if (block == "LED Off") {
digitalWrite(LED_PIN, LOW);
} else if (block.startsWith("Delay")) {
int value = block.substring(6).toInt();
delay(value);
}
}
void displayLEDSwitchMenu() {
tft.fillRect(0, 0, 160, 240, ILI9341_BLACK);
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(10, 10);
tft.print("LED On/Off:");
tft.setCursor(10, 50);
tft.print("A. On");
tft.setCursor(10, 90);
tft.print("B. Off");
// Wait for input
char key = keypad.waitForKey();
if (key == 'A') {
selectedAction = ACTION_LED_ON;
} else if (key == 'B') {
selectedAction = ACTION_LED_OFF;
}
// Return to main menu
displayMenu();
}
void displayDelayInput() {
tft.fillRect(0, 0, 160, 240, ILI9341_BLACK);
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(10, 10);
tft.print("Enter Delay:");
tft.setCursor(10, 50);
tft.print(delayValue);
}