Mega, TFT, RTC, and WS2815 query

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);
}

heres a pic of display

SZZV9597[1].jpg

#include <TouchScreen.h>
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <Wire.h>
#include <RTClib.h>
#include <FastLED.h>
#include <time.h>
#include <FastLED.h>

RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {" Sunday", " Monday", "Tuesday", "Wednesday", "Thursday", " Friday", "Saturday"};

#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 ON_DELAY 25 
#define OFF_DELAY 50
#define STAY_DELAY 2000

const int Sensor = 40;
int SensorState = 0; 
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, exit_btn, yellow_btn, auto_btn, navy_btn, orange_btn, cyan_btn, grey_btn, purple_btn;

int pixel_x, pixel_y;
bool Touch_getXY(void)
{
  TSPoint p = ts.getPoint();
  pinMode(YP, OUTPUT);
  pinMode(XM, OUTPUT);
  digitalWrite(YP, HIGH);
  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 GREY            0x7BEF
#define BLUE            0x001F
#define GREEN           0x07E0 
#define CYAN            0x07FF
#define RED             0xF800
#define YELLOW          0xFFE0
#define WHITE           0xFFFF 
#define ORANGE          0xFD20 

#define BLACK1           0x0000 
#define NAVY1            0x000F 
#define PURPLE1          0x780F
#define GREY1            0x7BEF
#define BLUE1            0x001F 
#define GREEN1           0x07E0
#define CYAN1            0x07FF
#define RED1             0xF800
#define YELLOW1          0xFFE0
#define WHITE1           0xFFFF
#define ORANGE1          0xFD20

int COL = WHITE1;

void setup(void)
{
  Serial.begin(9600);
  Wire.begin();

  FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);

  pinMode(Sensor, INPUT);

  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; // write-only shield
  tft.begin(ID);
  tft.setRotation(0);
  tft.fillScreen(BLACK);


  auto_btn.initButton(&tft, 160, 265, 300, 50, WHITE, GREEN, BLACK, "AUTO", 5);
  exit_btn.initButton(&tft, 80 , 325, 140, 50, WHITE, GREEN, RED, "EXIT", 3);
  off_btn.initButton(&tft, 240, 325, 140, 50, WHITE, RED, GREEN, "OFF", 3);

  white_btn.initButton(&tft,  30, 380, 55, 42, WHITE, WHITE, BLACK, "", 2);
  blue_btn.initButton(&tft,  95, 380, 55, 42, WHITE, BLUE, WHITE, "", 2);
  red_btn.initButton(&tft, 160, 380, 55, 42, WHITE, RED, WHITE, "", 2);
  green_btn.initButton(&tft, 225 , 380, 55, 42, WHITE, GREEN, WHITE, "", 2);
  yellow_btn.initButton(&tft, 290, 380, 55, 42, WHITE, YELLOW, WHITE, "", 2); 

  purple_btn.initButton(&tft,  30, 430, 55, 42, WHITE, PURPLE, BLACK, "", 2);
  orange_btn.initButton(&tft,  95, 430, 55, 42, WHITE, ORANGE, WHITE, "", 2);
  cyan_btn.initButton(&tft, 160, 430, 55, 42, WHITE, CYAN, WHITE, "", 2);
  grey_btn.initButton(&tft, 225 , 430, 55, 42, WHITE, GREY, WHITE, "", 2);
  navy_btn.initButton(&tft, 290, 430, 55, 42, WHITE, NAVY, WHITE, "", 2);

  auto_btn.drawButton(false);
  exit_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);

  purple_btn.drawButton(false);
  orange_btn.drawButton(false);
  cyan_btn.drawButton(false);
  grey_btn.drawButton(false);
  navy_btn.drawButton(false);

  tft.fillRect(0, 460, 350, 20, BLACK);


}
void loop() {

  DateTime now = rtc.now();
  SensorState = digitalRead(Sensor);


  


  //--- BUTTON INFO  

  bool down = Touch_getXY();

  auto_btn.press(down && auto_btn.contains(pixel_x, pixel_y));
  exit_btn.press(down && exit_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));

  purple_btn.press(down && purple_btn.contains(pixel_x, pixel_y));
  orange_btn.press(down && orange_btn.contains(pixel_x, pixel_y));
  cyan_btn.press(down && cyan_btn.contains(pixel_x, pixel_y));
  grey_btn.press(down && grey_btn.contains(pixel_x, pixel_y));
  navy_btn.press(down && navy_btn.contains(pixel_x, pixel_y));

  if (auto_btn.justReleased())
    auto_btn.drawButton();
  if (exit_btn.justReleased())
    exit_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 (purple_btn.justReleased())
    purple_btn.drawButton();
  if (orange_btn.justReleased())
    orange_btn.drawButton();
  if (cyan_btn.justReleased())
    cyan_btn.drawButton();
  if (grey_btn.justReleased())
    grey_btn.drawButton();
  if (navy_btn.justReleased())
    navy_btn.drawButton();


  if (exit_btn.justPressed()) {
    exit_btn.drawButton(true);
    ledexit();

  }

  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();
    }
    Serial.println("LED's Turned off");
    COL = BLACK1;
  }

  if (white_btn.justPressed()) {
    white_btn.drawButton(true);
    tft.fillRect(0, 460, 350, 20, WHITE);
    COL = WHITE1;

  }

  if (blue_btn.justPressed()) {
    blue_btn.drawButton(true);
    tft.fillRect(0, 460, 350, 20, BLUE);
    COL = BLUE1;

  }

  if (red_btn.justPressed()) {
    red_btn.drawButton(true);
    tft.fillRect(0, 460, 350, 20, RED);
    COL = RED1;

  }

  if (green_btn.justPressed()) {
    green_btn.drawButton(true);
    tft.fillRect(0, 460, 350, 20, GREEN);
    COL = GREEN1;

  }

  if (yellow_btn.justPressed()) {
    yellow_btn.drawButton(true);
    tft.fillRect(0, 460, 350, 20, YELLOW);
    COL = YELLOW1;

  }

  if (purple_btn.justPressed()) {
    purple_btn.drawButton(true);
    tft.fillRect(0, 460, 350, 20, PURPLE);
    COL = PURPLE1;

  }

  if (orange_btn.justPressed()) {
    orange_btn.drawButton(true);
    tft.fillRect(0, 460, 350, 20, ORANGE);
    COL  = ORANGE1;

  }

  if (cyan_btn.justPressed()) {
    cyan_btn.drawButton(true);
    tft.fillRect(0, 460, 350, 20, CYAN);
    COL = CYAN1;

  }

  if (grey_btn.justPressed()) {
    grey_btn.drawButton(true);
    tft.fillRect(0, 460, 350, 20, GREY);
    COL = GREY1;

  }


  if (navy_btn.justPressed()) {
    navy_btn.drawButton(true);
    tft.fillRect(0, 460, 350, 20, NAVY);
    COL = NAVY1;

  }

  if (SensorState == HIGH) {
    ledin();
  }
}

void ledexit() {
   Serial.print(COL);
  for (int i = 0; i <= NUM_LEDS - 1; i++) {
    leds[i] = CRGB ("COL");
    FastLED.show();
    delay (ON_DELAY); 
  }

  delay (STAY_DELAY);


  for (int i = 0; i <= NUM_LEDS - 1; i++) {
    leds[i] = CRGB::Black;
    FastLED.show();
    delay (OFF_DELAY);   
  }
}

void ledin() {

  for (int i = NUM_LEDS; i >= 0; i--) {
    leds[i] = CRGB("COL");
    FastLED.show();
    delay (ON_DELAY);  
  }

  delay (STAY_DELAY);    Serial.println("Going OFF");
  for (int i = NUM_LEDS; i >= 0; i--) {
    leds[i] = CRGB::Black;
    FastLED.show();
    delay (OFF_DELAY);         
  }
}

Bit more on my problem.

i want to be able to call up a standard strip of ws2815 LED's, on in sequence (either way from end to end) with a small delay between each on coming on, one for about 7-10 seconds, then off in the same direction at a different speed.

BUT, AND THIS IS WHERE I AM STRUGGLING, the colour needs to preset and stored somehow, ready for when its triggered (by the beam sensor) then they come on and then go off. But it remembers the colours for next time..

and then i press the touch screen for a different colour it will use that colour next next (this also puts a small box on the very bottom of the display to visually show me what colour is selected...

Full code is in previous post.. (deleted the part for diaplay date and time on TFT so code would be small enough to post in full

Trouble lies in storing colour info in "COL"

updated pic of display

IMG_0103[2].jpg

617451140be6897c0d483ca39d7fb6b12ae8bd81.jpg

0ca309f862ebd69f36203510e9c0b455c5e153fe.jpg

can anyone please help in storing the colour info so it can be called up.

something to replace "::Black;" with a variable

leds = CRGB::Black;

  • FastLED.show();*

OK, spent over a week on this problem. but i will keep trying things, even if no one helps me :o(