I am doing a project which needs to read a modbus slave device, write some values on a OLED 0.96' display and some actions with outputs.
When I compile the program it shows Low memory available, stability problems may occur.
After running for about 10 minutes the program hangs.( Doesnt respond to inputs)
I tried commenting the OLED update part of the program and solving the low memory issue.
But I need to run the OLED too. My program is attached below.
Is there a better way to run the OLED without consuming much memory.
I am using a Arduino Genuinio/micro built for industrial environments. It programs as a normal arduino board.
unsigned long int up_reading = 0;
unsigned long int down_reading = 0;
unsigned int dir = 0;
...
attachInterrupt(digitalPinToInterrupt(7), blink, FALLING);
...
void blink()
{
if (dir == 1) up_reading += 5;
else if (dir == 0) down_reading += 5;
}
Variables that you share between normal program flow and an ISR need to be declared as volatile.
You would also need to disable interrupts before reading their value in normal program flow to ensure that you are reading them atomically. dir could be a byte.
void auto_mode_on() {
while (auto_mode == 1) {
...
while (stage == 0) {
...
}
while (stage == 1) {
...
}
}
Add some diagnostics to these while loops to ensure that you notice if you are getting stuck in them.
Overall, not the most maintainable code that I've ever seen.