code optimalization needed :)

Hello all. I am a very beginner Arduino developer and to be honest this is my first project. I create it on examples from the internet, so it’s a bit chaotic and I hit the wall. in my Arduino UNO the memory ran out :(, unfortunately at this stage I am not able to deal with it. Maybe there will be someone who could look at my program and correct the novice’s errors? I will be very grateful.

dispenser_state_machine.ino (13.9 KB)

hat is the program intended to do ?

Start with the simple stuff

Make sure that all variables are of the appropriate type, ie, are you using ints to hold values that are never greater than 255 for which a byte would be sufficient ?

Use the F() macro when printing strings so that they are stored in program memory
eg

Serial.println(F("no SD card"));

lcd.print(F("Made by:"));

It is a little dispenser intended to help my wife to drop smoking :smiley:
It give one cigarete and register it, then shows on the screen how long time ago was last one and, when new one is taken it shows how many was today and compare it to yesterday.

Is there any chance of using EEPROM to hold the data ?
I ask because the SD buffer uses a lot of memory

Don't use Strings, particularly as you are already short of memory. Consider zero terminated arrays of chars (aka C style strings) instead

I used SD card because I already have it, and it was easiest, might in the future I can swap to eprom.
I have had some issues with char data types, the compiler always was complayning for something :frowning:

I have had some issues with char data types, the compiler always was complayning for something :frowning:

So fix it !

I have changed all printing lines to use sugested F() macro, and now compiler stops at marked line and shows error:
initializer fails to determine size of ‘__c’

strMinutes=String(minutes);
if (minutes<10) {
strMinutes=“0”+String(minutes);
}
lcd.print(F(String(hours)+" "+ strMinutes +“min ago”)); ← here
}
else{

Serial.println(F(“First use”));

Are hours or strMinutes variables that would be fixed (flash) memory?
I think the line probably needs to be broken up into F() and variable stuff.

The F() macro only works with strings constants ie text in quotes

In any case, I repeat the advice to use strings instead of Strings anyway

Great thanks to you, using F() macros only in string printing lines solved the problem for now, I am going to read how to properly use char string type, and definitely I will apply that in my project.
But now if I can I would like to ask you if I can for another advice.
As you can see I am operating on time stamps many times, I did find that linux time format is easiest for me. But I have problem to do procedure which will check today’s date and then read file check each record and compare it to current date and then count all record from yesterday and al records from today. Might can you help me how to do that?

mareckil:
As you can see I am operating on time stamps many times, I did find that linux time format is easiest for me. But I have problem to do procedure which will check today’s date and then read file check each record and compare it to current date and then count all record from yesterday and al records from today. Might can you help me how to do that?

The Time library makes that fairly easy.

previousMidnight(time_t) will give you the unix time at the start of today.

The pre-defined constant SECS_PER_DAY can be subtracted from previousMidnight() of today to give the start of yesterday.

Then it is just a process of comparing to see if a given time is between those two times.

I have changed all printing lines to use sugested F() macro, and now compiler stops at marked line and shows error:
initializer fails to determine size of ‘__c’

strMinutes=String(minutes);
if (minutes<10) {
strMinutes=“0”+String(minutes);
}
lcd.print(F(String(hours)+" "+ strMinutes +“min ago”)); ← here
}
else{

You can break that up into separate print() statements, and let print() do the conversion from numeric values to text. When printing a single character, it can be a bit more efficient to use single quotes.

          //strMinutes = String(minutes);
          //if (minutes < 10) {
          //  strMinutes = "0" + String(minutes);
          //}
          //lcd.print(String(hours) + " " + strMinutes + "min ago");
          lcd.print(hours);
          lcd.print(' ');
          if (minutes < 10) {
            lcd.print('0');
          }
          lcd.print(minutes);
          lcd.print(F("min ago"));

That machine better have a pretty thick steel casing.

-jim lee

jimLee:
That machine better have a pretty thick steel casing.

-jim lee

C'mon, it won't fail and refuse to dispense! :slight_smile: But seriously, the OP device actually only counts smokes, doesn't regulate them.

I use to do it by keeping the packs on the table. It's easier to count by packs.

aarg:
I use to do it by keeping the packs on the table. It's easier to count by packs.

:astonished:

the packs are easy to throw away, the machine will stand on the table and clearly show and remind frequency and quantity, every time she take another cigarete, I hope this will make her aware how much she smokes.

@mareckil I take it you've never been hooked on cigarettes?

-jim lee

mareckil:
the packs are easy to throw away, the machine will stand on the table and clearly show and remind frequency and quantity, every time she take another cigarete, I hope this will make her aware how much she smokes.

No doubt she does know how much she smokes but, nicotine is as addictive as heroin, cocaine or amphetamines, and for most people more addictive than alcohol. The primary thing is for her to *want * to quit.