Advertisement
PowerTGS440

mod_dynamic_bufor

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