Skip to main content
Arduino

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Regarding running multiple functions using millis

I have been writing code for measuring when the digital signal comes and goes (ON and OFF respectively). I have done the part calculating a single system's downtime (difference in timings between ON and OFF from the system), but when I'm not able to scale up to multiple systems when I try to copy the same code which worked above. So it would be helpful if anyone could provide some insight into how it can be done plus explaining with the code would help.

My code:

unsigned long a=0, b=0, difference; 
void setup() 
{ 
 Serial.begin(9600); 
 pinMode(2, INPUT); 
 Serial.print("start"); 
 Serial.print(","); 
 Serial.print("stop"); 
 Serial.print(","); 
 Serial.print("minutes"); 
 Serial.print(","); 
 Serial.println("seconds"); 
}
void loop() 
{ 
 //delay(1000); 
 int A = digitalRead(2); 
 if(A == LOW) 
 { 
 b = millis(); 
 } 
 if(A == HIGH) 
 { 
 a = millis(); 
 display_output(); 
 } 
 delay(3000); 
} 
void display_output() 
{ 
 int minutes, seconds, sec; 
 difference = a - b; 
 seconds = int(difference/1000); 
 if(seconds >= 60) 
 { 
 //sec = seconds; 
 seconds = seconds%60; 
 minutes = minutes+1; 
 } 
 //minutes = int(seconds/60); 
 Serial.print((a/1000)+1); 
 Serial.print(","); 
 Serial.print((b/1000)+1); 
 Serial.print(","); 
// Serial.print(difference); 
 //Serial.print(","); 
 Serial.print(minutes); 
 Serial.print(","); 
 Serial.println(seconds); 
 //Serial.println(","); 
} 

Answer*

Draft saved
Draft discarded
Cancel
4
  • What about a link to the state-change-detection example? arduino.cc/en/Tutorial/StateChangeDetection Commented Dec 13, 2018 at 13:00
  • This code looks neat and will definitely try with this one. I don't know much about constructors and structure things. Will learn about that. I used a delay in my program so as not to have too many data points and instead the data once in two or three seconds will be enough plus its not a time critical application, so I don't know if using millis function is overkill for my application. Thank you though :) Commented Dec 13, 2018 at 15:28
  • @Jot: Good idea. I added the link. Commented Dec 13, 2018 at 16:18
  • 2
    @RajeshAnand10: 1. Learning about object-oriented programming (classes with constructors and methods) is useful, but you can live without it for simple projects like this. 2. delay() was useful in your previous version only because you got the logic wrong. Once you properly detect state changes, it serves no useful purpose. 3. What do you mean by "overkill"? Do you believe millis() is more heavyweight than delay()? It's not. For your particular problem, it's the right tool for the job. Commented Dec 13, 2018 at 16:29

AltStyle によって変換されたページ (->オリジナル) /