Go Down

Topic: Switch-case problem: exit from case during it's running time (Read 1 time) previous topic - next topic

HazardsMind

Replace "nowtime-prevtime" with "millis() - nowtime"
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

AWOL

Quote
Replace "nowtime-prevtime" with "millis() - nowtime"

Normally, I'd disagree (call "millis" once and assign the value to a variable), but here "nowtime" is at least a second in the past...
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

8bit_Biker

#17
Mar 14, 2013, 04:05 pm Last Edit: Mar 14, 2013, 04:06 pm by 8bit_Biker Reason: 1

Did you read what I said about the return type of "millis"?
Did you read what I said about static variables?
Did you read what I said about getting rid of "delay"?


The code below, exit after "second print". I've used delay to make an useless experiment..
I've found in google that millis() has some problem when it's called inside functions and outside loop(). Is it true??

Code: [Select]
char firstcase(){
 static byte state;
 static unsigned long prevtime=0;
 static unsigned long nowtime=millis();
 Serial.println("first print");
 Serial.println("second print");
 char inputChar
 if(nowtime-prevtime>10000){
   Serial.println("third print");
   if(Serial.available()){
     inputChar=Serial.read();
   }      
   return inputChar;
 }
 if(Serial.available()){
   ch=Serial.read();
 }
 if(nowtime-prevtime>5000){
   Serial.println("fourth print");
   if(Serial.available()){
     inputChar=Serial.read();
   }
   return inputChar;
 }
 return inputChar;
}

AWOL

Quote
I've found in google that millis() has some problem when it's called inside functions and outside loop().

That's a new one to me.
You can't (shouldn't) use it to initialise static or global variables, but I'm not aware of doing that here.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

8bit_Biker


Quote
I've found in google that millis() has some problem when it's called inside functions and outside loop().

That's a new one to me.
You can't (shouldn't) use it to initialise static or global variables, but I'm not aware of doing that here.


Did You test the code you've post before? As you post it (even two braces are missing in the end) it does not work.

AWOL

Quote
As you post it (even two braces are missing in the end) it does not work.

No, I told you it was a generic example.
It is obviously incomplete.

If I wrote much more of it, there wouldn't be anything for you to do, and where would be the fun in that?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

8bit_Biker


Quote
As you post it (even two braces are missing in the end) it does not work.

No, I told you it was a generic example.
It is obviously incomplete.

If I wrote much more of it, there wouldn't be anything for you to do, and where would be the fun in that?


:smiley-yell: This sounds good!
Ok... challenge accepted! I'll try to work hard to solve my problem. I hope I'll get the goal soon.

8bit_Biker

Hi AWOL,
I've tried to solve the problem without using the delay, but i can't get the solution. Can you help me again explain to me in a better way your example?

AWOL

No, I really can't, but here's a similar problem, well explained.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

8bit_Biker


No, I really can't, but here's a similar problem, well explained.


Thank you for your kindness.

Go Up