Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //--------------------------------------------------------------------------------------------------------------------------
- // Dispenser V1.0
- // Designed by: Marek Lorenc
- //--------------------------------------------------------------------------------------------------------------------------
- #include <Bounce2.h>
- #include <Stepper.h>
- #include <Wire.h>
- #include <Time.h>
- #include <TimeLib.h>
- #include "RTClib.h"
- #include <LiquidCrystal_I2C.h> // Library for LCD
- #include <SD.h>
- #include <SPI.h>
- //--------------------------------------------------------------------------------------------------------------------------
- const char msg_00[] PROGMEM = "Dispenser";
- const char msg_01[] PROGMEM = "V 1.0";
- const char msg_02[] PROGMEM = "Made by:";
- const char msg_03[] PROGMEM = "Marek Lorenc";
- const char msg_04[] PROGMEM = "SD card: OK";
- const char msg_05[] PROGMEM = "no SD card";
- const char * const wiadomosc [] PROGMEM = { msg_00, msg_01, msg_02, msg_03, msg_04, msg_05 };
- const char msg_s00 [] PROGMEM = "SD card: OK";
- const char msg_s01 [] PROGMEM = "no SD card";
- const char msg_s02 [] PROGMEM = "Writing headers to csv.txt";
- const char msg_s03 [] PROGMEM = "Headers written";
- const char msg_s04 [] PROGMEM = "Error opening csv.txt";
- const char msg_s05 [] PROGMEM = "Previous time is: ";
- const char msg_s06 [] PROGMEM = "****************************";
- const char msg_s07 [] PROGMEM = "Couldn't find RTC";
- const char msg_s08 [] PROGMEM = "RTC is not runing!";
- const char msg_s09 [] PROGMEM = "item given, disply turned off, I am waiting";
- const char msg_s10 [] PROGMEM = "Writing to csv.txt";
- const char msg_s11 [] PROGMEM = "error opening csv.txt";
- const char msg_s12 [] PROGMEM = "display off";
- const char * const serialmsg [] PROGMEM = { msg_s00, msg_s01, msg_s02, msg_s03, msg_s04, msg_s05, msg_s06,
- msg_s07, msg_s08, msg_s09, msg_s10, msg_s11, msg_s12};
- //--------------------------------------------------------------------------------------------------------------------------
- // Set all variables
- File myFile;
- char fileName[] = "csv.txt";
- const int chipSelect = 10;
- RTC_DS1307 rtc;
- DateTime nowVM;
- time_t nowUniVM;
- time_t previous;
- tmElements_t MagStart;
- unsigned long czas; // time marker for milis()
- int krok; // stepper motor steps for 1/4 of rotation
- int dzisiaj; // quantity of items taken today
- int wczoraj; // quantity of items taken yesterday
- String strMinutes; // minutes
- boolean Display_on; // display on trigger
- String dataStr; // date in linux format read from file
- byte z=0; // position of the beginning of the last record in the file
- double stepsPerRevolution = 2048; // steps of full revolution
- Stepper myStepper(stepsPerRevolution, 8, 5, 9, 6); // stepper motor definition
- // Bounce2::Button button = Bounce2::Button();
- Button button = Button();
- #define button_pin 3 // button pin
- LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
- //------------------------------------------------------------------------------------------------------------------
- void NapisNaLcd(byte nr_wiadomosci)
- {
- char *bufor = new char [ strlen_P( pgm_read_word (& wiadomosc[nr_wiadomosci] ) ) ];
- strcpy_P ( bufor, (char*) pgm_read_word (& wiadomosc[nr_wiadomosci] ));
- lcd.print( bufor );
- delete bufor;
- }
- void NapisNaSerial(byte nr_wiadomosci)
- {
- char *bufor = new char [ strlen_P( pgm_read_word (& serialmsg[nr_wiadomosci] ) ) ];
- strcpy_P ( bufor, (char*) pgm_read_word (& serialmsg[nr_wiadomosci] ));
- Serial.println( bufor );
- delete bufor;
- }
- void LcdInit()
- {
- lcd.init(); // initialize the lcd
- lcd.backlight(); //lcd backlight on
- lcd.setCursor(3, 0); // move cursor to (0, 0)
- NapisNaLcd(0); // print message at (0, 0)
- lcd.setCursor(5, 1); // move cursor to (2, 1)
- NapisNaLcd(1); // print message at (2, 1)
- delay(2000); // wait 2s
- lcd.clear(); // clear lcd display
- lcd.setCursor(0, 0); //
- NapisNaLcd(2); // print message at (0, 0)
- lcd.setCursor(0, 1); // move cursor to (2, 1)
- NapisNaLcd(3); // print message at (2, 1)
- delay(2000);
- lcd.clear();
- }
- //------------------------------------------------------------------------------------------------------------------
- void setup()
- {
- LcdInit();
- Serial.begin(115200);
- delay(3000); // wait for console opening
- // ------------------------------------------------// Initialize SD card
- if (SD.begin(chipSelect)) // Check if SD card initiated properly
- {
- NapisNaSerial(0);
- lcd.setCursor(0, 0);
- NapisNaLcd(5);
- }
- else
- {
- NapisNaSerial(1);
- lcd.clear();
- lcd.setCursor(0, 0);
- NapisNaLcd(6);
- }
- //---------------------------------------------------------------------------
- if (!SD.exists("csv.txt"))
- { //check if fitle csv.txt exist
- myFile = SD.open("csv.txt", FILE_WRITE); // if not create it and write in column header
- if (myFile) // it opened OK
- {
- NapisNaSerial(2);
- myFile.println(" Data ");
- myFile.close();
- NapisNaSerial(3);
- }
- else NapisNaSerial(4);
- }
- //--------------------------------------------------------------------------------
- myFile = SD.open("csv.txt", FILE_READ);
- if (myFile.size()>12)
- {
- z = myFile.size()-12;
- myFile.seek(z);
- dataStr=myFile.readStringUntil('\n');
- }
- myFile.close();
- //-------------------------------------------------------------------------------
- if (dataStr == "")
- {
- //byte startHour = 00;
- //byte startMin = 00;
- //byte startSec = 0;
- //byte startDay = 1;
- //byte startMonth = 1;
- //int twoDigYear = 00; // Enter last two digits of year
- //byte startYear = twoDigYear ;
- MagStart.Hour= 00;
- MagStart.Minute = 00;
- MagStart.Second = 0;
- MagStart.Day = 1;
- MagStart.Month = 1;
- MagStart.Year = 00;
- previous = makeTime(MagStart);
- }
- else
- {
- previous = dataStr.toInt(); // if date record exist set it as date and time of last use in linux format
- }
- // Start message
- NapisNaSerial(6);
- Serial.print(hour(previous));
- Serial.print(F(":"));
- Serial.print(minute(previous));
- Serial.print(F(":"));
- Serial.print(second(previous));
- Serial.print(F(" "));
- Serial.print(month(previous));
- Serial.print(F("/"));
- Serial.print(day(previous));
- Serial.print(F("/"));
- Serial.println(year(previous));
- NapisNaSerial(7);
- if (! rtc.begin())
- { // initiate RTC module
- NapisNaSerial(8);
- //while (1);
- }
- if (!rtc.isrunning())
- {
- NapisNaSerial(9);
- rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- }
- button.attach(button_pin,INPUT_PULLUP);
- button.interval(50);
- button.setPressedState(LOW);
- krok = 512; // set 512 steps for 1/4 of rotation
- czas = millis(); // set time marker
- //---------------------------------------------------------------------------------------------------------------------
- //
- // HERE PROBLEM BEGINS
- //
- //---------------------------------------------------------------------------------------------------------------------
- // In this part of programm I would like to read file and count all records for yesterday and today
- myFile = SD.open("csv.txt", FILE_READ);
- nowVM = rtc.now(); // read current time from RTC
- nowUniVM = nowVM.unixtime(); // convert current time to Unix format
- String today = String(day(nowUniVM))+String(month(nowUniVM)) + String(year(nowUniVM)); // create string date of today ddmmyy
- String yesterday = String(day(nowUniVM)-1)+String(month(nowUniVM))+String(year(nowUniVM)); // create string date of yesterday ddmmyy
- dzisiaj=0;
- wczoraj=0;
- if (myFile.size()>12)
- { // if date records exist in file
- myFile.seek(14); // skip header line
- while (myFile.available()) // read all records one by one until the end of file
- {
- dataStr=myFile.readStringUntil('\n');
- String dat = String(day(dataStr.toInt()))+String(month(dataStr.toInt())) + String(year(dataStr.toInt())); // extract ddmmyy from each record
- if (dat == today)
- {
- dzisiaj++; // count todays records
- }
- if (dat == yesterday)
- {
- wczoraj++; // count yesterdays records
- }
- }
- }
- myFile.close();
- //-------------------------------------------------------------------------------
- }
- #define ST_Init 0 // initiation stage after power on
- #define ST_Wait 1 // waiting for first button press stage
- #define ST_Button_1 2 // button pressed first time waiting for second
- #define ST_Motor_run 3 // button pressed second time
- byte state = ST_Init;
- void loop() {
- // put your main code here, to run repeatedly:
- button.update();
- switch (state) {
- case ST_Motor_run:
- if (krok > 0) {
- // stepper motor rotation 1/4 of round
- if (Display_on == true) {
- lcd.clear();
- lcd.noBacklight();
- Display_on=false;
- }
- krok=krok-1;
- myStepper.step(1);
- delay(5);
- }
- else{
- // after rotation turn off motor, increase todays counter, display message on LCD and write new record in to file
- state = ST_Init;
- krok=512;
- dzisiaj=dzisiaj+1;
- digitalWrite(8,LOW);
- digitalWrite(9,LOW);
- digitalWrite(5,LOW);
- digitalWrite(6,LOW);
- lcd.clear();
- lcd.backlight();
- lcd.setCursor(0, 0);
- lcd.print("Today: " + String(dzisiaj));
- lcd.setCursor(0, 1);
- lcd.print("Yesterday: " + String(wczoraj));
- NapisNaSerial(10);
- czas = millis();
- previous = nowUniVM; // remember new time of last use
- myFile = SD.open("csv.txt", FILE_WRITE);
- if (myFile)
- {
- NapisNaSerial(11);
- myFile.println(String(previous));
- myFile.close();
- }
- else
- {
- NapisNaSerial(12);
- }
- }
- break;
- case ST_Button_1:
- if (millis() - czas > 10000) { // if button not pressed for 10s return to waiting stage
- state = ST_Wait;
- lcd.clear();
- lcd.noBacklight();
- NapisNaSerial(13);
- }
- else{
- if (button.pressed()) { // if button pressed second time go to run motor stage
- state = ST_Motor_run;
- Display_on = true;
- Serial.println(F("motor run"));
- }
- }
- break;
- case ST_Wait:
- if (button.pressed()) { // wait for button press
- state = ST_Button_1; // if pressed read current date and time count diference in hours and minutes since last use
- // display it on LCD screen
- // go to ST_Button_1 stage
- nowVM = rtc.now();
- nowUniVM = nowVM.unixtime();
- 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
- int32_t diff = nowUniVM - previous; // if that year is diferent count diference ....
- uint32_t hours = diff / 3600;
- uint8_t minutes = (diff / 60) % 60;
- uint8_t seconds = diff % 60;
- Serial.print( F("Last use: " ));
- Serial.print( hours );
- Serial.print( F(":" ));
- Serial.print( minutes );
- Serial.print( F(":" ));
- Serial.print( seconds );
- Serial.println( F(" ago." ));
- lcd.clear();
- lcd.backlight();
- lcd.setCursor(0, 0);
- lcd.print(F("Last use"));
- lcd.setCursor(0, 1);
- strMinutes=String(minutes);
- if (minutes<10) {
- strMinutes="0"+String(minutes);
- }
- lcd.print(String(hours)+"h "+ strMinutes +"min ago");
- }
- else{
- Serial.println(F("First use"));
- lcd.clear();
- lcd.backlight();
- lcd.setCursor(0, 0);
- lcd.print(F("First use"));
- }
- }
- czas = millis();
- break;
- case ST_Init:
- if (millis() - czas > 10000) {
- state = ST_Wait;
- lcd.clear();
- lcd.noBacklight();
- NapisNaSerial(13);
- }
- break;
- }
- }
Add Comment
Please, Sign In to add comment