Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Libraries
- #include <SoftwareSerial.h>
- #include <Adafruit_GFX.h> // Core graphics library
- #include <Adafruit_TFTLCD.h> // LCD library
- #include <TouchScreen.h> // Touchscreen Library
- #include <MCUFRIEND_kbv.h> // Touchscreen Hardware-specific library
- #include <DS3231.h> // Real time clock library
- #include <DFRobotDFPlayerMini.h> // MP3 library
- SoftwareSerial mySoftwareSerial(47, 45); // RX, TX Define so the MP3 can write back to the computer
- DFRobotDFPlayerMini myDFPlayer;
- // Define pins
- //LED
- const byte pwmLED = 46;
- // define directions for LED fade
- #define UP 0
- #define DOWN 1
- //for LCD
- #define LCD_CS A3 // Chip Select goes to Analog 3
- #define LCD_CD A2 // Command/Data goes to Analog 2
- #define LCD_WR A1 // LCD Write goes to Analog 1
- #define LCD_RD A0 // LCD Read goes to Analog 0
- #define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
- //Define Mp3
- # define startByte 0x7E
- # define endByte 0xEF
- # define versionByte 0xFF
- # define dataLength 0x06
- # define infoReq 0x01
- # define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]
- //# define ACTIVATED LOW
- // Assign human-readable names to some common 16-bit color values:
- #define BLACK 0x0000
- #define BLUE 0x001F
- #define RED 0xF800
- #define GREEN 0x07E0
- #define CYAN 0x07FF
- #define MAGENTA 0xF81F
- #define YELLOW 0xFFE0
- #define WHITE 0xFFFF
- /******************* UI details */
- // Location
- #define BUTTON_X 52
- #define BUTTON_Y 150
- // Size
- #define BUTTON_W 80
- #define BUTTON_H 45
- #define BUTTON_SPACING_X 26
- #define BUTTON_SPACING_Y 30
- #define BUTTON_TEXTSIZE 2
- // Define pins for resistive touchscreen
- #define YP A2 // must be an analog pin, use "An" notation!
- #define XM A3 // must be an analog pin, use "An" notation!
- #define YM 8 // can be a digital pin
- #define XP 9 // can be a digital pin
- // Define touchscreen pressure points
- #define MINPRESSURE 10
- #define MAXPRESSURE 1000
- // Define touchscreen parameters
- // Use test sketch to refine if necessary
- #define TS_MINX 120
- #define TS_MAXX 847
- #define TS_MINY 75
- #define TS_MAXY 930
- #define STATUS_X 10
- #define STATUS_Y 65
- //Define LED outputs
- #define RED_LED 35
- #define GRN_LED 33
- #define BLU_LED 31
- //Define Alarm
- DS3231 rtc(SDA, SCL); //Define RTC
- int fadeTime = 30;
- int setHour = 21;
- int setMin = 15;
- int alarmTime = 10000;
- //LED
- // constants for min and max PWM
- const int minPWM = 0;
- const int maxPWM = 255;
- // State Variable for Fade Direction
- byte fadeDirection = UP;
- // Global Fade Value
- // but be bigger than byte and signed, for rollover
- int fadeValue = 0;
- // How smooth to fade?
- byte fadeIncrement = 5;
- // millis() timing Variable, just for fading
- unsigned long previousFadeMillis;
- unsigned long buttonPushedMillis;
- // How fast to increment?
- int fadeInterval = 50;
- //TOUCH SCREEN
- //Define & buttons state variables
- boolean isPlaying = false;
- boolean PREV_pressed = false;
- boolean PAUSE_pressed = false;
- boolean NEXT_pressed = false;
- boolean RED_pressed = false;
- boolean GRN_pressed = false;
- boolean BLU_pressed = false;
- boolean Check_pressed = false;
- boolean Stop_pressed = false;
- // Define button array object
- Adafruit_GFX_Button buttons[9];
- // Define arrays with button text and colors
- char const * const buttonlabels[]
- = {"Prev", "Pause", "Next", "R", "G", "B", "Check", "Stop", "Light" };
- uint16_t buttoncolors[9] = {RED, GREEN, BLUE, RED, GREEN, BLUE, CYAN, MAGENTA, YELLOW,};
- // Define object for TFT (LCD)display
- MCUFRIEND_kbv tft;
- // Define object for touchscreen
- // Last parameter is X-Y resistance, measure or use 300 if unsure
- TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
- void setup() {
- // LED
- // put pwmLED into known state (off)
- analogWrite(pwmLED, fadeValue);
- //MP3
- mySoftwareSerial.begin(9600);
- Serial.begin(9600);
- // Serial.begin(115200);
- Serial.println();
- Serial.println(F("DFRobot DFPlayer Mini Demo"));
- Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
- //execute_CMD(0x06, 0, 1); // Set volume
- //execute_CMD(0x07, 0, 1); // Set the equalizer (00= normal, 01=pop, 02=rock, 03=jazz, 04=classic 05=bass)
- //execute_CMD(0x0E, 0, 0);
- if (!myDFPlayer.begin(mySoftwareSerial, false)) { //Use softwareSerial to communicate with mp3.
- Serial.println(F("Unable to begin:"));
- Serial.println(F("1.Please recheck the connection!"));
- Serial.println(F("2.Please insert the SD card!"));
- while (true);
- }
- Serial.println(F("DFPlayer Mini online."));
- myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
- //----Set volume----
- myDFPlayer.volume(2); //Set volume value (0~30).
- delay(100);
- //myDFPlayer.volumeUp(); //Volume Up
- //myDFPlayer.volumeDown(); //Volume Down
- //----Set different EQ----
- myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
- // myDFPlayer.EQ(DFPLAYER_EQ_POP);
- // myDFPlayer.EQ(DFPLAYER_EQ_ROCK);
- // myDFPlayer.EQ(DFPLAYER_EQ_JAZZ);
- // myDFPlayer.EQ(DFPLAYER_EQ_CLASSIC);
- // myDFPlayer.EQ(DFPLAYER_EQ_BASS);
- //----Set device we use SD as default----
- myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
- //----Mp3 control----
- // myDFPlayer.sleep(); //sleep
- // myDFPlayer.reset(); //Reset the module
- // myDFPlayer.enableDAC(); //Enable On-chip DAC
- // myDFPlayer.disableDAC(); //Disable On-chip DAC
- // myDFPlayer.outputSetting(true, 15); //output setting, enable the output and set the gain to 15
- // Touch Screen text
- Serial.println(F("TFT LCD test")); //TFT recognision
- Serial.println("version 3"); // Version declaration
- // Declare LEDs
- pinMode(RED_LED, OUTPUT);
- pinMode(GRN_LED, OUTPUT);
- pinMode(BLU_LED, OUTPUT);
- // Real time clock
- rtc.begin();
- // rtc.setDOW(MONDAY); // Set Day-of-Week to SUNDAY
- // rtc.setTime(16, 48, 0); // Set the time to 12:00:00 (24hr format)
- // rtc.setDate(5, 4, 2021); // Set the date to January 1st, 2014
- //Touch screen
- tft.reset();
- uint16_t identifier = tft.readID();
- if (identifier == 0x9486)
- {
- Serial.println(F("Found 0x9486 LCD driver"));
- }
- // Setup the Display
- tft.begin(identifier);
- Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());
- tft.setRotation(0); // Rotation 0 = portrait 1 = landscape
- tft.fillScreen(BLACK);
- tft.print("Rozan's wake up light V.1");
- // Draw buttons
- for (uint8_t row = 0; row < 3; row++) {
- for (uint8_t col = 0; col < 3; col++) {
- buttons[col + row * 3].initButton(&tft, BUTTON_X + col * (BUTTON_W + BUTTON_SPACING_X),
- BUTTON_Y + row * (BUTTON_H + BUTTON_SPACING_Y), // x, y, w, h, outline, fill, text
- BUTTON_W, BUTTON_H, WHITE, buttoncolors[col + row * 3], WHITE,
- buttonlabels[col + row * 3], BUTTON_TEXTSIZE);
- buttons[col + row * 3].drawButton();
- }
- }
- }
- void loop() {
- // For fade alarm
- unsigned long currentMillis = millis();
- // playAlarm(currentMillis);
- // Define button state variables
- PREV_pressed = false;
- PAUSE_pressed = false;
- NEXT_pressed = false;
- RED_pressed = false;
- GRN_pressed = false;
- BLU_pressed = false;
- // Check_pressed = false;
- Stop_pressed = false;
- digitalWrite(13, HIGH);
- TSPoint p = ts.getPoint();
- digitalWrite(13, LOW);
- // if sharing pins, you'll need to fix the directions of the touchscreen pins
- pinMode(XM, OUTPUT);
- pinMode(YP, OUTPUT);
- // There is a minimum pressure that is consider valid
- // Pressure of 0 means no pressing
- if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
- {
- p.x = p.x + p.y;
- p.y = p.x - p.y;
- p.x = p.x - p.y;
- p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
- p.y = tft.height() - (map(p.y, TS_MINY, TS_MAXY, tft.height(), 0));
- }
- // Go thru all the buttons, checking if they were pressed
- for (uint8_t b = 0; b < 9; b++) {
- if ((buttons[b].contains(p.x, p.y)) && p.x > 10)
- {
- Serial.print("Pressing: "); Serial.println(b);
- if (b == 0) {
- // Toggle Prev status
- PREV_pressed = true;
- } else if (b == 1) {
- // Toggle Pause status
- PAUSE_pressed = true;
- } else if (b == 2) {
- // Toggle Next status
- NEXT_pressed = true;
- } else if (b == 3) {
- // Toggle Red status
- RED_pressed = true;
- } else if (b == 4) {
- // Toggle Green status
- GRN_pressed = true;
- } else if (b == 5) {
- // Toggle Blue status
- BLU_pressed = true;
- } else if (b == 6) {
- // Toggle Blue status
- Check_pressed = true;
- }
- else if (b == 7) {
- // Toggle Blue status
- Stop_pressed = true;
- }
- Serial.print("0: "); Serial.println(PREV_pressed);
- Serial.print("1: "); Serial.println(PAUSE_pressed);
- Serial.print("2: "); Serial.println(NEXT_pressed);
- Serial.print("3: "); Serial.println(RED_pressed);
- Serial.print("4: "); Serial.println(GRN_pressed);
- Serial.print("5: "); Serial.println(BLU_pressed);
- Serial.print("6: "); Serial.println(Check_pressed);
- //Mp3
- //
- //if (digitalRead(buttonPause) == ACTIVATED)
- if (PAUSE_pressed)
- {
- if (isPlaying)
- {
- pause1();
- isPlaying = false;
- } else
- {
- isPlaying = true;
- play();
- }
- }
- else if (NEXT_pressed)
- {
- if (isPlaying)
- {
- playNext();
- }
- }
- else if (PREV_pressed)
- {
- // if (isPlaying)
- // {
- // playPrevious();
- // }
- unsigned long currentMillis = millis();
- playAlarm(currentMillis);
- }
- // alarm check
- else if (Check_pressed)
- {
- // play();
- buttonPushedMillis = currentMillis;
- playAlarm(currentMillis);
- // startLight();
- }
- // else if (Stop_pressed)
- // {
- // pause1();
- // analogWrite(pwmLED, fadeValue);
- // Check_pressed = false;
- // }
- else if (RED_pressed || GRN_pressed || BLU_pressed) {
- // Button Display
- if (RED_pressed) {
- digitalWrite(RED_LED, HIGH);
- } else {
- digitalWrite(RED_LED, LOW);
- }
- if (GRN_pressed) {
- digitalWrite(GRN_LED, HIGH);
- } else {
- digitalWrite(GRN_LED, LOW);
- }
- if (BLU_pressed) {
- digitalWrite(BLU_LED, HIGH);
- } else {
- digitalWrite(BLU_LED, LOW);
- }
- }
- }
- }
- //Show time and date
- tft.setTextColor(WHITE, BLACK) ;
- tft.setCursor(80, 50);
- tft.print("Time: ");
- tft.print(rtc.getTimeStr());
- tft.setCursor(80, 80);
- tft.print("Date: ");
- tft.print(rtc.getDateStr());
- tft.setCursor(80, 20);
- tft.print("Temp: ");
- tft.print(rtc.getTemp());
- }
- // Functions MP3
- void pause1()
- {
- // execute_CMD(0x0E, 0, 0);
- myDFPlayer.pause();
- delay(500);
- }
- void play()
- {
- // execute_CMD(0x0D, 0, 1);
- myDFPlayer.start();
- delay(500);
- }
- void playNext()
- {
- // execute_CMD(0x01, 0, 1);
- myDFPlayer.next();
- delay(500);
- }
- void playPrevious()
- {
- // execute_CMD(0x02, 0, 1);
- myDFPlayer.previous();
- delay(500);
- }
- // Functions Alarm
- void playAlarm(unsigned long thisMillis)
- {
- // is it time to update yet?
- // if not, nothing happens
- if (thisMillis - previousFadeMillis >= fadeInterval) {
- // yup, it's time!
- if (fadeDirection == UP) {
- fadeValue = fadeValue + fadeIncrement;
- if (fadeValue >= maxPWM) {
- // At max, limit and change direction
- fadeValue = maxPWM;
- fadeDirection = DOWN;
- }
- } else {
- //if we aren't going up, we're going down
- fadeValue = fadeValue - fadeIncrement;
- if (fadeValue <= minPWM) {
- // At min, limit and change direction
- fadeValue = minPWM;
- fadeDirection = UP;
- }
- }
- // Only need to update when it changes
- analogWrite(pwmLED, fadeValue);
- // reset millis for the next iteration (fade timer only)
- previousFadeMillis = thisMillis;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement