Checkweigher code freezes after a few seconds

Hey there, I am trying to build a customized Checkweigher. The hardware I am using is as follows:

  1. Arduino Uno
  2. DFRobot LCD Shield Gravity: 1602 LCD Keypad Shield For Arduino - DFRobot
  3. Contech weighing scale

When I upload the code attached below, it works fine for a few minutes and then the entire code freezes, the buttons don't work, the weight does not update, I need to reset the entire code to make sure it works. Initially thought it was a memory issue so even reduced the no. of products and other stuff but nothing worked. Please help me out, I have a deadline.

Thank you.
wc4.8.zip (7.7 KB)
checkASCIILims.ino (764 Bytes)
checkWeight.ino (1.2 KB)
editScreen.ino (4.6 KB)
loadEEPROM.ino (1.7 KB)
PassChangeScreen.ino (1.3 KB)
readButtons.ino (872 Bytes)
Screens.ino (12.3 KB)
updateEEPROM.ino (1.4 KB)
wc4.8.ino (2.3 KB)

@jashshah999, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with your project :wink: See About the Installation & Troubleshooting category.

I see a number of warning: iteration N invokes undefined behavior (where N is a number) when I compile your code; they are more than likely the cause of your problem (but I did not dig deep into it. I suggest that you set compiler warnings (in file -> preferences in the IDE) to All.


C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\Screens.ino: In function 'bool pwScreen()':

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\Screens.ino:147:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

       if(number==password){

          ~~~~~~^~~~~~~~~~

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\Screens.ino:157:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

       if(number!=password){

          ~~~~~~^~~~~~~~~~

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\Screens.ino:177:1: warning: control reaches end of non-void function [-Wreturn-type]

 }

 ^

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\updateEEPROM.ino: In function 'updateEEPROM()':

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\updateEEPROM.ino:16:33: warning: iteration 20 invokes undefined behavior [-Waggressive-loop-optimizations]

     dtostrf(weights[prodIndex][0], 7, 1, tempBuf);

             ~~~~~~~~~~~~~~~~~~~~^

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\updateEEPROM.ino:14:34: note: within this loop

   for(byte prodIndex=0; prodIndex<21; prodIndex++){             //product lower lims

                         ~~~~~~~~~^~~

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\updateEEPROM.ino:18:65: warning: iteration 7 invokes undefined behavior [-Waggressive-loop-optimizations]

       EEPROM.update(prodIndex*7+byteIndex+221, tempBuf[byteIndex]);

                                                ~~~~~~~~~~~~~~~~~^

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\updateEEPROM.ino:17:36: note: within this loop

     for(byte byteIndex=0; byteIndex<8; byteIndex++){

                           ~~~~~~~~~^~

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\updateEEPROM.ino: In function 'loadEEPROM()':

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\loadEEPROM.ino:8:19: warning: iteration 8 invokes undefined behavior [-Waggressive-loop-optimizations]

       Serial.print(tempBuf[byteIndex]);

       ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\loadEEPROM.ino:5:36: note: within this loop

     for(byte byteIndex=0; byteIndex<10; byteIndex++){

                           ~~~~~~~~~^~~

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\loadEEPROM.ino:24:26: warning: iteration 20 invokes undefined behavior [-Waggressive-loop-optimizations]

     weights[prodIndex][0]=atof(tempBuf);

     ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\loadEEPROM.ino:17:34: note: within this loop

   for(byte prodIndex=0; prodIndex<21; prodIndex++){             //product lower lims

                         ~~~~~~~~~^~~

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\loadEEPROM.ino:22:19: warning: iteration 6 invokes undefined behavior [-Waggressive-loop-optimizations]

       Serial.print(tempBuf[byteIndex]);

       ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\loadEEPROM.ino:20:36: note: within this loop

     for(byte byteIndex=0; byteIndex<7; byteIndex++){

                           ~~~~~~~~~^~

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\loadEEPROM.ino:36:26: warning: iteration 20 invokes undefined behavior [-Waggressive-loop-optimizations]

     weights[prodIndex][1]=atof(tempBuf);

     ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\loadEEPROM.ino:29:34: note: within this loop

   for(byte prodIndex=0; prodIndex<21; prodIndex++){             //product upper lims

                         ~~~~~~~~~^~~

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\loadEEPROM.ino:34:19: warning: iteration 6 invokes undefined behavior [-Waggressive-loop-optimizations]

       Serial.print(tempBuf[byteIndex]);

       ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\loadEEPROM.ino:32:36: note: within this loop

     for(byte byteIndex=0; byteIndex<7; byteIndex++){

                           ~~~~~~~~~^~

Archiving built core (caching) in: C:\Users\sterretje\AppData\Local\Temp\arduino_cache_838555\core\core_arduino_avr_leonardo_4e759d11e131d256fe058d84342dcde1.a
C:\Users\sterretje\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.3\cores\arduino\main.cpp: In function 'main':

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\loadEEPROM.ino:36:26: warning: iteration 20 invokes undefined behavior [-Waggressive-loop-optimizations]

     weights[prodIndex][1]=atof(tempBuf);

                          ^

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\loadEEPROM.ino:29:34: note: within this loop

   for(byte prodIndex=0; prodIndex<21; prodIndex++){             //product upper lims

                                  ^

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\loadEEPROM.ino:34:19: warning: iteration 6 invokes undefined behavior [-Waggressive-loop-optimizations]

       Serial.print(tempBuf[byteIndex]);

                   ^

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\loadEEPROM.ino:32:36: note: within this loop

     for(byte byteIndex=0; byteIndex<7; byteIndex++){

                                    ^

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\loadEEPROM.ino:24:26: warning: iteration 20 invokes undefined behavior [-Waggressive-loop-optimizations]

     weights[prodIndex][0]=atof(tempBuf);

                          ^

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\loadEEPROM.ino:17:34: note: within this loop

   for(byte prodIndex=0; prodIndex<21; prodIndex++){             //product lower lims

                                  ^

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\loadEEPROM.ino:22:19: warning: iteration 6 invokes undefined behavior [-Waggressive-loop-optimizations]

       Serial.print(tempBuf[byteIndex]);

                   ^

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\loadEEPROM.ino:20:36: note: within this loop

     for(byte byteIndex=0; byteIndex<7; byteIndex++){

                                    ^

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\loadEEPROM.ino:8:19: warning: iteration 8 invokes undefined behavior [-Waggressive-loop-optimizations]

       Serial.print(tempBuf[byteIndex]);

                   ^

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\loadEEPROM.ino:5:36: note: within this loop

     for(byte byteIndex=0; byteIndex<10; byteIndex++){

                                    ^

C:\Users\sterretje\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.3\cores\arduino\WString.cpp:694:15: warning: iteration 20 invokes undefined behavior [-Waggressive-loop-optimizations]

  if (index >= len) { return; }

               ^

C:\Users\sterretje\Documents\Arduino\Forums\899048\wc4.8\wc4.8\loadEEPROM.ino:2:34: note: within this loop

   for(byte prodIndex=0; prodIndex<21; prodIndex++){           //load product names

                                  ^

Sketch uses 20020 bytes (69%) of program storage space. Maximum is 28672 bytes.
Global variables use 994 bytes (38%) of dynamic memory, leaving 1566 bytes for local variables. Maximum is 2560 bytes.

Hello,

You have many buffer overflows

For example in wc4.8.ino :

String productNames[20]
...
for(byte n=0; n<21; n++){   // <-- productNames can store 20 elements, arrays indices range from 0 to size-1, so this should be 20 not 21
  productNames[n]= "PRODUCTXYZ\0";

In many for loops you have the same error

Another example in loadEEPROM.ino :

char tempBuf[8];
...
for(byte byteIndex=0; byteIndex<10; byteIndex++){
      EEPROM.get(prodIndex*10+byteIndex, tempBuf[byteIndex]); // <-- tempBuf can store 8 elements but you try to store 10 elements in it

Fix ALL those buffer overflows and ALL compile warnings