Arduino crashes after a while

Hi to all. I’ve finished a project trying to monitor some operations for my coffee machine. Using a hall sensor and an OLED screen, I created a brew timer and a coffee counter.

The problem is that after 2-3 uses or so, the program crashes. Also sometimes when the actions triggered some pixels “brake” for less than a sec and then gets back to normal without any problem on the printed items on the display.

I’m a begginer to all these and I don’t know much of coding but many to combine ready projects and logical functions. Can you see any flaws on the code that could explain that chrash?

Any help would be apreciated… Please be kind :slight_smile:

#include <elapsedMillis.h>

#include "U8g2lib.h"

#include <EEPROM.h>

U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /*reset=*/ U8X8_PIN_NONE,/* clock=*/ SCL, /* data=*/ SDA);

const int numReadings = 10;
int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int average = 0.0;                // the average
int hallSensor = A2; // for the hall sensor
float rawValue = 0.0;               // Raw ADC Reading
float zeroLevel = 522;  // Adjust value as needed to get zero rawValue output with no magnetic field.

int LED = 7; // for the led strip

unsigned int value; // for the coffees
unsigned int coffees = 0; // for the coffees
int eeAddress = 0; // for the coffees


unsigned long time = 0; // for the timer
elapsedMillis timeElapsed; // for the timer

unsigned long currentMillis = 0; // for the timer and coffee reading
unsigned long previousCoffeeReadMillis = 0;   // for the timer and coffee reading
const int CoffeeReadInterval = 1000; // number of millisecs between coffee writing
unsigned long previoussmoothingMillis = 0;   // for the timer and coffee reading
const int smoothingInterval = 1; // number of millisecs between coffee reading




void setup() {

  u8g2.begin();

  // set input and output
  pinMode(LED, OUTPUT);
  pinMode (hallSensor, INPUT);
  Serial.begin(9600);

  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
  // read coffees for first time
  coffees = EEPROM.get(0, coffees);

}


void draw() {
  u8g2.drawLine(70, 0, 70, 70);
  u8g2.drawLine(0, 32, 128, 32);
  u8g2.drawFrame(0, 0, 128, 64);


  u8g2.setFont(u8g2_font_helvB10_tf);
  u8g2.drawStr(10, 21, "Gaggia");

  u8g2.setFont(u8g2_font_helvB18_tf);
  u8g2.setCursor(77, 25);
  u8g2.print(time);

  u8g2.setFont(u8g2_font_profont12_tf);
  u8g2.drawStr(107, 25, "sec");

  u8g2.setFont(u8g2_font_profont12_tf);
  u8g2.drawStr(17, 43, "Coffees");


  u8g2.setFont(u8g2_font_profont15_tf);
  u8g2.setCursor(15, 58);
  u8g2.print(value * 0.018);

  u8g2.setFont(u8g2_font_profont15_tf);
  u8g2.drawStr(50, 58, "kg");

  u8g2.setFont(u8g2_font_profont12_tf);
  u8g2.drawStr(78, 43, "Coffees");

  u8g2.setFont(u8g2_font_profont15_tn);
  u8g2.setCursor(85, 58);
  u8g2.print(value);

}

void loop() {
  u8g2.firstPage();
  currentMillis = millis();   // capture the latest value of millis()
  smoothingLEDtimer();
  readCoffees();
  do {
    draw();
  } while (u8g2.nextPage() );

}

void  smoothingLEDtimer() {
  if (currentMillis - previoussmoothingMillis >= smoothingInterval) {



    //_________________________Reading magnetic field_______________________

    rawValue = analogRead (hallSensor) - zeroLevel; // Output normalized to '0' with no field present
    Serial.print ("Reading Raw: ");
    Serial.println (rawValue);
    Serial.print ("Average: ");
    Serial.println (average);


    //____________________________Smoothing_____________________________________

    //subtract the last reading:
    total = total - readings[readIndex];
    // read from the sensor:
    readings[readIndex] = analogRead(hallSensor);
    // add the reading to the total:
    total = total + abs(readings[readIndex]); //convert negative reading to positive
    // advance to the next position in the array:
    readIndex = readIndex + 1;

    // if we're at the end of the array...
    if (readIndex >= numReadings) {
      // ...wrap around to the beginning:
      readIndex = 0;
    }

    // calculate the average:
    average = total / numReadings - 521.5;
    // send it to the computer as ASCII digits




    //____________Timer - LED___________________________________________________________________

    if (average > 1) {
      time = timeElapsed / 1000;  //Start timer
      digitalWrite(LED, HIGH);  //Enable LED
    }
    else {
      timeElapsed = 0;  // zero timer
      digitalWrite(LED, LOW);   //Disable LED
    }

    previoussmoothingMillis += smoothingInterval;
  }
}


void  readCoffees() {

  if (currentMillis - previousCoffeeReadMillis >= CoffeeReadInterval) {
    //____________Read Coffees___________________________________________________________________
    coffees = EEPROM.get(0, coffees);

    if (average > 1 & time == 16) {
      coffees = coffees + 1;
      EEPROM.put(0, coffees);

      value = EEPROM.get(0, coffees);


      if (eeAddress == EEPROM.length()) {
        eeAddress = 0;
      }

    }
    previousCoffeeReadMillis += CoffeeReadInterval;
  }
}

Your brief description of the problem leads me so suspect there is a problem with the power to your Arduino system. Please describe the power supply and show a schematic or block diagram of the system.

Paul

Paul_KD7HB:
Your brief description of the problem leads me so suspect there is a problem with the power to your Arduino system. Please describe the power supply and show a schematic or block diagram of the system.

Paul

Oh my dear friend I really don't know how to do the schematic

I'm using a nano Atmel atmega 328p xplained soldered in a board
Powered using a 5V USB charger (from mobile phone)

The OLED is a 1.3 I2C connected
5V
GND
SCL to A5
SDA to A4

The hall sensor is connected using a 2MOhm resistor between 5V and Data
5V
GND
Data to A2

A LED strip connected
5V to D7
GND

all the connections are made with jumper cables to soldered pins on the board

sport_billy:
Oh my dear friend I really don't know how to do the schematic

Have You heard about pen and paper?
What kind of power supply are You using? Nothing visible on that screen shot.

sport_billy:
Oh my dear friend I really don't know how to do the schematic

Please, no Fritzing pictures. Pretty Fritzing pictures are useless for engineering purposes.

My gift to you-

I see one major issue. There is no power supply. I wouldn’t even start, nor produce even one cup.

Railroader:
Have You heard about pen and paper?
What kind of power supply are You using? Nothing visible on that screen shot.

Yes it was easy to do it but I thought you wanted an full schematic like the ones I've seen.

If you were reading the text above the picture that you don't approve you would see that I mention a 5V wall charger from a mobile phone

SteveMann:
Please, no Fritzing pictures. Pretty Fritzing pictures are useless for engineering purposes.

My gift to you-

I could definitely could do a hand schematic but not so simple like that.

So I really thank you

Did you understand something that it's not right?

sport_billy:
Yes it was easy to do it but I thought you wanted an full schematic like the ones I've seen.

If you were reading the text above the picture that you don't approve you would see that I mention a 5V wall charger from a mobile phone

What is data for that charger, regarding current?
Have You measured the voltage at the controller after it has crashed?
Powerbanks do cut off if the current consumtion goes too low. I'm uncertain about chargers in this respect.

I found the solution!
It was the refresh of OLED that was on the loop without any delay. That was crashing the system!
Thank you all of you guys that spent your time and thinking.

I have it under testing now and so far seems to work just fine. I'll let you know if anything changes

P.S.: Try not to be so hard with people in general, especially with those that say from the beginning that they are beginners.

I'm a Civil Engineer and I do all this for fun. All I wanted was 5 minutes of your time in order not to spend 30 minutes of mine just looking around.

But I see that some people just can't help it!

Thank you for the good advice