My Arduino code acts weirdly whenever Serial.println() is removed from loop().

I have this code: http://pastebin.com/DvPb6Liu.
From this code I want to create a physical electronic dice using tilt switch.

The problems are in line number 21 and line number 46.
If I commented Serial.println() in line number 21, the Serial.println() in line number 46 is never executed. So in order for me to know what is the result from this electronic dice I need to put Serial.println() in the void loop(), outside any other statement (if(), for(), while(), etc). This happened to me several times. Is this behavior intended? If not, how can I fix this problem?

I have seen some other people have this similar problem.

The first link tell me that I need to free some space from header file (?). Which I am not sure what he meant there, but my code only use 10% from the program storage space and its variable occupy 9% of total dynamic memory. So, I am not sure whether it is really the problem. The latter link does not come up with any solution.

Here is my code in case PasteBin cannot be accessed.

#define PIN_DICE        A5
#define PIN_RANDOM_SEED A0

int     numberDice                  = 0;
bool    numberDiceNew               = false;
int     counterDice                 = 0;
int     counterDiceThreshold        = 10;
bool    counterInitialTrigger       = false;
int     counterInitialDuration      = 1000;
int     counterInitialDurationMax   = 1000;
int     statePin                    = 0;
int     statePinPrevious            = 0;

void setup(){
    Serial.begin    (9600);
    pinMode         (A5, INPUT);
}

void loop(){
    //POINT OF INTEREST 1.
    //Serial.println("PUT ANY STRING HERE!");
    randomSeed(analogRead(PIN_RANDOM_SEED));
    int analogPinValue              = analogRead(PIN_DICE);
    if(analogPinValue               > 511)          { statePin = 1; }
    else if(analogPinValue          <= 511)         { statePin = 0; }
    if(statePinPrevious             != statePin)    { counterInitialTrigger = true; }
    if(counterInitialTrigger){
        if(statePinPrevious         != statePin)    { counterDice ++; }
        counterInitialDuration      --;
    }
    if(counterDice                  >= counterDiceThreshold){
        numberDice                  = random(1, 21);
        numberDiceNew               = true;
        counterDice                 = 0;
        counterInitialDuration      = counterInitialDurationMax;
        counterInitialTrigger       = false;
    }
    if(counterInitialDuration       <= 0){
        counterDice                 = 0;
        counterInitialDuration      = counterInitialDurationMax;
        counterInitialTrigger       = false;
    }
    statePinPrevious                = statePin;
    if(numberDiceNew){
        //POINT OF INTEREST 2.
        Serial.println              (numberDice);
        numberDiceNew               = false;
    }
}
    randomSeed(analogRead(PIN_RANDOM_SEED));

randomSeed() should be called ONCE, in setup(), not on every pass through loop().

    if(analogPinValue               > 511)          { statePin = 1; }
    else if(analogPinValue          <= 511)         { statePin = 0; }

If the value is not greater then 511, is there ANY way for it not to be less than or equal 511? The second if statement is useless.

I find your code very hard to read.

There is too much horizontal white space, but not enough vertical white space.

There are NO comments. The names of variables might mean something to you, but I can’t guess what your program is doing. statePin, for instance, holds neither a state or a pin.

The Serial.print() is acting like a delay. When you remove it, then I find it hard to believe that your tilt switch will change states 10 times in much less than 1 ms. (This information of what your program does would have been GREAT to know instead of me following your code step by step for 10 minutes trying to figure it out…)

Try changing this

int     counterInitialDuration      = 1000;
int     counterInitialDurationMax   = 1000;

to this

int     counterInitialDuration      = 32767;
int     counterInitialDurationMax   = 32767;

Chances are it will probably work, but you have no delays or timing, it just runs as fast as it can, soo…ya

Chances are it will probably work

It is certain that that will NOT work. What range of values can an int hold?

PaulS:

    randomSeed(analogRead(PIN_RANDOM_SEED));

randomSeed() should be called ONCE, in setup(), not on every pass through loop().

    if(analogPinValue               > 511)          { statePin = 1; }

else if(analogPinValue          <= 511)         { statePin = 0; }



If the value is not greater then 511, is there ANY way for it not to be less than or equal 511? The second if statement is useless.

I find your code very hard to read.

There is too much horizontal white space, but not enough vertical white space.

There are NO comments. The names of variables might mean something to you, but I can't guess what your program is doing. statePin, for instance, holds neither a state or a pin.

Changing the randomSeed to setup() does not solve the problem apparently.

Ps991:
The Serial.print() is acting like a delay. When you remove it, then I find it hard to believe that your tilt switch will change states 10 times in much less than 1 ms. (This information of what your program does would have been GREAT to know instead of me following your code step by step for 10 minutes trying to figure it out…)

Try changing this

int     counterInitialDuration      = 1000;

int     counterInitialDurationMax   = 1000;




to this



int     counterInitialDuration      = 50000;
int     counterInitialDurationMax   = 50000;




Chances are it will probably work, but you have no delays or timing, it just runs as fast as it can, soo...ya

Hi thanks for your insight I put small delay in the loop() and now it is working. My problem is solved for now :).

My problem is solved for now

You misspelled delay()ed. You have NOT solved the problem. You've masked it.

PaulS:
You misspelled delay()ed. You have NOT solved the problem. You've masked it.

I am using setInterval() :P.

PaulS:
It is certain that that will NOT work. What range of values can an int hold?

depends on the compiler, could be 32bit..........but no seriously, rookie move on my part...