Serial.println errors ** fixed

hey all. yesterday i discovered a problem that doesn't make any since to me. If i attempt to serial print one of the values from my code the variable of the print part shows 16 squares. I listed it as an Int as the values are limited to 3 digits.

Here's the kicker of the situation. If i apply a 2 more serial prints on other values in my code just a few lines down, then all of the serial prints are correct as it should be, but it's not working on that one value by itself. but when it messes up, the code itself works normally, but won't print values, and i've never seen it do this before.

Here is the section of code where the prints are located.

              
  int pumpReading = analogRead(pumpPin);                                                
  long pumpPeriod = map(pumpReading, 1023, 0, minPump, maxPump);         

newPumpPeriod = pumpPeriod;

if ( abs (newPumpPeriod - oldPumpPeriod) > 2)        {monitorPressure();}
          
int currentPressure = (analogRead(pressurePin)-SensorOffset);              // Pressure check
  
randomPump = random(200, pumpPeriod);                                   // Random pump time on generation


        
          if (currentPressure > randomPump){
               randomPump = random(currentPressure, pumpPeriod);            
          }

Serial.print("randomPump  ");                  //  won't print values by itself
Serial.println(randomPump);  

digitalWrite(pumpRelay, LOW);                                               
pumpMillisTime = millis();  
pressureReading = (analogRead(pressurePin)-SensorOffset);
             
              while (analogRead(pressurePin)-SensorOffset < randomPump) {         // RANDOM PUMP
                    fillMillisTime = millis();
                    inflateTime = fillMillisTime - pumpMillisTime;

                       if (inflateTime > noPressureTime){
                              oldPressureReading = (analogRead(pressurePin)-SensorOffset);
                                      if (oldPressureReading < pressureReading)  { noPressure(); } 
                        }
    
                       
                                    if (inflateTime >= pumpErrorTime){
                                          digitalWrite(pumpRelay, HIGH); 
                                          digitalWrite(valveRelay, LOW);

                                       for (int c = 0; c <= 2; c++){
                            display.printFixedN (0,  48, "Pumps Go", STYLE_NORMAL, FONT_SIZE_2X);
                            delay(500);
                            display.printFixedN (0,  48, "          ", STYLE_NORMAL, FONT_SIZE_2X);
                            delay(250);
                            display.printFixedN (0,  48, "TimeERROR", STYLE_NORMAL, FONT_SIZE_2X);
                            delay(500);
                            display.printFixedN (0,  48, "          ", STYLE_NORMAL, FONT_SIZE_2X);
                            delay(250);                            
                            break;
                                       }
                        digitalWrite(valveRelay, HIGH); 
                        break;              
                       }               
                }
                  
digitalWrite(pumpRelay, HIGH);                                                 


          int delayReading = analogRead(delayPin);                                             
          long pumpDelayPeriod = map(delayReading, 1023, 0, 1000, 60000);                    


long randomDelay = random(1000, pumpDelayPeriod);                         
      delay(randomDelay); 



checkButton();

randomValve = random(100, pumpPeriod/.75);                                        

Serial.print("random valve  ");                  //  prints fine
Serial.println(randomValve); 


      digitalWrite(valveRelay, LOW);                                                   
              delay(randomValve);
           digitalWrite(valveRelay, HIGH);                                             
 
          int valveReading = analogRead(valvePin);                                      
          long valveTimePeriod = map(valveReading, 1023, 0, 1000, 30000);
 
longDelay = random(500, valveTimePeriod);                                          
      delay(longDelay);

Serial.print("long delay  ");                  //  prints fine
Serial.println(longDelay); 

oldPumpPeriod = pumpPeriod; 
display.printFixedN (0,  48, "          ", STYLE_NORMAL, FONT_SIZE_2X);

}

any idea's on what could be causing something like this to happen?

I also just discovered that if i remove the Serial.print("randomPump "); and only try to do Serial.println(randomPump); then doing that completely glitches the code and messes up the code farther on the page where randomPump is called.

maybe you forgot Serial.begin(9600);.
It would help us a lot if you post the whole code.
Greetings Frenik

I didn't forget it. Rest of the code prints correctly, except when i try to print that single value, by itself, and then i get boxes in place of the value.

In this reason I have no clue so far.
But it would help us a lot if you could take a picture of the serial output and upload it here.
Greetings Frenik

Please post ALL the code.

The first pic is trying to print only the randomPump variable, ignore the error in the middle, that was my mistake.

The second pic is printing 2 variables, and it works fine.

but like i was saying, in the first case, the code works fine but doesn't print correctly. If i remove the Serial.print("randomPump ");, and only try to print the variable, it not only doesn't print it, it will sometimes ignore the randomPump variable all together which messes up whenever that variable is called later in the code.

One thing i'm gonna try, which i haven't is to restart my computer and arduino and see if there is some type of communication error that is messing things up.

It's possible the randomPump variable is being overwritten in memory somehow... but without ALL the code it's hard to say.

Restarting the computer didn't help. but after more testing, i did figure out that if i Serial.println any other variable along with the randomPump, no matter what it is, then both variables print fine. but it wont allow me to print only that one variable.

I've double checked, and there's really no way that randomPump can be overwritten. where i'm trying to print is the first place that it's called and generated as a random number. And then it's used right after that as a target pressure for my air pump to reach.

I suppose that it could be some type of glitch with the arduino itself, that value normally isn't printed, but was being used to monitor the numbers to fine tune the code. But when the code is finished, it's going to be removed so it won't be an issue. it was just something i haven't seen before.

Here's all of the variables and function that is messing up. It's the only section that has the randomPump in it.

#include <Wire.h> 
#include "lcdgfx.h"
DisplaySSD1306_128x64_I2C display(-1);
                // LCD SDA = A4 pin
                // LCD SDL = A5 pin

const byte pauseButton = 2;
const byte startSwitch = 7;
byte button1State;
byte button2State;

int pumpRelay = 4;
int valveRelay = 5;

int pumpPeriod;
volatile int newPumpPeriod;
volatile int oldPumpPeriod = 0;

long valveTimePeriod;
long pumpDelayPeriod;

volatile int minPump = 200;
volatile int maxPump = 800;


// random number generator = A0;
int pumpPin = A1;
int delayPin = A2;
int valvePin = A3;
int pressurePin = A7;

int randomPump;
long randomValve;
long randomDelay;

long pauseDelay;
long longDelay;
long loopRunlongDelay;
long loopRunShortDelay;

long microRunDelay;
long microRunlongDelay;
long microRunShortDelay;

int deflatedPressure = 15;
const int SensorOffset = 27;

volatile int pressureReading;
volatile int oldPressureReading;

unsigned long pumpMillisTime;
unsigned long fillMillisTime;
unsigned long inflateTime;

unsigned long valveMillisTime;
unsigned long deflateMillisTime;
unsigned long deflateTime;

int pumpErrorTime = 10000;
int noPressureTime = 200;          // time to wait before taking pressure reading
int randomPumpErrorTime;
int randomInflateTime;
int microPumpErrorTime;
int microInflateTime;

 int  randomDelayInterval = 75;
 int microDelayInterval = 100;

unsigned long previousTime = 0;
unsigned long currentTime;

int microDelayTimeCount = 1;
int randomDelayTimeCount = 1;


void setup(){
  Serial.begin(115200);
// other code
}

void loop(){
 
updateOLED();
checkButton();
setupLimits();
pumpsGo();
  
}


void pumpsGo(){         


  

checkButton();


 display.printFixedN (0,  48, "Pumps Go", STYLE_NORMAL, FONT_SIZE_2X);


 delay(10);
              
  int pumpReading = analogRead(pumpPin);                                       //  Reading pump pressure settings                    
  long pumpPeriod = map(pumpReading, 1023, 0, minPump, maxPump);         

newPumpPeriod = pumpPeriod;

if ( abs (newPumpPeriod - oldPumpPeriod) > 2)        {monitorPressure();}
          
int currentPressure = (analogRead(pressurePin)-SensorOffset);              // Pressure check
  
randomPump = random(200, pumpPeriod);                                   // Random pump time on generation

Serial.print("randomPump ");  Serial.println(randomPump); 
    

        
           if(currentPressure > randomPump){
               randomPump = random(currentPressure, pumpPeriod);            //  if pressure is already higher than random pump, recalculate with current reading as the min. 
          }

digitalWrite(pumpRelay, LOW);                                                     //PUMP ON +++++
pumpMillisTime = millis();  
pressureReading = (analogRead(pressurePin)-SensorOffset);
              
              while (analogRead(pressurePin)-SensorOffset < randomPump) {         // RANDOM PUMP
                    fillMillisTime = millis();
                    inflateTime = fillMillisTime - pumpMillisTime;

                       if (inflateTime > noPressureTime){
                              oldPressureReading = (analogRead(pressurePin)-SensorOffset);
                                      if (oldPressureReading < pressureReading)  { noPressure(); } 
                        }
    
                       
                                    if (inflateTime >= pumpErrorTime){
                                          digitalWrite(pumpRelay, HIGH); 
                                          digitalWrite(valveRelay, LOW);

                                       for (int c = 0; c <= 2; c++){
                            display.printFixedN (0,  48, "Pumps Go", STYLE_NORMAL, FONT_SIZE_2X);
                            delay(500);
                            display.printFixedN (0,  48, "          ", STYLE_NORMAL, FONT_SIZE_2X);
                            delay(250);
                            display.printFixedN (0,  48, "TimeERROR", STYLE_NORMAL, FONT_SIZE_2X);
                            delay(500);
                            display.printFixedN (0,  48, "          ", STYLE_NORMAL, FONT_SIZE_2X);
                            delay(250);                            
                           
                                       }
                        digitalWrite(valveRelay, HIGH); 
                        break;              
                       }               
                }
              
digitalWrite(pumpRelay, HIGH);                                                    // PUMP OFF ______


          int delayReading = analogRead(delayPin);                                           // Delay pot reading    
          long pumpDelayPeriod = map(delayReading, 1023, 0, 1000, 60000);                    


long randomDelay = random(1000, pumpDelayPeriod);                                  // Random delay time generation after pump up,  1 sec. to 60 seconds
      delay(randomDelay); 



checkButton();

randomValve = random(100, pumpPeriod/.75);                                          // Random Valve time generation
//Serial.print("randomValve  ");  Serial.println(randomValve);
      digitalWrite(valveRelay, LOW);                                                      // Valve ON #####
              delay(randomValve);
           digitalWrite(valveRelay, HIGH);                                               // Valve OFF ------
 
          int valveReading = analogRead(valvePin);                                         // Delay pot reading
          long valveTimePeriod = map(valveReading, 1023, 0, 1000, 30000);
 
longDelay = random(500, valveTimePeriod);                                             // Random Delay before restarting  1 to 30 seconds
      delay(longDelay);

oldPumpPeriod = pumpPeriod; 
display.printFixedN (0,  48, "          ", STYLE_NORMAL, FONT_SIZE_2X);

}

Highly unlikely. The description indicates a memory problem. Common causes are:

  • Accessing beyond the end of an array
  • Stack overflowing into the heap
  • Failure of application code to check for failure of a dynamic allocation request
  • De-referencing an invalid pointer

The error's source could be anywhere in the code … including in libraries that you use. So, you're delusional if you think only looking at the code where the symptom occurs will help.

I will look more into it tomorrow when i have the time. I always thought that if a specific variable is called and is isolated into one function only, and is not used anywhere else in the code, then it shouldn't be able to be overwritten? i may be wrong, but that's been my experience so far. how i see it, if it prints fine along with any another variable, then there shouldn't be any problem printing it by itself. But i'm still learning and trying to understand it. I thought there could have been some problem with a buffer or something, but don't know yet

OK.. for the 4th time (posts #2, #5, #7)... can you post ALL (every line) of the code.

There's no way anyone can help unless you do.

The C language doesn't provide such protection. It's the programmers responsibility to obey the bounds of program allocated memory.

Other parts of the code can't write that variable by name... but a programming error can cause your code to write any data into any location in memory. This allows any code that invokes undefined behavior to damage any variable in any way.

Maybe you have at Serial.begin(); the wrong number?
Greetings Frenik

What does that mean ?

If you open the serial Monitor you have on the bottom right side a box with a number. This Number should be the same as the number in the brackets in Seerial.begin().
here is a picture what I mean

If this Numbers are not the same you get only unexpected signs on the screen.
Greetings Frenik

There is a mix of correct text and garbage so it is not a baud rate mismatch.

For the time being, i'm giving up on trying to find the glitch as it doesn't effect the code or how it operates. It's just one of those things if i need that particular value, i'll just have to print any another value to get it correctly, which is what i've did in the past, which is why i never noticed it before. as that value is pretty much pointless without other values. But it's just something i've never experienced before and was curious on what could cause it, and maybe some day i'll figure it out.

but thanks for everyones input

As I mentioned in post #7 the most likely cause of this issue is some kind of memory violation. Something is overwriting parts of the memory where that variable is stored. This is not an unsolvable problem, and by ignoring it you may find there are other issues that remain undetected in your code.

For some unknown reason you won't post all your code so I guess we'll never know.

Hi,
Just something I noticed.

randomValve = random(100, pumpPeriod/.75);  

pumpPeriod is an INT, if you divide pumpPeriod by 0.75, you will end up with a bigger number, possibly bigger than the limit of an int.
What Arduino controller are you using?

I'm not sure how 0.75 would be interpreted as the random function needs an int value.
0.75 may be interpreted as ZERO, so the resulting value will not be rational because you are dividing by ZERO.

Do you mean to multiply pumpPeriod by 0.75 to make a smaller value?
ie, pumpPeriod * 3 / 4
OR do you really want to divide by 0.75.
ie. pumpPeriod * 4 / 3 but be careful of integer overload.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia: