Hi
Although i have been messing around with Arduinos for a while, i still have lots to learn, but as only a small hobby its hard to get time!
anyway, code is below, what i want, is a touch tft with controls over a 5 metre strip of leds which are outside up the length of our drive, which are triggered automatically when some breaks a IR beam (already in place for my old system).
this then triggers the leds to come on in sequence from sensor end up to the door (leading the way kind of) staying on for 10 seconds and then turning off from sensor to door..
but to complicate things again, i want to have control on the colour from inside. and also have a "temp" options to put the lights via toggle button on tft. And also an "exit" option which puts the lights on from door to sensor etc...
this is where im struggling. witht he delays and colour options to be stored and then called when needed...
the code has been played with over the last few days and isnt the prettiest im sure..
#include <TouchScreen.h>
#include <Adafruit_GFX.h> // Core graphic library
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <Wire.h>
#include <RTClib.h> // Library for RTC Module
#include <FastLED.h>
#include <time.h>
#include <FastLED.h>
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sun ", "Mon ", "Tue ", "Wed ", "Thur ", "Fri ", "Sat "};
#define MINPRESSURE 200
#define MAXPRESSURE 1000
#define DATA_PIN 30
#define NUM_LEDS 25
#define COLOR_ORDER GRB
#define CHIPSET WS2812B
#define BRIGHTNESS 50
#define FRAMES_PER_SECOND 120
#define DELAY 100
int COL;
CRGB leds[NUM_LEDS];
const int XP = 8, XM = A2, YP = A3, YM = 9; //320x480 ID=0x9486
const int TS_LEFT = 136, TS_RT = 913, TS_TOP = 952, TS_BOT = 94;
time_t syncProvider() //
{
return rtc.now().unixtime();
}
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_GFX_Button blue_btn, red_btn, off_btn, white_btn, green_btn, on_btn, yellow_btn, auto_btn;
int pixel_x, pixel_y; //Touch_getXY() updates global vars
bool Touch_getXY(void)
{
TSPoint p = ts.getPoint();
pinMode(YP, OUTPUT); //restore shared pins
pinMode(XM, OUTPUT);
digitalWrite(YP, HIGH); //because TFT control pins
digitalWrite(XM, HIGH);
bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
if (pressed) {
pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
}
return pressed;
}
#define BLACK 0x0000
#define NAVY 0x000F
#define PURPLE 0x780F
#define OLIVE 0x7BE0
#define GREY 0x7BEF/
#define BLUE 0x001F
#define GREEN 0x07E0
#define CYAN 0x07FF
#define RED 0xF800
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define ORANGE 0xFD20
void setup(void)
{
// Serial.begin(9600);
Wire.begin();
FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
uint16_t ID = tft.readID();
Serial.print("TFT ID = 0x");
Serial.println(ID, HEX);
Serial.println("Calibrate for your Touch Panel");
if (ID == 0xD3D3) ID = 0x9486;
tft.begin(ID);
tft.setRotation(0); //PORTRAIT
tft.fillScreen(BLACK); //CLEAR SCREEN
//----------------- BUTTON INFO -----------------
auto_btn.initButton(&tft, 170, 230, 200, 75, WHITE, GREEN, BLACK, "AUTO!", 5); //AUTO MODE
on_btn.initButton(&tft, 80 , 320, 140, 60, WHITE, GREEN, RED, "EXIT", 3); //TEMP MODE
off_btn.initButton(&tft, 240, 320, 140, 60, WHITE, RED, GREEN, "OFF", 3); //ALL OFF
white_btn.initButton(&tft, 30, 420, 60, 42, WHITE, WHITE, BLACK, "", 2); // WHITE
blue_btn.initButton(&tft, 95, 420, 60, 42, WHITE, BLUE, WHITE, "", 2); // BLUE
red_btn.initButton(&tft, 160, 420, 60, 42, WHITE, RED, WHITE, "", 2); // RED
green_btn.initButton(&tft, 225 , 420, 60, 42, WHITE, GREEN, WHITE, "", 2); // GREEN
yellow_btn.initButton(&tft, 290, 420, 60, 42, WHITE, YELLOW, WHITE, "", 2); // YELLOW
auto_btn.drawButton(false);
on_btn.drawButton(false);
off_btn.drawButton(false);
white_btn.drawButton(false);
blue_btn.drawButton(false);
red_btn.drawButton(false);
green_btn.drawButton(false);
yellow_btn.drawButton(false);
tft.fillRect(0, 460, 350, 20, BLACK);
}
void loop(void) {
DateTime now = rtc.now();
// unsigned int COL;
bool down = Touch_getXY();
auto_btn.press(down && auto_btn.contains(pixel_x, pixel_y));
on_btn.press(down && on_btn.contains(pixel_x, pixel_y));
off_btn.press(down && off_btn.contains(pixel_x, pixel_y));
white_btn.press(down && white_btn.contains(pixel_x, pixel_y));
blue_btn.press(down && blue_btn.contains(pixel_x, pixel_y));
red_btn.press(down && red_btn.contains(pixel_x, pixel_y));
green_btn.press(down && green_btn.contains(pixel_x, pixel_y));
yellow_btn.press(down && yellow_btn.contains(pixel_x, pixel_y));
if (auto_btn.justReleased())
auto_btn.drawButton();
if (on_btn.justReleased())
on_btn.drawButton();
if (off_btn.justReleased())
off_btn.drawButton();
if (white_btn.justReleased())
white_btn.drawButton();
if (blue_btn.justReleased())
blue_btn.drawButton();
if (red_btn.justReleased())
red_btn.drawButton();
if (green_btn.justReleased())
green_btn.drawButton();
if (yellow_btn.justReleased())
yellow_btn.drawButton();
if (on_btn.justPressed()) {
on_btn.drawButton(true);
COL = 1;
}
if (off_btn.justPressed()) {
off_btn.drawButton(true);
tft.fillRect(0, 460, 350, 20, BLACK);
for (int i = 0; i <= NUM_LEDS - 1; i++) {
leds[i] = CRGB::Black;
FastLED.show();
}
COL = 99;
}
if (white_btn.justPressed()) {
white_btn.drawButton(true);
tft.fillRect(0, 460, 350, 20, WHITE);
for (int i = 0; i <= NUM_LEDS - 1; i++) {
leds[i] = CRGB::White;
}
COL = 3;
}
if (blue_btn.justPressed()) {
blue_btn.drawButton(true);
tft.fillRect(0, 460, 350, 20, BLUE);
for (int i = 0; i <= NUM_LEDS - 1; i++) {
leds[i] = CRGB::Blue;
}
COL = 4;
}
if (red_btn.justPressed()) {
red_btn.drawButton(true);
tft.fillRect(0, 460, 350, 20, RED);
for (int i = 0; i <= NUM_LEDS - 1; i++) {
leds[i] = CRGB::Red;
}
COL = 5;
}
if (green_btn.justPressed()) {
green_btn.drawButton(true);
tft.fillRect(0, 460, 350, 20, GREEN);
for (int i = 0; i <= NUM_LEDS - 1; i++) {
leds[i] = CRGB::Green;
}
COL = 6;
}
if (yellow_btn.justPressed()) {
yellow_btn.drawButton(true);
tft.fillRect(0, 460, 350, 20, YELLOW);
for (int i = 0; i <= NUM_LEDS - 1; i++) {
leds[i] = CRGB::Yellow;
}
COL = 7;
}
if (COL == 1) {
// for (int i = 0; i <= NUM_LEDS - 1; i++){
// leds[i];
FastLED.show();
// delay(DELAY);
Serial.println("ON!");
}
if (COL == 99) {
FastLED.show();
Serial.println("OFF!");
}
// ----------TIME DISPLAY---------
tft.setCursor(4, 8); // SET POSITION READY FOR TIME
tft.setTextSize(5); // SET SIZE READY FOR TIME
if (now.hour() < 10) {
tft.setTextColor(WHITE, BLACK); // COLOUR FOR 0
tft.print('0'); // INSERT ZERO IF LESS THAT 10 ie "09" NOT JUST "9"
}
tft.setTextColor(WHITE, BLACK); // COLOUR FOR HOUR
tft.print(now.hour(), DEC); // DISPLAY HOUR
tft.setTextColor(GREEN, BLACK); // COLOUR FOR :
tft.print(":");
if (now.minute() < 10) {
tft.setTextColor(WHITE, BLACK); // COLOUR FOR 0
tft.print('0'); // INSERT ZERO IF LESS THAT 10 ie "09" NOT JUST "9"
}
tft.setTextColor(WHITE, BLACK); // COLOUR FOR HOUR
tft.print(now.minute(), DEC);
// ----------DATE DISPLAY---------
tft.setCursor(170, 8);
tft.setTextSize(5);
tft.setTextColor(GREEN, BLACK); //COLOUR FOR DAY OF WEEK
tft.print(daysOfTheWeek[now.dayOfTheWeek()]); // DISPLAY DAY OF WEEK
if (now.day() < 10) {
tft.setTextColor(RED, BLACK);
tft.print('0');
}
tft.setTextColor(RED, BLACK);
tft.print(now.day(), DEC); // DISPLAY DATE
tft.setTextColor(GREEN, BLACK);
tft.print("/");
if (now.month() < 10) {
tft.setTextColor(RED, BLACK);
tft.print('0');
}
tft.print(now.month(), DEC);
tft.setTextColor(GREEN, BLACK);
tft.print("/");
tft.setTextColor(RED, BLACK);
tft.print(now.year(), DEC);
}