Advertisement
Guest User

Code for Arduino Clock/ Cryptofolio

a guest
Jun 7th, 2013
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.76 KB | None | 0 0
  1. /*
  2. * NewTimeSerial.pde
  3. * example code illustrating Time library date strings
  4. *
  5. * Time can be set using an 11 character message consisiting of the letter 'T'
  6. * followed by the ten digit number of seconds since Jan 1 1970 (Unix time)
  7. *
  8. * The time can also be set as follows:
  9. * S[hh:mm:ss dd mm yy]
  10. * Values must be given in order starting from hh
  11. * S[14] sets the hour to 2pm, S[14:10] sets 2:10 pm
  12. * values must be two digits with preceeding 0 if necessary (eg 05 to set the value 5)
  13. * year must be two digits, 2010 is entered as 10
  14. * opening and closing square brackest are required, colon and spaces are optional
  15. *
  16. *
  17. */
  18.  
  19. #include "Wire.h"
  20. #define DS1307_I2C_ADDRESS 0x68 // This is the I2C address
  21. // Arduino version compatibility Pre-Compiler Directives
  22. #if defined(ARDUINO) && ARDUINO >= 100 // Arduino v1.0 and newer
  23. #define I2C_WRITE Wire.write
  24. #define I2C_READ Wire.read
  25. #else // Arduino Prior to v1.0
  26. #define I2C_WRITE Wire.send
  27. #define I2C_READ Wire.receive
  28. #endif
  29.  
  30.  
  31. #include <Time.h>
  32. #include <LiquidCrystal.h>
  33.  
  34. #include <SPI.h>
  35. #include <Ethernet.h>
  36.  
  37. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
  38.  
  39. char serverName[] = "www.cryptofolio.info"; // web page
  40. EthernetClient client;
  41.  
  42.  
  43. #define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
  44. #define TIME_HEADER 'T' // Header tag for serial time sync message
  45. #define TIME_SET_TAG 'S' // Indicates start of time set string
  46. #define TIME_SET_StrLen 15 // the maximum length of a time set string [hhmmssddmmyyyy]
  47. #define TIME_REQUEST 7 // ASCII bell character requests a time sync message
  48.  
  49. TimeElements te; // holds the time elements for setting the time
  50.  
  51. // initialize the library with the numbers of the interface pins
  52. LiquidCrystal lcd(9, 8, 6, 5, 4, 3);
  53.  
  54. int ledPin = 7; // LED connected to digital pin 13
  55. int inPin = 2; // IR Button connected to digital pin 7
  56.  
  57.  
  58. // Variables will change:
  59. volatile int ledState = HIGH; // ledState used to set the LED
  60. //long previousMillis = 0; // will store last time LED was updated
  61.  
  62. // the follow variables is a long because the time, measured in miliseconds,
  63. // will quickly become a bigger number than can be stored in an int.
  64. long interval = 250; // interval at which to blink (milliseconds)
  65.  
  66. // Global Variables
  67. int command = 0; // This is the command char, in ascii form, sent from the serial port
  68. int i;
  69. long previousMillis = 0; // will store last time Temp was updated
  70. //byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  71. byte test;
  72. byte zero;
  73. char *Day[] = {"","Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
  74. char *Mon[] = {"","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  75.  
  76. String TotalBTC = "TotalBTC";
  77. String GrandTotal = "GrandTotal";
  78.  
  79. int timeCount = 0;
  80.  
  81. void setup()
  82. {
  83.  
  84. Wire.begin();
  85. //Serial.begin(57600);
  86. zero=0x00;
  87.  
  88.  
  89. pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
  90. pinMode(inPin, INPUT); // sets the digital pin 7 as input
  91.  
  92. attachInterrupt(0, backlight, LOW); // 0 = digital pin 2
  93.  
  94. // set up the LCD's number of columns and rows:
  95. lcd.begin(16, 2);
  96.  
  97. Serial.begin(9600);
  98. setSyncInterval(1800);
  99. setSyncProvider(requestSync); //set function to call when sync required
  100. Ethernet.begin(mac);
  101. Serial.println(Ethernet.localIP());
  102. Startup();
  103.  
  104.  
  105. }
  106.  
  107. void loop()
  108. {
  109.  
  110.  
  111.  
  112. if (Serial.available()) { // Look for char in serial que and process if found
  113. command = Serial.read();
  114. if (command == 84 || command == 116) { //If command = "Tt" Set Date
  115. setDateDs1307();
  116. getDateDs1307();
  117. Serial.println(" ");
  118. }
  119. else if (command == 82 || command == 114) { //If command = "Rr" Read Date ... BBR
  120. getDateDs1307();
  121. Serial.println(" ");
  122. }
  123. }
  124.  
  125.  
  126. processSetTime();
  127.  
  128. if(timeStatus()!= timeNotSet)
  129. {
  130. digitalClockDisplay();
  131.  
  132. }
  133.  
  134.  
  135. timeCount++;
  136. Serial.println(timeCount);
  137.  
  138. if(timeCount == 60)
  139. {
  140. GetData("TotalBTC");
  141.  
  142. }
  143. else if( timeCount == 120)
  144. {
  145. timeCount = 0;
  146. GetData("GrandTotal");
  147. }
  148.  
  149.  
  150. delay(1000);
  151.  
  152. //backlight();
  153.  
  154. }
  155.  
  156.  
  157.  
  158. void GetData(String type)
  159. {
  160. if (client.connect(serverName, 80))
  161. { //starts client connection, checks for connection
  162.  
  163. Serial.println("connected");
  164. client.println("GET /api.aspx?User=<!!!!passprhase here!!!>&Exchange=BTC-E&LocalCurrency=AUD&Result="+type+" HTTP/1.0"); //download text
  165. client.println("Host: www.cryptofolio.info");
  166. client.println(); //end of get request
  167.  
  168.  
  169. int counter = 0;
  170. boolean goTime = false;
  171. while(client.connected() && !client.available()) delay(1); //waits for data
  172. while (client.connected() || client.available())
  173. { //connected or data available
  174. char c = client.read(); //gets byte from ethernet buffer
  175.  
  176.  
  177. Serial.print(c); //prints byte to serial monitor
  178.  
  179. if( c == char(124))
  180. {
  181. goTime = !goTime;
  182.  
  183. if(goTime)
  184. {
  185. if(type == "TotalBTC")
  186. TotalBTC = "";
  187. else
  188. GrandTotal = "";
  189. }
  190. }
  191.  
  192. if(goTime)
  193. {
  194. if(c != char(124))
  195. {
  196. if(type == "TotalBTC")
  197. {
  198. TotalBTC += c ;
  199. }
  200. else
  201. {
  202. GrandTotal += c ;
  203. }
  204.  
  205. counter = counter + 1;
  206. }
  207. }
  208.  
  209. }
  210.  
  211. }
  212. else
  213. {
  214. Serial.println("connection failed");
  215. Serial.println("disconnecting");
  216. client.stop();
  217. GetData(type);
  218. }
  219.  
  220. }
  221.  
  222. void backlight()
  223. {
  224. //tone1.stop();
  225.  
  226.  
  227. unsigned long currentMillis = millis();
  228.  
  229. if(currentMillis - previousMillis > interval) {
  230. // save the last time you blinked the LED
  231. previousMillis = currentMillis;
  232.  
  233. // if the LED is off turn it on and vice-versa:
  234. if (ledState == LOW)
  235. ledState = HIGH;
  236. else
  237. ledState = LOW;
  238.  
  239. // set the LED with the ledState of the variable:
  240. digitalWrite(ledPin, ledState);
  241. }
  242.  
  243. }
  244.  
  245. void Startup()
  246. {
  247. digitalWrite(ledPin, LOW );
  248. lcd.clear();
  249. lcd.setCursor(0, 0);
  250. lcd.print("WELCOME");
  251. delay(50);
  252. lcd.setCursor(0, 0);
  253. lcd.print(" WELCOME");
  254. delay(100);
  255. lcd.setCursor(0, 0);
  256. lcd.print(" WELCOME");
  257. delay(200);
  258. lcd.setCursor(0, 0);
  259. lcd.print(" WELCOME");
  260. delay(250);
  261. lcd.setCursor(0, 0);
  262. lcd.print(" WELCOME");
  263. delay(450);
  264. lcd.clear();
  265. digitalWrite(ledPin, HIGH );
  266. lcd.print("Waiting...");
  267. }
  268.  
  269. void digitalClockDisplay()
  270. {
  271. if(second() < 10 || (second() > 20 && second() <30) || (second() > 40 && second() < 50))
  272. {
  273.  
  274. lcd.setCursor(0, 0);
  275. lcd.print( " "+TotalBTC+" ");
  276. Serial.println(TotalBTC);
  277.  
  278. //bottom of display
  279. lcd.setCursor(0, 1);
  280. String dmy = " " + strWeekday() + " "+printDigits(day())+"/"+printDigits(month())+"/"+(String)year();
  281. lcd.print( dmy);
  282. }
  283. else
  284. {
  285.  
  286. lcd.setCursor(0, 0);
  287. lcd.print( " "+GrandTotal+" ");
  288. Serial.println( GrandTotal);
  289.  
  290. //bottom of display
  291. lcd.setCursor(0, 1);
  292. int hour12 = hourFormat12();
  293. if(isAM())
  294. lcd.print(" "+printDigits(hour12) +":" + printDigits(minute()) +" AM" + " ");// + ":" + printDigits(second())
  295. else
  296. lcd.print(" "+printDigits(hour12) +":" + printDigits(minute()) +" PM" + " ");//+ ":" + printDigits(second())
  297. }
  298. }
  299.  
  300. String strWeekday()
  301. {
  302. String week = "";
  303.  
  304. switch (weekday())
  305. {
  306. case 1:
  307. week = "Sun";
  308. break;
  309. case 2:
  310. week = "Mon";
  311. break;
  312. case 3:
  313. week = "Tue";
  314. break;
  315. case 4:
  316. week = "Wed";
  317. break;
  318. case 5:
  319. week = "Thu";
  320. break;
  321. case 6:
  322. week = "Fri";
  323. break;
  324. case 7:
  325. week = "Sat";
  326. break;
  327. }
  328.  
  329. return week;
  330. }
  331.  
  332.  
  333. String printDigits(int digits)
  334. {
  335. // utility function for digital clock display: prints preceding colon and leading 0
  336. String result = "";
  337. if(digits < 10)
  338. result += "0";
  339. result += digits;
  340. return result;
  341. }
  342.  
  343. void processSetTime(){
  344. // //[hh:mm:ss dd mm yyyy]
  345. // char setString[TIME_SET_StrLen];
  346. // int index = 0;
  347. // char c = Serial.read();
  348. // if( c != '[')
  349. // return; // first character must be opening square brackets
  350. // do
  351. // {
  352. // c = Serial.read();
  353. // if( isdigit(c)) // non numeric characters are discarded
  354. // setString[index++] = c -'0'; // convert from ascii
  355. // }
  356. // while (c != ']'); // wait for trailing square brackets
  357. //
  358. // breakTime(now(), te); // put the time now into the TimeElements structure
  359. //
  360. // int count = index;
  361. // int element = 0;
  362. // for( index = 0; index < count; index += 2) // step through each pair of digits
  363. // {
  364. // int val = setString[index] * 10 + setString[index+1] ; // get the numeric value of the next pair of numbers
  365. // switch( element++){
  366. // case 0 : te.Hour = val; break;
  367. // case 1 : te.Minute = val; break;
  368. // case 2 : te.Second = val; break;
  369. // case 3 : te.Day = val; break;
  370. // case 4 : te.Month= val; break;
  371. // case 5 : te.Year = val + 30; break; // year 0 is 1970
  372. // }
  373. // }
  374. // setTime( makeTime(te));
  375.  
  376. // Reset the register pointer
  377. Wire.beginTransmission(DS1307_I2C_ADDRESS);
  378. I2C_WRITE(zero);
  379. Wire.endTransmission();
  380.  
  381. Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
  382.  
  383. // A few of these need masks because certain bits are control bits
  384. te.Second = bcdToDec(I2C_READ() & 0x7f);
  385. te.Minute = bcdToDec(I2C_READ());
  386. te.Hour = bcdToDec(I2C_READ() & 0x3f); // Need to change this if 12 hour am/pm
  387. int dayOfWeek = bcdToDec(I2C_READ());
  388. //dayOfMonth = bcdToDec(I2C_READ());
  389. te.Day = bcdToDec(I2C_READ());
  390. te.Month = bcdToDec(I2C_READ());
  391. te.Year = bcdToDec(I2C_READ()) + 30;
  392.  
  393. setTime( makeTime(te));
  394. }
  395.  
  396. void processSyncMessage() {
  397. // // wait for ten digts, convert to time and update systemTime
  398. // // todo - timeout after say 5 seconds
  399. // time_t pctime = 0;
  400. //
  401. //
  402. // while(Serial.available() < TIME_MSG_LEN-1 ) // time message consists of a header and ten ascii digits
  403. // ;
  404. // for(int i=0; i < TIME_MSG_LEN -1; i++)
  405. // {
  406. // char c = Serial.read();
  407. // if( c >= '0' && c <= '9'){
  408. // pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
  409. // }
  410. // }
  411. // setTime(pctime); // Sync Arduino clock to the time received on the serial port
  412. }
  413.  
  414. time_t requestSync()
  415. {
  416. //Serial.print(TIME_REQUEST,BYTE);
  417. //Serial.print(1);
  418. return 0; // the time will be sent later in response to serial mesg
  419. }
  420.  
  421.  
  422. // Convert binary coded decimal to normal decimal numbers
  423. byte bcdToDec(byte val)
  424. {
  425. return ( (val/16*10) + (val%16) );
  426. }
  427.  
  428. // Convert normal decimal numbers to binary coded decimal
  429. byte decToBcd(byte val)
  430. {
  431. return ( (val/10*16) + (val%10) );
  432. }
  433.  
  434. // 1) Sets the date and time on the ds1307
  435. // 2) Starts the clock
  436. // 3) Sets hour mode to 24 hour clock
  437. // Assumes you're passing in valid numbers, Probably need to put in checks for valid numbers.
  438.  
  439. void setDateDs1307()
  440. {
  441.  
  442. byte second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result.
  443. byte minute = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
  444. byte hour = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
  445. byte dayOfWeek = (byte) (Serial.read() - 48);
  446. byte dayOfMonth = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
  447. byte month = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
  448. byte year= (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
  449. Wire.beginTransmission(DS1307_I2C_ADDRESS);
  450. I2C_WRITE(zero);
  451. I2C_WRITE(decToBcd(second) & 0x7f); // 0 to bit 7 starts the clock
  452. I2C_WRITE(decToBcd(minute));
  453. I2C_WRITE(decToBcd(hour)); // If you want 12 hour am/pm you need to set
  454. // bit 6 (also need to change readDateDs1307)
  455. I2C_WRITE(decToBcd(dayOfWeek));
  456. I2C_WRITE(decToBcd(dayOfMonth));
  457. I2C_WRITE(decToBcd(month));
  458. I2C_WRITE(decToBcd(year));
  459. Wire.endTransmission();
  460. }
  461.  
  462. // Gets the date and time from the ds1307 and prints result
  463. void getDateDs1307()
  464. {
  465. // Reset the register pointer
  466. Wire.beginTransmission(DS1307_I2C_ADDRESS);
  467. I2C_WRITE(zero);
  468. Wire.endTransmission();
  469.  
  470. Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
  471.  
  472. // A few of these need masks because certain bits are control bits
  473. byte second = bcdToDec(I2C_READ() & 0x7f);
  474. byte minute = bcdToDec(I2C_READ());
  475. byte hour = bcdToDec(I2C_READ() & 0x3f); // Need to change this if 12 hour am/pm
  476. byte dayOfWeek = bcdToDec(I2C_READ());
  477. byte dayOfMonth = bcdToDec(I2C_READ());
  478. byte month = bcdToDec(I2C_READ());
  479. byte year = bcdToDec(I2C_READ());
  480.  
  481. if (hour < 10)
  482. Serial.print("0");
  483. Serial.print(hour, DEC);
  484. Serial.print(":");
  485. if (minute < 10)
  486. Serial.print("0");
  487. Serial.print(minute, DEC);
  488. Serial.print(":");
  489. if (second < 10)
  490. Serial.print("0");
  491. Serial.print(second, DEC);
  492. Serial.print(" ");
  493. Serial.print(Day[dayOfWeek]);
  494. Serial.print(", ");
  495. Serial.print(dayOfMonth, DEC);
  496. Serial.print(" ");
  497. Serial.print(Mon[month]);
  498. Serial.print(" 20");
  499. if (year < 10)
  500. Serial.print("0");
  501. Serial.println(year, DEC);
  502.  
  503. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement