Advertisement
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 * const wiadomosc [] PROGMEM = { msg_00, msg_01, msg_02, msg_03 };
- //--------------------------------------------------------------------------------------------------------------------------
- // 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
- 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[20];
- strcpy_P ( bufor, (char*) pgm_read_word (& wiadomosc[nr_wiadomosci] ));
- lcd.print( bufor );
- }
- void NapisNaSerial(byte nr_wiadomosci)
- {
- char bufor[30];
- strcpy_P ( bufor, (char*) pgm_read_word (& wiadomosc[nr_wiadomosci] ));
- Serial.print( bufor );
- }
- //------------------------------------------------------------------------------------------------------------------
- void setup() {
- // -------------------------------------------------// Initialize LCD display
- 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();
- // -------------------------------------------------// Initialize serial port
- Serial.begin(115200);
- delay(3000); // wait for console opening
- // ------------------------------------------------// Initialize SD card
- if (SD.begin(chipSelect)) // Check if SD card initiated properly
- {
- Serial.println("SD card: OK");
- lcd.setCursor(0, 0);
- lcd.print("SD card: OK");
- }
- else
- {
- Serial.println("no SD card");
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("no SD card");
- while(1); // if no card wait here forever
- }
- //---------------------------------------------------------------------------
- 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
- {
- Serial.println("Writing headers to csv.txt");
- myFile.println(" Data ");
- myFile.close();
- Serial.println("Headers written");
- }
- else {
- Serial.println("Error opening csv.txt");
- }
- }
- //--------------------------------------------------------------------------------
- // Read last date record from file if it exist
- 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 == ""){ // if date record doesn't exist set date of last use temporarly for 01.01.1970 00.00
- 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=startHour;
- MagStart.Minute = startMin;
- MagStart.Second = startSec;
- MagStart.Day = startDay;
- MagStart.Month = startMonth;
- MagStart.Year = startYear;
- // Takes the tmElement and converts it to a time_t variable. Which can now be compared to the current (now) unix time
- 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
- Serial.println("Previous time is: ");
- Serial.print(hour(previous));
- Serial.print(":");
- Serial.print(minute(previous));
- Serial.print(":");
- Serial.print(second(previous));
- Serial.print(" ");
- Serial.print(month(previous));
- Serial.print("/");
- Serial.print(day(previous));
- Serial.print("/");
- Serial.println(year(previous));
- Serial.println("****************************");
- if (! rtc.begin()) { // initiate RTC module
- Serial.println("Couldn't find RTC");
- while (1);
- }
- if (!rtc.isrunning()) {
- Serial.println("RTC is not runing!");
- 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));
- Serial.println("item given, disply turned off, I am waiting");
- czas = millis();
- previous = nowUniVM; // remember new time of last use
- myFile = SD.open("csv.txt", FILE_WRITE);
- if (myFile)
- {
- Serial.println("Writing to csv.txt");
- myFile.println(String(previous));
- myFile.close();
- }
- else
- {
- Serial.println("error opening csv.txt");
- }
- }
- 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();
- Serial.println("display off");
- }
- else{
- if (button.pressed()) { // if button pressed second time go to run motor stage
- state = ST_Motor_run;
- Display_on = true;
- Serial.println("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( "Last use: " );
- Serial.print( hours );
- Serial.print( ":" );
- Serial.print( minutes );
- Serial.print( ":" );
- Serial.print( seconds );
- Serial.println( " ago." );
- lcd.clear();
- lcd.backlight();
- lcd.setCursor(0, 0);
- lcd.print("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("First use");
- lcd.clear();
- lcd.backlight();
- lcd.setCursor(0, 0);
- lcd.print("First use");
- }
- }
- czas = millis();
- break;
- case ST_Init:
- if (millis() - czas > 10000) {
- state = ST_Wait;
- lcd.clear();
- lcd.noBacklight();
- Serial.println("Display off");
- }
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement