0% found this document useful (0 votes)
44 views9 pages

Mega 1

This document defines constants and functions for displaying buttons, menus, and a numeric keypad on a touchscreen. It includes definitions for colors, button sizes and positions. Functions are provided to draw and update buttons, display main and mode menus, and draw the numeric keypad. Global variables track the current mode and item count. Pins are configured for sensors, motors and a buzzer. On startup, it displays a welcome message and main menu.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views9 pages

Mega 1

This document defines constants and functions for displaying buttons, menus, and a numeric keypad on a touchscreen. It includes definitions for colors, button sizes and positions. Functions are provided to draw and update buttons, display main and mode menus, and draw the numeric keypad. Global variables track the current mode and item count. Pins are configured for sensors, motors and a buzzer. On startup, it displays a welcome message and main menu.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

#include <Adafruit_GFX.

h> // Core graphics library


#include <TouchScreen.h>
#include "pitches.h"
#include <MCUFRIEND_kbv.h>

#define LS1 35
#define LS2 37
#define THERMOMETER 39
#define LDR1 41
#define LDR2 43
#define MOTOR_IN1 51
#define MOTOR_IN2 49
#define MOTOR_IN3 47
#define MOTOR_IN4 45
#define BUZZER 31

#define BLACK 0x0000


#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define DARK_BLUE 0x00008B // Dark blue color
#define DARK_RED 0x8B0000 // Dark red color
#define PRIMARY_COLOR 0x4A11
#define PRIMARY_LIGHT_COLOR 0x7A17
#define PRIMARY_DARK_COLOR 0x4016
#define PRIMARY_TEXT_COLOR 0x7FFF

bool showingKeypad = false; // Add this line at the top of your sketch

const char* keys[12] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "0",
"#"};

// Button dimensions and positions


int screen_width = 400;
int screen_height = 240;

// At the beginning of your sketch, where you declare global variables


int startYEdit = 20; // You can adjust this value as per your screen layout

int button2Width = 200; // Width for button2


int button2Height = 160; // Height for button2

int manualButtonWidth = 200; // Width for manual screen buttons


int manualButtonHeight = 40; // Shorter height for manual screen buttons

int buttonWidth = 200; // Smaller button width


int buttonHeight = 60; // Smaller button height
int screen_width_rotated = 240; // Width after rotation

// Center horizontally for rotated orientation


int startX = (screen_width_rotated - buttonWidth) / 2;

int startYAuto = 20; // Starting Y position for Auto Mode button


int startYManual = startYAuto + buttonHeight + 20; // Below Auto Mode button
int startYReturn = startYManual + buttonHeight + 20; // Adjust as necessary
// Text positioning for buttons (adjust as needed)
int textPosXAuto = startX + (buttonWidth - 9 * 6) / 2; // Center for "Auto Mode"
int textPosYAuto = startYAuto + (buttonHeight - 16) / 2; // Center vertically in
button
int textPosXManual = startX + (buttonWidth - 12 * 6) / 2; // Center for "Manual
Mode"
int textPosYManual = startYManual + (buttonHeight - 16) / 2;

const int keyWidth = 60;


const int keyHeight = 40;
const int keysInRow = 3;
const int keyPadding = 10;
int keypadStartX = (screen_width_rotated - (keysInRow * keyWidth + (keysInRow - 1)
* keyPadding)) / 2;
int keypadStartY = 100; // Adjust this value as needed

MCUFRIEND_kbv tft;

#define LOWFLASH (defined(__AVR_ATmega328P__) && defined(MCUFRIEND_KBV_H_))

// Touch screen pressure threshold


#define MINPRESSURE 40
#define MAXPRESSURE 1000
// Touch screen calibration
const int16_t XP = 8, XM = 56, YP = 57, YM = 9; //400x240
const int16_t TS_LEFT = 197, TS_RT = 854, TS_TOP = 904, TS_BOT = 101;

const TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

// Global variables to keep track of the current mode


bool inAutoMode = false;
bool inManualMode = false;
int itemCount = 0; // For manual mode item count

void updateButton(int x, int y, const char* label, uint16_t bgColor, uint16_t


textColor, int btnWidth, int btnHeight) {
tft.fillRect(x, y, btnWidth, btnHeight, bgColor);
tft.setTextColor(textColor);

// Center the text horizontally and vertically


int textX = x + (btnWidth - strlen(label) * 6) / 2; // Assuming character width
of 6 pixels
int textY = y + (btnHeight - 8) / 2; // Assuming character height of 8 pixels

tft.setCursor(textX, textY);
tft.println(label);
delay(100); // Delay for button press effect
}

void drawKey(int x, int y, const char* label, uint16_t bgColor, uint16_t textColor)
{
tft.fillRect(x, y, keyWidth, keyHeight, bgColor);
tft.setTextColor(textColor);
tft.setTextSize(2); // Adjust text size as needed
tft.setCursor(x + (keyWidth - strlen(label) * 6) / 2, y + (keyHeight - 8) / 2);
// Adjust for text size
tft.println(label);
}

void updateItemCountDisplay() {
int startYNumber = 20; // Adjust as needed
tft.fillRect(startX, startYNumber, buttonWidth, buttonHeight, BLACK);
tft.drawRect(startX, startYNumber, buttonWidth, buttonHeight, WHITE);
tft.setCursor(startX + (buttonWidth - 4 * 6) / 2, startYNumber + (buttonHeight
- 16) / 2);
tft.setTextSize(2);
tft.setTextColor(WHITE);
char numStr[5];
sprintf(numStr, "%04d", itemCount);
tft.println(numStr);
}

void drawKeypad() {
tft.fillScreen(BLACK);
tft.setTextColor(WHITE);
tft.setTextSize(2);

const char *keys[12] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "0",
"#"};

for (int row = 0; row < 4; ++row) {


for (int col = 0; col < 3; ++col) {
int x = keypadStartX + col * (keyWidth + keyPadding);
int y = keypadStartY + row * (keyHeight + keyPadding);
tft.drawRect(x, y, keyWidth, keyHeight, WHITE);
tft.setCursor(x + keyWidth / 2 - 6, y + keyHeight / 2 - 8);
tft.println(keys[row * 3 + col]);
}
}
}

void enterEditMode() {
itemCount = 0; // Reset the number to zero
showingKeypad = true;
displayNumericKeypad();
}

void setup() {
uint16_t ID = tft.readID();
tft.begin(ID);
tft.setRotation(2); // Adjust as needed

// Display welcome message


tft.fillScreen(BLACK);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.setCursor(120, 100);
tft.println("Welcome!");
delay(2000);

// Display main menu


displayMainMenu();

// Set up the limit switches as inputs


pinMode(LS1, INPUT);
pinMode(LS2, INPUT);

// Set up the thermometer module


// Assuming it's analog, if it's digital, the setup might be different
pinMode(THERMOMETER, INPUT);

// Set up the LDR modules as inputs


pinMode(LDR1, INPUT);
pinMode(LDR2, INPUT);

// Set up the motor controller pins as outputs


pinMode(MOTOR_IN1, OUTPUT);
pinMode(MOTOR_IN2, OUTPUT);
pinMode(MOTOR_IN3, OUTPUT);
pinMode(MOTOR_IN4, OUTPUT);

// Set up the buzzer as an output


pinMode(BUZZER, OUTPUT);
}

void displayMainMenu() {
tft.fillScreen(BLACK);
tft.setTextColor(WHITE);
tft.setTextSize(2);

// Auto Mode Button (using button2 dimensions)


tft.drawRect(startX, startYAuto, button2Width, button2Height, WHITE);
tft.setCursor(startX + (button2Width - 9 * 6) / 2, startYAuto + (button2Height
- 16) / 2);
tft.println("Auto");

// Manual Mode Button (using button2 dimensions)


tft.drawRect(startX, startYAuto + button2Height + 20, button2Width,
button2Height, WHITE);
tft.setCursor(startX + (button2Width - 12 * 6) / 2, startYAuto + button2Height
+ 20 + (button2Height - 16) / 2);
tft.println("Manual");
}

void displayAutoMode() {
tft.fillScreen(BLACK);
tft.setTextColor(WHITE);
tft.setTextSize(2);

int gap = 20; // Gap between buttons


int startYStart = screen_height / 6; // Starting Y position for the Start
button

// Start Button
tft.drawRect(startX, startYStart, buttonWidth, buttonHeight, WHITE);
tft.setCursor(startX + (buttonWidth - 6 * 5) / 2, startYStart + (buttonHeight -
16) / 2);
tft.println("Start");

// Stop Button
int startYStop = startYStart + buttonHeight + gap;
tft.drawRect(startX, startYStop, buttonWidth, buttonHeight, WHITE);
tft.setCursor(startX + (buttonWidth - 6 * 4) / 2, startYStop + (buttonHeight -
16) / 2);
tft.println("Stop");

// Return to Main Menu Button


int startYReturn = startYStop + buttonHeight + gap;
tft.drawRect(startX, startYReturn, buttonWidth, buttonHeight, WHITE);
tft.setCursor(startX + (buttonWidth - 6 * 4) / 2, startYReturn + (buttonHeight
- 16) / 2);
tft.println("Back");
}

bool isPaused = false; // Global variable to track pause state

void displayManualMode() {
tft.fillScreen(BLACK);
tft.setTextColor(WHITE);
tft.setTextSize(2);

int gap = 10; // Gap between buttons

// 0000 Indicator
int startYIndicator = 20;
tft.drawRect(startX, startYIndicator, manualButtonWidth, manualButtonHeight,
WHITE);
updateItemCountDisplay(); // Update the 0000 display

// Edit Button
int startYEdit = startYIndicator + manualButtonHeight + 10;
tft.drawRect(startX, startYEdit, manualButtonWidth, manualButtonHeight, WHITE);
tft.setCursor(startX + (manualButtonWidth - 6 * 4) / 2, startYEdit +
(manualButtonHeight - 16) / 2);
tft.println("Edit");

// Start Button
int startYStart = startYEdit + manualButtonHeight + 10;
tft.drawRect(startX, startYStart, manualButtonWidth, manualButtonHeight,
WHITE);
tft.setCursor(startX + (manualButtonWidth - 6 * 5) / 2, startYStart +
(manualButtonHeight - 16) / 2);
tft.println("Start");

// Pause/Continue Button
int startYPause = startYStart + manualButtonHeight + 10;
tft.drawRect(startX, startYPause, manualButtonWidth, manualButtonHeight,
WHITE);
tft.setCursor(startX + (manualButtonWidth - 6 * (isPaused ? 8 : 5)) / 2,
startYPause + (manualButtonHeight - 16) / 2);
tft.println(isPaused ? "Continue" : "Pause");

// Stop Button
int startYStop = startYPause + manualButtonHeight + 10;
tft.drawRect(startX, startYStop, manualButtonWidth, manualButtonHeight, WHITE);
tft.setCursor(startX + (manualButtonWidth - 6 * 4) / 2, startYStop +
(manualButtonHeight - 16) / 2);
tft.println("Stop");
// Return to Main Menu Button
int startYReturn = startYStop + manualButtonHeight + 10;
tft.drawRect(startX, startYReturn, manualButtonWidth, manualButtonHeight,
WHITE);
tft.setCursor(startX + (manualButtonWidth - 6 * 4) / 2, startYReturn +
(manualButtonHeight - 16) / 2);
tft.println("Back");
}

void displayNumericKeypad() {
tft.fillScreen(BLACK); // Clear the screen
tft.setTextColor(WHITE);
tft.setTextSize(2);

const char *keys[12] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "0",
"#"};

for (int row = 0; row < 4; ++row) {


for (int col = 0; col < 3; ++col) {
int x = keypadStartX + col * (keyWidth + keyPadding);
int y = keypadStartY + row * (keyHeight + keyPadding);
tft.drawRect(x, y, keyWidth, keyHeight, WHITE);
tft.setCursor(x + (keyWidth - 6) / 2, y + (keyHeight - 8) / 2);
tft.println(keys[row * 3 + col]);
}
}

// Draw the OK button


int startYOK = keypadStartY + 4 * (keyHeight + keyPadding); // Below the keypad
tft.drawRect(startX, startYOK, buttonWidth, buttonHeight, WHITE);
tft.setCursor(startX + (buttonWidth - 6 * 2) / 2, startYOK + (buttonHeight -
16) / 2);
tft.println("OK");
}

void loop() {
digitalWrite(YP, HIGH); // disable Pull-up
digitalWrite(XM, HIGH); // disable Pull-up
TSPoint p = ts.getPoint();
digitalWrite(YP, LOW); // enable Pull-down
digitalWrite(XM, LOW); // enable Pull-down

pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);

if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {


p.x = map(p.x, TS_LEFT, TS_RT, 0, tft.width());
p.y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());

if (!inAutoMode && !inManualMode) {


// Auto Mode Button
if (!inAutoMode && !inManualMode && p.x > startX && p.x < startX +
button2Width && p.y > startYAuto && p.y < startYAuto + button2Height) {
updateButton(startX, startYAuto, "Auto", YELLOW, BLACK, button2Width,
button2Height); // Yellow background, black text
delay(100); // Short delay to show button press
updateButton(startX, startYAuto, "Auto", WHITE, BLACK, button2Width,
button2Height); // Reset to original colors
inAutoMode = true;
displayAutoMode();
}
// Manual Mode Button
else if (!inAutoMode && !inManualMode && p.x > startX && p.x < startX +
button2Width && p.y > startYAuto + button2Height + 20 && p.y < startYAuto + 2 *
button2Height + 20) {
updateButton(startX, startYAuto + button2Height + 20, "Manual", YELLOW,
BLACK, button2Width, button2Height); // Yellow background, black text
delay(100); // Short delay to show button press
updateButton(startX, startYAuto + button2Height + 20, "Manual", WHITE,
BLACK, button2Width, button2Height); // Reset to original colors
inManualMode = true;
displayManualMode();
}

}
else if (inAutoMode) {
// Start Button
int startYStart = screen_height / 6;
if (p.x > startX && p.x < startX + buttonWidth && p.y > startYStart &&
p.y < startYStart + buttonHeight) {
updateButton(startX, startYStart, "Start", WHITE, BLACK, buttonWidth,
buttonHeight);
delay(100);
displayAutoMode();
}
// Stop Button
int startYStop = startYStart + buttonHeight + 20;
if (p.x > startX && p.x < startX + buttonWidth && p.y > startYStop &&
p.y < startYStop + buttonHeight) {
updateButton(startX, startYStop, "Stop", WHITE, BLACK, buttonWidth,
buttonHeight);
delay(100);
displayAutoMode();
}
// Return to Main Menu Button
int startYReturn = startYStop + buttonHeight + 20;
if (p.x > startX && p.x < startX + buttonWidth && p.y > startYReturn &&
p.y < startYReturn + buttonHeight) {
updateButton(startX, startYReturn, "Back", WHITE, BLACK, buttonWidth,
buttonHeight);
displayMainMenu();
inAutoMode = false;
}
}
else if (inManualMode) {

static int lastPressedKeyX = -1;


static int lastPressedKeyY = -1;

if (showingKeypad) {
for (int row = 0; row < 4; ++row) {
for (int col = 0; col < 3; ++col) {
int x = keypadStartX + col * (keyWidth + keyPadding);
int y = keypadStartY + row * (keyHeight + keyPadding);
if (p.x >= x && p.x < x + keyWidth && p.y >= y && p.y < y +
keyHeight) {
drawKey(x, y, keys[row * 3 + col], GREEN, BLACK); // Key press
indication

lastPressedKeyX = x;
lastPressedKeyY = y;

int numKey = row * 3 + col + 1;


if (numKey <= 9) { // Keys 1-9
itemCount = (itemCount % 1000) * 10 + numKey; // Shift left and add
new digit
} else if (numKey == 10) { // Key 0
itemCount = (itemCount % 1000) * 10;
} else if (numKey == 11) { // Clear or other function
itemCount = 0;
}
updateItemCountDisplay();
delay(100); // Debounce delay
// Revert the key color back to original
drawKey(x, y, keys[row * 3 + col], WHITE, BLACK);
}
}
}

// OK Button
int startYOK = keypadStartY + 4 * (keyHeight + keyPadding);
if (p.x > startX && p.x < startX + buttonWidth && p.y > startYOK &&
p.y < startYOK + manualButtonHeight) {
updateButton(startX, startYOK, "OK", WHITE, BLACK, buttonWidth,
buttonHeight);
delay(100); // Delay for button press effect
showingKeypad = false;
displayManualMode();
}
} else {
// Edit Button
int startYEdit = 20 + manualButtonHeight + 10;
if (p.x > startX && p.x < startX + manualButtonWidth && p.y >
startYEdit && p.y < startYEdit + manualButtonHeight) {
updateButton(startX, startYEdit, "Edit", WHITE, BLACK,
manualButtonWidth, manualButtonHeight);
delay(100); // Delay for button press effect
enterEditMode(); // Enter edit mode and reset number
}

// Start Button
int startYStart = startYEdit + manualButtonHeight + 10;
if (p.x > startX && p.x < startX + manualButtonWidth && p.y >
startYStart && p.y < startYStart + manualButtonHeight) {
updateButton(startX, startYStart, "Start", WHITE, BLACK,
manualButtonWidth, manualButtonHeight);
delay(100); // Delay for button press effect
displayManualMode(); // Redraw the screen to update the button
label
}

// Pause/Continue Button
int startYPause = startYStart + manualButtonHeight + 10;
if (p.x > startX && p.x < startX + manualButtonWidth && p.y >
startYPause && p.y < startYPause + manualButtonHeight) {
isPaused = !isPaused; // Toggle the pause state
updateButton(startX, startYPause, isPaused ? "Continue" :
"Pause", WHITE, BLACK, manualButtonWidth, manualButtonHeight);
delay(100); // Delay for button press effect
displayManualMode(); // Redraw the screen to update the button
label
}

// Stop Button
int startYStop = startYPause + manualButtonHeight + 10;
if (p.x > startX && p.x < startX + manualButtonWidth && p.y >
startYStop && p.y < startYStop + manualButtonHeight) {
updateButton(startX, startYStop, "Stop", WHITE, BLACK,
manualButtonWidth, manualButtonHeight);
delay(100); // Delay for button press effect
displayManualMode(); // Redraw the screen to update the button
label
}

// Return to Main Menu Button


int startYReturn = startYStop + manualButtonHeight + 10;
if (p.x > startX && p.x < startX + manualButtonWidth && p.y >
startYReturn && p.y < startYReturn + manualButtonHeight) {
updateButton(startX, startYReturn, "Back", WHITE, BLACK,
manualButtonWidth, manualButtonHeight);
delay(100); // Delay for button press effect
inManualMode = false;
showingKeypad = false;
displayMainMenu();
}
}
}
}
}

You might also like