PowerTGS440

wygenerowany_mod

Feb 9th, 2021 (edited)
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.30 KB | None | 0 0
  1. //--------------------------------------------------------------------------------------------------------------------------
  2. // Dispenser V1.0
  3. // Designed by: Marek Lorenc
  4. //--------------------------------------------------------------------------------------------------------------------------
  5. #include <Bounce2.h>
  6. #include <Stepper.h>
  7. #include <Wire.h>
  8. #include <Time.h>
  9. #include <TimeLib.h>
  10. #include "RTClib.h"
  11. #include <LiquidCrystal_I2C.h> // Library for LCD
  12. #include <SD.h>
  13. #include <SPI.h>
  14.  
  15. //--------------------------------------------------------------------------------------------------------------------------
  16. const char msg_00[] PROGMEM = "Dispenser";
  17. const char msg_01[] PROGMEM = "V 1.0";
  18. const char msg_02[] PROGMEM = "Made by:";
  19. const char msg_03[] PROGMEM = "Marek Lorenc";
  20. const char msg_04[] PROGMEM = "SD card: OK";
  21. const char msg_05[] PROGMEM = "no SD card";
  22. const char * const wiadomosc [] PROGMEM = { msg_00, msg_01, msg_02, msg_03, msg_04, msg_05 };
  23.  
  24. const char msg_s00 [] PROGMEM = "SD card: OK";
  25. const char msg_s01 [] PROGMEM = "no SD card";
  26. const char msg_s02 [] PROGMEM = "Writing headers to csv.txt";
  27. const char msg_s03 [] PROGMEM = "Headers written";
  28. const char msg_s04 [] PROGMEM = "Error opening csv.txt";
  29. const char msg_s05 [] PROGMEM = "Previous time is: ";
  30. const char msg_s06 [] PROGMEM = "****************************";
  31. const char msg_s07 [] PROGMEM = "Couldn't find RTC";
  32. const char msg_s08 [] PROGMEM = "RTC is not runing!";
  33. const char msg_s09 [] PROGMEM = "item given, disply turned off, I am waiting";
  34. const char msg_s10 [] PROGMEM = "Writing to csv.txt";
  35. const char msg_s11 [] PROGMEM = "error opening csv.txt";
  36. const char msg_s12 [] PROGMEM = "display off";
  37.  
  38. const char * const serialmsg [] PROGMEM = { msg_s00, msg_s01, msg_s02, msg_s03, msg_s04, msg_s05, msg_s06,
  39. msg_s07, msg_s08, msg_s09, msg_s10, msg_s11, msg_s12};
  40.  
  41.  
  42.  
  43. //--------------------------------------------------------------------------------------------------------------------------
  44. // Set all variables
  45. File myFile;
  46. char fileName[] = "csv.txt";
  47. const int chipSelect = 10;
  48. RTC_DS1307 rtc;
  49. DateTime nowVM;
  50. time_t nowUniVM;
  51. time_t previous;
  52. tmElements_t MagStart;
  53.  
  54. unsigned long czas; // time marker for milis()
  55. int krok; // stepper motor steps for 1/4 of rotation
  56. int dzisiaj; // quantity of items taken today
  57. int wczoraj; // quantity of items taken yesterday
  58. String strMinutes; // minutes
  59. boolean Display_on; // display on trigger
  60. String dataStr; // date in linux format read from file
  61. byte z=0; // position of the beginning of the last record in the file
  62. double stepsPerRevolution = 2048; // steps of full revolution
  63. Stepper myStepper(stepsPerRevolution, 8, 5, 9, 6); // stepper motor definition
  64. // Bounce2::Button button = Bounce2::Button();
  65. Button button = Button();
  66. #define button_pin 3 // button pin
  67.  
  68. LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
  69. //------------------------------------------------------------------------------------------------------------------
  70. void NapisNaLcd(byte nr_wiadomosci)
  71. {
  72. char *bufor = new char [ strlen_P( pgm_read_word (& wiadomosc[nr_wiadomosci] ) ) ];
  73. strcpy_P ( bufor, (char*) pgm_read_word (& wiadomosc[nr_wiadomosci] ));
  74. lcd.print( bufor );
  75. delete bufor;
  76. }
  77.  
  78. void NapisNaSerial(byte nr_wiadomosci)
  79. {
  80. char *bufor = new char [ strlen_P( pgm_read_word (& serialmsg[nr_wiadomosci] ) ) ];
  81. strcpy_P ( bufor, (char*) pgm_read_word (& serialmsg[nr_wiadomosci] ));
  82. Serial.println( bufor );
  83. delete bufor;
  84. }
  85.  
  86. void LcdInit()
  87. {
  88. lcd.init(); // initialize the lcd
  89. lcd.backlight(); //lcd backlight on
  90. lcd.setCursor(3, 0); // move cursor to (0, 0)
  91. NapisNaLcd(0); // print message at (0, 0)
  92. lcd.setCursor(5, 1); // move cursor to (2, 1)
  93. NapisNaLcd(1); // print message at (2, 1)
  94. delay(2000); // wait 2s
  95. lcd.clear(); // clear lcd display
  96. lcd.setCursor(0, 0); //
  97. NapisNaLcd(2); // print message at (0, 0)
  98. lcd.setCursor(0, 1); // move cursor to (2, 1)
  99. NapisNaLcd(3); // print message at (2, 1)
  100. delay(2000);
  101. lcd.clear();
  102. }
  103.  
  104. //------------------------------------------------------------------------------------------------------------------
  105. void setup()
  106. {
  107. LcdInit();
  108.  
  109. Serial.begin(115200);
  110. delay(3000); // wait for console opening
  111.  
  112. // ------------------------------------------------// Initialize SD card
  113. if (SD.begin(chipSelect)) // Check if SD card initiated properly
  114. {
  115. NapisNaSerial(0);
  116. lcd.setCursor(0, 0);
  117. NapisNaLcd(5);
  118. }
  119. else
  120. {
  121. NapisNaSerial(1);
  122. lcd.clear();
  123. lcd.setCursor(0, 0);
  124. NapisNaLcd(6);
  125. }
  126. //---------------------------------------------------------------------------
  127. if (!SD.exists("csv.txt"))
  128. { //check if fitle csv.txt exist
  129.  
  130. myFile = SD.open("csv.txt", FILE_WRITE); // if not create it and write in column header
  131. if (myFile) // it opened OK
  132. {
  133. NapisNaSerial(2);
  134. myFile.println(" Data ");
  135. myFile.close();
  136. NapisNaSerial(3);
  137. }
  138. else NapisNaSerial(4);
  139. }
  140. //--------------------------------------------------------------------------------
  141. myFile = SD.open("csv.txt", FILE_READ);
  142. if (myFile.size()>12)
  143. {
  144. z = myFile.size()-12;
  145. myFile.seek(z);
  146. dataStr=myFile.readStringUntil('\n');
  147. }
  148. myFile.close();
  149. //-------------------------------------------------------------------------------
  150. if (dataStr == "")
  151. {
  152. //byte startHour = 00;
  153. //byte startMin = 00;
  154. //byte startSec = 0;
  155. //byte startDay = 1;
  156. //byte startMonth = 1;
  157. //int twoDigYear = 00; // Enter last two digits of year
  158. //byte startYear = twoDigYear ;
  159. MagStart.Hour= 00;
  160. MagStart.Minute = 00;
  161. MagStart.Second = 0;
  162. MagStart.Day = 1;
  163. MagStart.Month = 1;
  164. MagStart.Year = 00;
  165.  
  166. previous = makeTime(MagStart);
  167. }
  168. else
  169. {
  170. previous = dataStr.toInt(); // if date record exist set it as date and time of last use in linux format
  171. }
  172.  
  173. // Start message
  174. NapisNaSerial(6);
  175. Serial.print(hour(previous));
  176. Serial.print(F(":"));
  177. Serial.print(minute(previous));
  178. Serial.print(F(":"));
  179. Serial.print(second(previous));
  180. Serial.print(F(" "));
  181. Serial.print(month(previous));
  182. Serial.print(F("/"));
  183. Serial.print(day(previous));
  184. Serial.print(F("/"));
  185. Serial.println(year(previous));
  186. NapisNaSerial(7);
  187.  
  188. if (! rtc.begin())
  189. { // initiate RTC module
  190. NapisNaSerial(8);
  191. //while (1);
  192. }
  193.  
  194. if (!rtc.isrunning())
  195. {
  196. NapisNaSerial(9);
  197. rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  198. }
  199.  
  200. button.attach(button_pin,INPUT_PULLUP);
  201. button.interval(50);
  202. button.setPressedState(LOW);
  203.  
  204. krok = 512; // set 512 steps for 1/4 of rotation
  205. czas = millis(); // set time marker
  206.  
  207. //---------------------------------------------------------------------------------------------------------------------
  208. //
  209. // HERE PROBLEM BEGINS
  210. //
  211. //---------------------------------------------------------------------------------------------------------------------
  212. // In this part of programm I would like to read file and count all records for yesterday and today
  213.  
  214. myFile = SD.open("csv.txt", FILE_READ);
  215.  
  216. nowVM = rtc.now(); // read current time from RTC
  217. nowUniVM = nowVM.unixtime(); // convert current time to Unix format
  218.  
  219. String today = String(day(nowUniVM))+String(month(nowUniVM)) + String(year(nowUniVM)); // create string date of today ddmmyy
  220. String yesterday = String(day(nowUniVM)-1)+String(month(nowUniVM))+String(year(nowUniVM)); // create string date of yesterday ddmmyy
  221. dzisiaj=0;
  222. wczoraj=0;
  223.  
  224. if (myFile.size()>12)
  225. { // if date records exist in file
  226. myFile.seek(14); // skip header line
  227. while (myFile.available()) // read all records one by one until the end of file
  228. {
  229. dataStr=myFile.readStringUntil('\n');
  230. String dat = String(day(dataStr.toInt()))+String(month(dataStr.toInt())) + String(year(dataStr.toInt())); // extract ddmmyy from each record
  231.  
  232. if (dat == today)
  233. {
  234. dzisiaj++; // count todays records
  235. }
  236. if (dat == yesterday)
  237. {
  238. wczoraj++; // count yesterdays records
  239. }
  240. }
  241. }
  242. myFile.close();
  243.  
  244. //-------------------------------------------------------------------------------
  245. }
  246.  
  247. #define ST_Init 0 // initiation stage after power on
  248. #define ST_Wait 1 // waiting for first button press stage
  249. #define ST_Button_1 2 // button pressed first time waiting for second
  250. #define ST_Motor_run 3 // button pressed second time
  251.  
  252. byte state = ST_Init;
  253.  
  254. void loop() {
  255. // put your main code here, to run repeatedly:
  256.  
  257. button.update();
  258.  
  259. switch (state) {
  260.  
  261. case ST_Motor_run:
  262.  
  263. if (krok > 0) {
  264. // stepper motor rotation 1/4 of round
  265. if (Display_on == true) {
  266. lcd.clear();
  267. lcd.noBacklight();
  268. Display_on=false;
  269. }
  270. krok=krok-1;
  271. myStepper.step(1);
  272. delay(5);
  273. }
  274. else{
  275. // after rotation turn off motor, increase todays counter, display message on LCD and write new record in to file
  276. state = ST_Init;
  277. krok=512;
  278. dzisiaj=dzisiaj+1;
  279. digitalWrite(8,LOW);
  280. digitalWrite(9,LOW);
  281. digitalWrite(5,LOW);
  282. digitalWrite(6,LOW);
  283. lcd.clear();
  284. lcd.backlight();
  285. lcd.setCursor(0, 0);
  286. lcd.print("Today: " + String(dzisiaj));
  287. lcd.setCursor(0, 1);
  288. lcd.print("Yesterday: " + String(wczoraj));
  289. NapisNaSerial(10);
  290. czas = millis();
  291. previous = nowUniVM; // remember new time of last use
  292.  
  293. myFile = SD.open("csv.txt", FILE_WRITE);
  294.  
  295. if (myFile)
  296. {
  297. NapisNaSerial(11);
  298. myFile.println(String(previous));
  299. myFile.close();
  300. }
  301. else
  302. {
  303. NapisNaSerial(12);
  304. }
  305. }
  306. break;
  307.  
  308. case ST_Button_1:
  309.  
  310. if (millis() - czas > 10000) { // if button not pressed for 10s return to waiting stage
  311. state = ST_Wait;
  312. lcd.clear();
  313. lcd.noBacklight();
  314. NapisNaSerial(13);
  315. }
  316. else{
  317. if (button.pressed()) { // if button pressed second time go to run motor stage
  318. state = ST_Motor_run;
  319. Display_on = true;
  320. Serial.println(F("motor run"));
  321. }
  322. }
  323. break;
  324.  
  325. case ST_Wait:
  326.  
  327. if (button.pressed()) { // wait for button press
  328. state = ST_Button_1; // if pressed read current date and time count diference in hours and minutes since last use
  329. // display it on LCD screen
  330. // go to ST_Button_1 stage
  331. nowVM = rtc.now();
  332. nowUniVM = nowVM.unixtime();
  333. if (year(previous) != 1970) { // if the year of previous date is 1970 it means that it is first time of use and data file is empty
  334. int32_t diff = nowUniVM - previous; // if that year is diferent count diference ....
  335. uint32_t hours = diff / 3600;
  336. uint8_t minutes = (diff / 60) % 60;
  337. uint8_t seconds = diff % 60;
  338. Serial.print( F("Last use: " ));
  339. Serial.print( hours );
  340. Serial.print( F(":" ));
  341. Serial.print( minutes );
  342. Serial.print( F(":" ));
  343. Serial.print( seconds );
  344. Serial.println( F(" ago." ));
  345. lcd.clear();
  346. lcd.backlight();
  347. lcd.setCursor(0, 0);
  348. lcd.print(F("Last use"));
  349. lcd.setCursor(0, 1);
  350. strMinutes=String(minutes);
  351. if (minutes<10) {
  352. strMinutes="0"+String(minutes);
  353. }
  354. lcd.print(String(hours)+"h "+ strMinutes +"min ago");
  355. }
  356. else{
  357.  
  358. Serial.println(F("First use"));
  359. lcd.clear();
  360. lcd.backlight();
  361. lcd.setCursor(0, 0);
  362. lcd.print(F("First use"));
  363. }
  364. }
  365. czas = millis();
  366. break;
  367.  
  368. case ST_Init:
  369. if (millis() - czas > 10000) {
  370. state = ST_Wait;
  371. lcd.clear();
  372. lcd.noBacklight();
  373. NapisNaSerial(13);
  374. }
  375. break;
  376.  
  377. }
  378.  
  379. }
Add Comment
Please, Sign In to add comment