Pages: 1 [2]   Go Down
Author Topic: Switch-case problem: exit from case during it's running time  (Read 1545 times)
0 Members and 1 Guest are viewing this topic.
Queens, New York
Offline Offline
Faraday Member
**
Karma: 101
Posts: 3644
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26348
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
}
« Last Edit: March 14, 2013, 10:06:53 am by 8bit_Biker » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26348
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26348
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26348
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Thank you for your kindness.
Logged

Pages: 1 [2]   Go Up
Jump to: