Advertisement
Guest User

Untitled

a guest
Sep 25th, 2014
1,204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.33 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <SPI.h>
  3. #include <RTClib.h>
  4. #include <EEPROM.h>
  5. //#include <avr/pgmspace.h>
  6. //#include <Time.h>
  7. #include <Adafruit_GFX.h> // Core graphics library
  8. #include <Adafruit_ST7735.h> // Hardware-specific library
  9.  
  10.  
  11. RTC_DS3231 RTC;
  12.  
  13. //COLOR DEFINITIONS
  14. #define BLACK 0x0000
  15. #define BLUE 0x001F
  16. #define RED 0xF800
  17. #define GREEN 0x07E0
  18. #define CYAN 0x07FF
  19. #define MAGENTA 0xF81F
  20. #define YELLOW 0xFE0
  21. #define WHITE 0xFFFF
  22. #define GREY 0x8410
  23.  
  24. // ARRAY TO REPLACE SINGLE DIGITS WITH LEADING ZEROES
  25. char* leadingZero[]={"12", "01", "02", "03", "04", "05", "06", "07", "08", "09"};
  26.  
  27. // VARIABLES TO HOLD COLOR VALUES
  28. int redValue = 0;
  29. int grnValue = 0;
  30. int bluValue = 0;
  31.  
  32. //VARIABLE FOR MENU STATE
  33. int menuState = 0;
  34. int brightnessState = 0;
  35. int hourState = 0;
  36. int varSet[] = {0, 0};
  37.  
  38. //TFT SHIELD STUFF
  39. //Use these pins for the shield!
  40. #define sclk 13
  41. #define mosi 11
  42. #define cs 10
  43. #define dc 8
  44. #define rst 0 // you can also connect this to the Arduino reset
  45.  
  46.  
  47. // MORE SHIELD STUFF
  48. #if defined(__SAM3X8E__)
  49. #undef __FlashStringHelper::F(string_literal)
  50. #define F(string_literal) string_literal
  51. #endif
  52. int position = 0;
  53. int dim = 255;
  54. int bright = 0;
  55. long previousMillis = 0;
  56. int interval = 1000;
  57.  
  58. //THIS LINE MAY VARY - SEE EXAMPLE SKETCHES FROM SHIELD
  59. Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);
  60.  
  61. //DEFINITIONS FOR THE JOYSTICK
  62. #define Neutral 0
  63. #define Press 1
  64. #define Up 2
  65. #define Down 3
  66. #define Right 4
  67. #define Left 5
  68.  
  69.  
  70. void setup() {
  71. Wire.begin();
  72. RTC.begin();
  73. if (! RTC.isrunning()) {
  74. Serial.println("RTC is NOT running!");
  75. // following line sets the RTC to the date & time this sketch was compiled
  76. RTC.adjust(DateTime(__DATE__, __TIME__));
  77. }
  78. DateTime now = RTC.now();
  79.  
  80. //READ SAVED BRIGHTNESS AND 12/24 STATUS FROM PROGMEM
  81. brightnessState = EEPROM.read(254);
  82. hourState = EEPROM.read(255);
  83.  
  84. if (brightnessState == 1){
  85. analogWrite(5, 100);
  86. }
  87.  
  88. //Serial.begin(9600);
  89.  
  90.  
  91. // MANAUALLY ENTER TIME WITH THIS LINE, COMMENT OUT IF USING RTC
  92. //setTime(16, 56, 0, 16, 9, 2014);
  93.  
  94. // MORE TFT STUFF - SEE EXAMPLE SKETCHES
  95. tft.initR(INITR_BLACKTAB); // initialize a ST7735S chip, black tab
  96.  
  97. //ROTATES DISPLAY 90 DEGREES FOR LANDSCAPE VIEW
  98. tft.setRotation(3);
  99.  
  100. // analogWrite(5, 255); //jump "LITE" and Digital pin 5 to dim
  101.  
  102. // BLANKS OUT THE SCREEN AND DRAWS THE BLACK BAR
  103. tft.fillScreen(WHITE);
  104. tft.fillRect(0, 50, 160, 30, BLACK);
  105. }
  106.  
  107. void loop(){
  108.  
  109. Serial.println(brightnessState);
  110. Serial.println(hourState);
  111.  
  112. int joy = CheckJoystick();
  113. delay(60);
  114. switch (joy)
  115. {
  116. case Press:
  117. menuState = 1;
  118. menu();
  119. }
  120.  
  121. //PERFORMS A TASK EVERY SECOND WITHOUT USING DELAY
  122. unsigned long currentMillis = millis();
  123. if(currentMillis - previousMillis > interval) {
  124. previousMillis = currentMillis;
  125.  
  126. // UPDATES THE SCREEN
  127.  
  128. drawTime();
  129.  
  130. }
  131. }
  132.  
  133.  
  134. void drawTime(){
  135.  
  136. // SETS THE VARIABLE t TO THE CURRENT TIME - PREVENTS OVERLAPPING
  137. DateTime now = RTC.now();
  138.  
  139. //MAPS THE TIME TO CORRESPOND TO COLOR VALUES
  140. // CONVERTS 24 HOURS TO 0 - 255 STEPS, ETC.
  141. redValue = map(now.hour(), 0, 23, 0, 255);
  142. grnValue = map(now.minute(), 0, 59, 0, 255);
  143. bluValue = map(now.second(), 0, 59, 0, 255);
  144.  
  145. //SETS THE TEXT COLOR AND TEXT BACKGROUND COLOR
  146. //USES THE GFX FUNCTION TO CONVERT THREE INTEGER VALUES TO A HEX COLOR VALUE
  147. // REPLACE TFT.COLOR565 WITH WHITE OR ANOTHER COLOR IF YOU WANT A STATIC TIME COLOR
  148. tft.setTextColor((tft.Color565(redValue, grnValue, bluValue)), BLACK);
  149. // YOU HAVE TO SET THE TEXT SIZE BEFORE YOU START PRINTING
  150. tft.setTextSize(3);
  151.  
  152. //DRAWS RECTANGLES ABOVE AND BELOW THE BLACK BAR IN THE SAME COLOR
  153. tft.fillRect(0, 0, 160, 50, (tft.Color565(redValue, grnValue, bluValue)));
  154. tft.fillRect(0, 80, 160, 50, (tft.Color565(redValue, grnValue, bluValue)));
  155.  
  156. // LINES UP THE CURSOR AND DRAWS THE HOUR
  157. // IF THE HOUR IS LESS THAN 10 IT CALLS A VALUE FROM THE LEADING ZERO ARRAY
  158.  
  159. //FOR 24 HR TIME
  160. if (hourState == 0){
  161. tft.setCursor(13, 54);
  162. if (now.hour() >= 0 && now.hour() < 10) {
  163. tft.print(leadingZero[now.hour()]);
  164. }
  165. else{
  166. tft.print(now.hour());
  167. }
  168. }
  169. //FOR 12 HR TIME
  170. if (hourState == 1){
  171. tft.setCursor(13, 54);
  172. int hour = now.hour();
  173.  
  174. if (hour > 12){
  175. hour = hour - 12;
  176. tft.print(leadingZero[hour]);
  177. }
  178. else if (hour >= 0 && hour < 10) {
  179. tft.print(leadingZero[hour]);
  180. }
  181.  
  182. else{
  183. tft.print(hour);
  184. }
  185. }
  186. // INSERTS THE FIRST DIVIDING COLON
  187. tft.setCursor(48, 54);
  188. tft.print(":");
  189.  
  190. // MOVES THE CURSOR AND DRAWS THE MINUTES, LEADING ZERO, ETC
  191. tft.setCursor(65, 54);
  192. if (now.minute() >= 0 && now.minute() < 10){
  193. tft.print(leadingZero[now.minute()]);
  194. }
  195. else{
  196. tft.print(now.minute());
  197. }
  198.  
  199. // INSERTS THE SECOND COLON
  200. tft.setCursor(99, 54);
  201. tft.print(":");
  202.  
  203. // MOVES THE CURSOR AND DRAWS THE SECONDS, LEADING ZERO, ETC
  204. tft.setCursor(115, 54);
  205. if (now.second() >= 0 && now.second() <10){
  206. tft.print(leadingZero[now.second()]);
  207. }
  208. else{
  209. tft.print(now.second());
  210. }
  211.  
  212.  
  213. }
  214.  
  215. // FUNCTION TO CHECK THE JOYSTICK POSITION
  216. int CheckJoystick(){
  217. int joystickState = analogRead(3);
  218.  
  219. if (joystickState < 50) return Left;
  220. if (joystickState < 150) return Down;
  221. if (joystickState < 250) return Press;
  222. if (joystickState < 500) return Right;
  223. if (joystickState < 650) return Up;
  224. return Neutral;
  225. }
  226.  
  227. void menu(){
  228. tft.fillScreen(WHITE);
  229. tft.setTextColor(BLACK, WHITE);
  230. tft.setTextSize(1);
  231. tft.setCursor(43, 2);
  232. tft.print("LEFT or RIGHT");
  233. tft.setCursor(27, 15);
  234. tft.print("to set brightness.");
  235. if (brightnessState == 0){
  236. tft.setCursor(65, 30);
  237. tft.print("HIGH");
  238. }
  239. if (brightnessState == 1){
  240. tft.setCursor(63, 30);
  241. tft.print(" LOW ");
  242. }
  243. tft.setCursor(47, 50);
  244. tft.print("UP or DOWN");
  245. tft.setCursor(39, 65);
  246. tft.print("for 12/24 hr.");
  247. if (hourState == 0){
  248. tft.setCursor(62, 80);
  249. tft.print("24 HR");
  250. }
  251. if (hourState == 1){
  252. tft.setCursor(62, 80);
  253. tft.print("12 HR");
  254. }
  255. tft.setCursor(37, 110);
  256. tft.print("PRESS to exit.");
  257. //delay(40);
  258.  
  259. do{
  260. unsigned long currentMillis = millis();
  261.  
  262. if(currentMillis - previousMillis > 100){
  263. previousMillis = currentMillis;
  264.  
  265.  
  266. int joy = CheckJoystick();
  267. delay(60);
  268. switch (joy)
  269. {
  270. case Left:
  271. brightnessState = 1;
  272. analogWrite(5, 100);
  273. tft.setCursor(63, 30);
  274. tft.print(" LOW ");
  275. break;
  276. case Right:
  277. brightnessState = 0;
  278. analogWrite(5, 255);
  279. tft.setCursor(65, 30);
  280. tft.print("HIGH");
  281. break;
  282. case Up:
  283. hourState = 1;
  284. tft.setCursor(62, 80);
  285. tft.print("12 HR");
  286. break;
  287. case Down:
  288. hourState = 0;
  289. tft.setCursor(62, 80);
  290. tft.print("24 HR");
  291. break;
  292. case Press:
  293. tft.fillRect(0, 50, 160, 30, BLACK);
  294. menuState = 0;
  295. EEPROM.write(254, brightnessState); //SAVE BRIGHTNESS AND 12/24 STATUS TO EEPROM
  296. EEPROM.write(255, hourState); //SAVE BRIGHTNESS AND 12/24 STATUS TO EEPROM
  297. delay(100);
  298. return;
  299. break;
  300. }
  301. }
  302. }
  303. while (menuState == 1);
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement