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.
- http://electronics.stackexchange.com/questions/42049/arduino-serial-print-changes-behavior-of-program-undesireably
- http://forum.arduino.cc/index.php?topic=208612.0
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;
}
}