Advertisement
nillad

Wake up light code

May 2nd, 2021
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.29 KB | None | 0 0
  1. // Libraries
  2. #include <SoftwareSerial.h>
  3. #include <Adafruit_GFX.h>    // Core graphics library
  4. #include <Adafruit_TFTLCD.h> // LCD library
  5. #include <TouchScreen.h> // Touchscreen Library
  6. #include <MCUFRIEND_kbv.h> // Touchscreen Hardware-specific library
  7. #include <DS3231.h> // Real time clock library
  8. #include <DFRobotDFPlayerMini.h> // MP3 library
  9.  
  10. SoftwareSerial mySoftwareSerial(47, 45); // RX, TX  Define so the MP3 can write back to the computer
  11. DFRobotDFPlayerMini myDFPlayer;
  12.  
  13. // Define pins
  14.  
  15. //LED
  16.  
  17. const byte pwmLED = 46;
  18.  
  19. // define directions for LED fade
  20. #define UP 0
  21. #define DOWN 1
  22.  
  23.  
  24. //for LCD
  25. #define LCD_CS A3 // Chip Select goes to Analog 3
  26. #define LCD_CD A2 // Command/Data goes to Analog 2
  27. #define LCD_WR A1 // LCD Write goes to Analog 1
  28. #define LCD_RD A0 // LCD Read goes to Analog 0
  29. #define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
  30.  
  31. //Define Mp3
  32. # define startByte 0x7E
  33. # define endByte 0xEF
  34. # define versionByte 0xFF
  35. # define dataLength 0x06
  36. # define infoReq 0x01
  37. # define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]
  38. //# define ACTIVATED LOW
  39.  
  40. // Assign human-readable names to some common 16-bit color values:
  41. #define BLACK   0x0000
  42. #define BLUE    0x001F
  43. #define RED     0xF800
  44. #define GREEN   0x07E0
  45. #define CYAN    0x07FF
  46. #define MAGENTA 0xF81F
  47. #define YELLOW  0xFFE0
  48. #define WHITE   0xFFFF
  49.  
  50.  
  51. /******************* UI details */
  52. // Location
  53. #define BUTTON_X 52
  54. #define BUTTON_Y 150
  55. // Size
  56. #define BUTTON_W 80
  57. #define BUTTON_H 45
  58. #define BUTTON_SPACING_X 26
  59. #define BUTTON_SPACING_Y 30
  60. #define BUTTON_TEXTSIZE 2
  61.  
  62. // Define pins for resistive touchscreen
  63. #define YP A2  // must be an analog pin, use "An" notation!
  64. #define XM A3  // must be an analog pin, use "An" notation!
  65. #define YM 8   // can be a digital pin
  66. #define XP 9   // can be a digital pin
  67.  
  68. // Define touchscreen pressure points
  69. #define MINPRESSURE 10
  70. #define MAXPRESSURE 1000
  71.  
  72. // Define touchscreen parameters
  73. // Use test sketch to refine if necessary
  74. #define TS_MINX 120
  75. #define TS_MAXX 847
  76.  
  77. #define TS_MINY 75
  78. #define TS_MAXY 930
  79.  
  80. #define STATUS_X 10
  81. #define STATUS_Y 65
  82.  
  83. //Define LED outputs
  84. #define RED_LED 35
  85. #define GRN_LED 33
  86. #define BLU_LED 31
  87.  
  88. //Define Alarm
  89. DS3231  rtc(SDA, SCL); //Define RTC
  90.  
  91. int fadeTime = 30;
  92. int setHour = 21;
  93. int setMin = 15;
  94. int alarmTime = 10000;
  95.  
  96. //LED
  97.  
  98. // constants for min and max PWM
  99. const int minPWM = 0;
  100. const int maxPWM = 255;
  101.  
  102. // State Variable for Fade Direction
  103. byte fadeDirection = UP;
  104.  
  105. // Global Fade Value
  106. // but be bigger than byte and signed, for rollover
  107. int fadeValue = 0;
  108.  
  109. // How smooth to fade?
  110. byte fadeIncrement = 5;
  111.  
  112. // millis() timing Variable, just for fading
  113. unsigned long previousFadeMillis;
  114. unsigned long buttonPushedMillis;
  115.  
  116. // How fast to increment?
  117. int fadeInterval = 50;
  118.  
  119. //TOUCH SCREEN
  120.  
  121. //Define & buttons state variables
  122. boolean isPlaying = false;
  123. boolean PREV_pressed = false;
  124. boolean PAUSE_pressed = false;
  125. boolean NEXT_pressed = false;
  126. boolean RED_pressed = false;
  127. boolean GRN_pressed = false;
  128. boolean BLU_pressed = false;
  129. boolean Check_pressed = false;
  130. boolean Stop_pressed = false;
  131.  
  132. // Define button array object
  133. Adafruit_GFX_Button buttons[9];
  134.  
  135. // Define arrays with button text and colors
  136. char const * const buttonlabels[]
  137.   = {"Prev", "Pause", "Next", "R", "G", "B", "Check", "Stop", "Light" };
  138. uint16_t buttoncolors[9] = {RED, GREEN, BLUE, RED, GREEN, BLUE, CYAN, MAGENTA, YELLOW,};
  139.  
  140. // Define object for TFT (LCD)display
  141. MCUFRIEND_kbv tft;
  142.  
  143. // Define object for touchscreen
  144. // Last parameter is X-Y resistance, measure or use 300 if unsure
  145. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
  146.  
  147. void setup() {
  148.   // LED
  149.   // put pwmLED into known state (off)
  150.   analogWrite(pwmLED, fadeValue);
  151.  
  152.   //MP3
  153.  
  154.   mySoftwareSerial.begin(9600);
  155.   Serial.begin(9600);
  156.   //   Serial.begin(115200);
  157.  
  158.   Serial.println();
  159.   Serial.println(F("DFRobot DFPlayer Mini Demo"));
  160.   Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
  161.  
  162.   //execute_CMD(0x06, 0, 1); // Set volume
  163.   //execute_CMD(0x07, 0, 1); // Set the equalizer (00= normal, 01=pop, 02=rock, 03=jazz, 04=classic 05=bass)
  164.   //execute_CMD(0x0E, 0, 0);
  165.  
  166.   if (!myDFPlayer.begin(mySoftwareSerial, false)) {  //Use softwareSerial to communicate with mp3.
  167.     Serial.println(F("Unable to begin:"));
  168.     Serial.println(F("1.Please recheck the connection!"));
  169.     Serial.println(F("2.Please insert the SD card!"));
  170.     while (true);
  171.   }
  172.   Serial.println(F("DFPlayer Mini online."));
  173.  
  174.   myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
  175.  
  176.   //----Set volume----
  177.   myDFPlayer.volume(2);  //Set volume value (0~30).
  178.   delay(100);
  179.   //myDFPlayer.volumeUp(); //Volume Up
  180.   //myDFPlayer.volumeDown(); //Volume Down
  181.  
  182.   //----Set different EQ----
  183.   myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
  184.   //  myDFPlayer.EQ(DFPLAYER_EQ_POP);
  185.   //  myDFPlayer.EQ(DFPLAYER_EQ_ROCK);
  186.   //  myDFPlayer.EQ(DFPLAYER_EQ_JAZZ);
  187.   //  myDFPlayer.EQ(DFPLAYER_EQ_CLASSIC);
  188.   //  myDFPlayer.EQ(DFPLAYER_EQ_BASS);
  189.  
  190.   //----Set device we use SD as default----
  191.   myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
  192.  
  193.  
  194.   //----Mp3 control----
  195.   //  myDFPlayer.sleep();     //sleep
  196.   //  myDFPlayer.reset();     //Reset the module
  197.   //  myDFPlayer.enableDAC();  //Enable On-chip DAC
  198.   //  myDFPlayer.disableDAC();  //Disable On-chip DAC
  199.   //  myDFPlayer.outputSetting(true, 15); //output setting, enable the output and set the gain to 15
  200.  
  201.  
  202.   // Touch Screen text
  203.  
  204.   Serial.println(F("TFT LCD test")); //TFT recognision
  205.   Serial.println("version 3"); // Version declaration
  206.  
  207.   // Declare LEDs
  208.   pinMode(RED_LED, OUTPUT);
  209.   pinMode(GRN_LED, OUTPUT);
  210.   pinMode(BLU_LED, OUTPUT);
  211.  
  212.   // Real time clock
  213.   rtc.begin();
  214.  
  215.   //    rtc.setDOW(MONDAY);     // Set Day-of-Week to SUNDAY
  216.   //    rtc.setTime(16, 48, 0);     // Set the time to 12:00:00 (24hr format)
  217.   //    rtc.setDate(5, 4, 2021);   // Set the date to January 1st, 2014
  218.  
  219.   //Touch screen
  220.   tft.reset();
  221.  
  222.   uint16_t identifier = tft.readID();
  223.   if (identifier == 0x9486)
  224.   {
  225.     Serial.println(F("Found 0x9486 LCD driver"));
  226.   }
  227.  
  228.   // Setup the Display
  229.   tft.begin(identifier);
  230.   Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());
  231.   tft.setRotation(0); // Rotation 0 = portrait 1 = landscape
  232.   tft.fillScreen(BLACK);
  233.  
  234.   tft.print("Rozan's wake up light V.1");
  235.  
  236.   // Draw buttons
  237.   for (uint8_t row = 0; row < 3; row++) {
  238.     for (uint8_t col = 0; col < 3; col++) {
  239.       buttons[col + row * 3].initButton(&tft, BUTTON_X + col * (BUTTON_W + BUTTON_SPACING_X),
  240.                                         BUTTON_Y + row * (BUTTON_H + BUTTON_SPACING_Y), // x, y, w, h, outline, fill, text
  241.                                         BUTTON_W, BUTTON_H, WHITE, buttoncolors[col + row * 3], WHITE,
  242.                                         buttonlabels[col + row * 3], BUTTON_TEXTSIZE);
  243.       buttons[col + row * 3].drawButton();
  244.     }
  245.   }
  246.  
  247. }
  248.  
  249. void loop() {
  250.  
  251.   // For fade alarm
  252.   unsigned long currentMillis = millis();
  253.   //  playAlarm(currentMillis);
  254.  
  255.   // Define button state variables
  256.  
  257.   PREV_pressed = false;
  258.   PAUSE_pressed = false;
  259.   NEXT_pressed = false;
  260.   RED_pressed = false;
  261.   GRN_pressed = false;
  262.   BLU_pressed = false;
  263.   //  Check_pressed = false;
  264.   Stop_pressed = false;
  265.  
  266.   digitalWrite(13, HIGH);
  267.   TSPoint p = ts.getPoint();
  268.   digitalWrite(13, LOW);
  269.  
  270.   // if sharing pins, you'll need to fix the directions of the touchscreen pins
  271.   pinMode(XM, OUTPUT);
  272.   pinMode(YP, OUTPUT);
  273.  
  274.   // There is a minimum pressure that is consider valid
  275.   // Pressure of 0 means no pressing
  276.  
  277.   if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
  278.   {
  279.  
  280.     p.x = p.x + p.y;
  281.     p.y = p.x - p.y;
  282.     p.x = p.x - p.y;
  283.     p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
  284.     p.y = tft.height() - (map(p.y, TS_MINY, TS_MAXY, tft.height(), 0));
  285.  
  286.   }
  287.  
  288.   // Go thru all the buttons, checking if they were pressed
  289.   for (uint8_t b = 0; b < 9; b++) {
  290.     if ((buttons[b].contains(p.x, p.y)) && p.x > 10)
  291.     {
  292.       Serial.print("Pressing: "); Serial.println(b);
  293.       if (b == 0) {
  294.         // Toggle Prev status
  295.         PREV_pressed = true;
  296.       } else if (b == 1) {
  297.         // Toggle Pause status
  298.         PAUSE_pressed = true;
  299.       } else if (b == 2) {
  300.         // Toggle Next status
  301.         NEXT_pressed = true;
  302.       } else if (b == 3) {
  303.         // Toggle Red status
  304.         RED_pressed = true;
  305.       } else if (b == 4) {
  306.         // Toggle Green status
  307.         GRN_pressed = true;
  308.       } else if (b == 5) {
  309.         // Toggle Blue status
  310.         BLU_pressed = true;
  311.       } else if (b == 6) {
  312.         // Toggle Blue status
  313.         Check_pressed = true;
  314.       }
  315.       else if (b == 7) {
  316.         // Toggle Blue status
  317.         Stop_pressed = true;
  318.       }
  319.  
  320.  
  321.       Serial.print("0: "); Serial.println(PREV_pressed);
  322.       Serial.print("1: "); Serial.println(PAUSE_pressed);
  323.       Serial.print("2: "); Serial.println(NEXT_pressed);
  324.       Serial.print("3: "); Serial.println(RED_pressed);
  325.       Serial.print("4: "); Serial.println(GRN_pressed);
  326.       Serial.print("5: "); Serial.println(BLU_pressed);
  327.       Serial.print("6: "); Serial.println(Check_pressed);
  328.       //Mp3
  329.       //
  330.       //if (digitalRead(buttonPause) == ACTIVATED)
  331.       if (PAUSE_pressed)
  332.       {
  333.         if (isPlaying)
  334.         {
  335.           pause1();
  336.           isPlaying = false;
  337.         } else
  338.         {
  339.           isPlaying = true;
  340.           play();
  341.         }
  342.       }
  343.       else if (NEXT_pressed)
  344.       {
  345.         if (isPlaying)
  346.         {
  347.           playNext();
  348.         }
  349.       }
  350.  
  351.       else if (PREV_pressed)
  352.       {
  353.         //        if (isPlaying)
  354.         //        {
  355.         //          playPrevious();
  356.         //        }
  357.         unsigned long currentMillis = millis();
  358.         playAlarm(currentMillis);
  359.       }
  360.  
  361.       // alarm check
  362.       else if (Check_pressed)
  363.       {
  364.         //       play();
  365.         buttonPushedMillis = currentMillis;
  366.         playAlarm(currentMillis);
  367.         //        startLight();
  368.       }
  369.       //      else if (Stop_pressed)
  370.       //      {
  371.       //        pause1();
  372.       //        analogWrite(pwmLED, fadeValue);
  373.       //        Check_pressed = false;
  374.       //      }
  375.  
  376.  
  377.       else if (RED_pressed || GRN_pressed || BLU_pressed) {
  378.  
  379.         // Button Display
  380.         if (RED_pressed) {
  381.           digitalWrite(RED_LED, HIGH);
  382.         } else {
  383.           digitalWrite(RED_LED, LOW);
  384.         }
  385.         if (GRN_pressed) {
  386.           digitalWrite(GRN_LED, HIGH);
  387.         } else {
  388.           digitalWrite(GRN_LED, LOW);
  389.         }
  390.         if (BLU_pressed) {
  391.           digitalWrite(BLU_LED, HIGH);
  392.         } else {
  393.           digitalWrite(BLU_LED, LOW);
  394.         }
  395.       }
  396.     }
  397.   }
  398.  
  399.  
  400.   //Show time and date
  401.   tft.setTextColor(WHITE, BLACK) ;
  402.   tft.setCursor(80, 50);
  403.   tft.print("Time: ");
  404.   tft.print(rtc.getTimeStr());
  405.  
  406.   tft.setCursor(80, 80);
  407.   tft.print("Date: ");
  408.   tft.print(rtc.getDateStr());
  409.  
  410.   tft.setCursor(80, 20);
  411.   tft.print("Temp: ");
  412.   tft.print(rtc.getTemp());
  413. }
  414.  
  415. // Functions MP3
  416.  
  417. void pause1()
  418. {
  419.   //  execute_CMD(0x0E, 0, 0);
  420.   myDFPlayer.pause();
  421.   delay(500);
  422. }
  423.  
  424. void play()
  425. {
  426.   //  execute_CMD(0x0D, 0, 1);
  427.   myDFPlayer.start();
  428.   delay(500);
  429. }
  430.  
  431. void playNext()
  432. {
  433.   //  execute_CMD(0x01, 0, 1);
  434.   myDFPlayer.next();
  435.   delay(500);
  436. }
  437.  
  438. void playPrevious()
  439. {
  440.   //  execute_CMD(0x02, 0, 1);
  441.   myDFPlayer.previous();
  442.   delay(500);
  443. }
  444.  
  445.  
  446. // Functions Alarm
  447.  
  448. void playAlarm(unsigned long thisMillis)
  449. {
  450.   // is it time to update yet?
  451.   // if not, nothing happens
  452.   if (thisMillis - previousFadeMillis >= fadeInterval) {
  453.     // yup, it's time!
  454.     if (fadeDirection == UP) {
  455.       fadeValue = fadeValue + fadeIncrement;
  456.       if (fadeValue >= maxPWM) {
  457.         // At max, limit and change direction
  458.         fadeValue = maxPWM;
  459.         fadeDirection = DOWN;
  460.       }
  461.     } else {
  462.       //if we aren't going up, we're going down
  463.       fadeValue = fadeValue - fadeIncrement;
  464.       if (fadeValue <= minPWM) {
  465.         // At min, limit and change direction
  466.         fadeValue = minPWM;
  467.         fadeDirection = UP;
  468.       }
  469.     }
  470.     // Only need to update when it changes
  471.     analogWrite(pwmLED, fadeValue);
  472.  
  473.     // reset millis for the next iteration (fade timer only)
  474.     previousFadeMillis = thisMillis;
  475.   }
  476. }
  477.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement