Advertisement
PowerTGS440

modyfikacja4you

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