Bill, that's an impressive first sketch, congratulations! If you can find a copy of the freememory() library, you can try to display how much memory is available during your run.
Definitely, the compiler shows 1062 bytes of ram used on an UNO, add to that the 1024 bytes for the display buffer and you are already over the 2048 total bytes available. Extensive use of the F() macro will reduce that considerably, and as I said a text-only display library can eliminate the display buffer.
Oh so variables are already 0 after declaring them? I didn't know that
Only if they're global or static, and they haven't been initialised to some other value (also a hint)
How do I know if they're global? I'm still surprised I could access temperature inside the functions without sending it in as an argument
Start here, to learn some of the lingo:
A variable is global if it is defined outside any function.
It is static if it is defined as such.
Static variables may be local to a function, or local to a compilation unit
Words like "c++ vqariable scope" might provide something.
Or maybe not
Got it, thanks
No need for these lines. Pins default to INPUT and, when set to output, they default to LOW.
However, why are you not using INPUT_PULLUP for the button pins? That would save a couple of resistors.
Because when I made the hardware I wasn't aware there was an INPUT_PULLUP option
Now I have the hardware so the code needs to be such.
Good info about pin options though! Thanks
Ok folks so I have progress!
The code runs, finally
But, for some reason when it reads time in the main loop it reads hour as 41 and minute as 20? Same for any actual hour and minute... Can anyone see what's up with that?
Any variable which is only ever a true/false/yes/no, you can use bool instead of int, that might save the odd byte, and make the code more readable also.
When using Boolean (bool) variables, always choose names which makes it really clear what "true" means. For example "button_state" would be a bad choice of name, but "button_pressed" would be a good choice.
When using Boolean variables in if statements, don't put "== true" or "== false". That just looks dumb. Instead, just put "if (button_pressed) ..." or "if (!button_pressed) ...".
(The last 2 suggestions don't make the code more efficient or save memory, they just make the code more readable and easy to understand.)
That's the kind of weird thing that can happen when the Arduino has in fact run out of RAM memory. You don't get any kind of error message, just crazy stuff happening.
Keep going with the F() macro and other suggestions to save RAM.
This appears more than once. You could make this a function.
(This would save program memory, not RAM memory.)
As a test, go through your code and simply reduce every message by a factor of 2. E.g.
"Starting fan speed control"
becomes
"Fan spdctl strt"
That, combined with the other measures suggested, may prove illuminating. Messages are nice, but working is nicer.
Are those circles and "/" meant to represent "%" ? What's wrong with the "%" symbol?
(This is not meant to be a tirade of criticism, you asked for ways to reduce code and memory. I'm going though your code section by section).
If your code makes no other use of display.drawCircle(), then removing the last uses of it really will save program space. The C/C++ compiler/linker is intelligent enough to spot functions in libraries that are never used and not include them in the uploaded code.
Any reason to not consider something with lots more resources such as an ESP32?
About same cost but like 100X+ the resources of RAM and flash not to mention 32 bit vs 8 bit and clocked MUCH faster like 10-20X.
--- bill
I didn't like the look of it. Too strangely compressed. But good point about space and resource saving!
The school only had Arduinos, and this is my first contact with other microcontrollers than RPi. So it was really no choice.
Can you recommend a suitable one David?
I've so far made so much progress that it successfully runs on my Nano, which is great Thank you all for all your help guys!
A little off topic perhaps, but I really want to change PWM frequency also for the fan. It makes a bunch of noice on the default Hz..
From my research (link) it seems the better option is to setup Timer 1 to produce 20kHz PWM on pin 13. But I also read it "can have unintended consequences" so I'm a little bit scared
Maybe someone can help me figure out how to best write this? I'd rather not destroy my microcontroller by just trying myself if I can avoid it