I want a code which will stop millis() at any time when I send a text from phone please......
Why stop millis() ?
What problem are you trying to solve ?
Yes I also need help in this . could you please show some simple code example..
I have successfully run millis() in two if conditions
Now I want to stop millis() or code under if-condition with a text that I send to arduino with my phone and start another if-condition by sending different text from my phone could you please help.
74younus:
I want a code which will stop millis() at any time when I send a text from phone please......
74younus:
I have successfully run millis() in two if conditionsNow I want to stop millis() or code under if-condition with a text that I send to arduino with my phone and start another if-condition by sending different text from my phone could you please help.
X-Y problem.
See if you can follow this stoplight example that uses millis() and flags to keep track of where things are at
// Traffic light example code
// pre-setup, assign names to pins and declare variables
byte red_led = 6;
byte red_state = 0;
byte yellow_led = 5;
byte yellow_state = 0;
byte green_led = 4;
byte green_state = 0;
byte switch_pin = 2;
unsigned long red_start_time = 0;
unsigned long red_duration = 5000; // 5000 milliseconds, 5 seconds
unsigned long yellow_start_time = 0;
unsigned long yellow_duration = 5000; // 5 seconds
unsigned long green_start_time = 0;
unsigned long green_duration = 5000; // 5 seconds
unsigned long current_time = 0;
// set-up code, runs once
void setup(){
pinMode (red_led, OUTPUT);
pinMode (yellow_led, OUTPUT);
digitalWrite (yellow_led, HIGH);
yellow_state = 1;
yellow_start_time = millis();
pinMode (green_led, OUTPUT);
pinMode (switch_pin, INPUT);
digitalWrite (switch_pin, HIGH); // writing an input pin HIGH connects the internal pull-up resistor
} // end set-up
// loop code, runs over and over
void loop(){
current_time = millis(); // capture the time
if (yellow_state == 1){ // is yellow light on?
if ((current_time - yellow_start_time) >= yellow_duration){ // see if its time to turn it off
digitalWrite (yellow_led, LOW); // turn it off
yellow_state = 0; // store it's state
digitalWrite (red_led, HIGH); // turn on the red light
red_state = 1; // store its state
red_start_time = current_time; // store the time of the change
} // end yellow duration check
} // end yellow light check
if (red_state == 1){ // is red light on?
if ((current_time - red_start_time) >= red_duration){// see if its time to turn it off
digitalWrite (red_led, LOW); // turn it off
red_state = 0;// store it's state
digitalWrite (green_led, HIGH); // turn on the green light
green_state = 1;// store it's state
green_start_time = current_time;// store the time of the change
} // end red duration check
} // end red light check
if (green_state == 1){// is green light on?
if ( ((current_time - green_start_time) >= green_duration) || (digitalRead (switch_pin) == LOW) ) {// see if its time to turn it off, or if the switch was pressed
digitalWrite (green_led, LOW);// turn it off
green_state = 0;// store it's state
digitalWrite (yellow_led, HIGH); // turn on the yellow light
yellow_state = 1;// store it's state
yellow_start_time = current_time;// store the time of the change
} // end green duration or switch press check
} // end green light check
} // end loop
There is no need to stop millis() and there no simple way to stop it. You don't stop your kitchen clock when your chicken is ready to come out of the oven.
If you want to measure a new time period just save the value of millis() at the start of the period and compare the later value of millis() with that saved value
if (millis() - millisSavedAtStartOfPeriod >= interval) {
// time is up so do something
}
Have a look at how millis() is used to manage timing in Several Things at a Time.
And see Using millis() for timing. A beginners guide if you need more explanation.
...R
It's tough to keep multiple millis tasks sorted out. I like to create a Timer class, then you can create an array of timers:
// *********Begin Class nbTimer --- general purpose non-blocking timer
class nbTimer
{
public:
nbTimer(); // constructor prototype
unsigned char Time(unsigned long dly);
unsigned long Counter;
private:
unsigned long Count;
unsigned char Init = LOW;
unsigned char Timeout = LOW;
};
nbTimer::nbTimer() // constructor definition
{}
unsigned char nbTimer::Time(unsigned long dly) //*** Timer Function ****
{
if (Init == LOW)
{ // Run Once Code
Timeout = LOW; // Reset Flag
Count = millis (); // Snapshot of MILLIS count
Init = HIGH;
}
Counter = (millis ()) - Count; // so you can watch countdown
if (((millis ()) - Count) >= dly)
{
Timeout = HIGH; // set Flag
Init = LOW;
}
return Timeout;
}// *** END Class
Then at the start of your program, declare Timer objects:
nbTimer KitchenTimer;
nbTimer BathTimer;
The timer isn't retriggerable. It will only start over once it's timed out.
Often it's convenient to declare an array of timers: nbTimer StateTimer[6]
@74younus, do not cross-post. Do not hijack. Mess cleansed.
You have a fundamental misunderstanding what millis() is supposed to do:
Counting up milliseconds forever.
you can easily setup your own timer-function that is stopable
based on millis().
millis() is used for an if-condition to become true a certain period of time.
Just use a boolean variable to have a second if-condition which will count up or not count up your own counter conditional On the boolean variable.
If this description gives you no idea how to do it then it is time to change from copy&paste status to knowing the basics status.
Best regsrds Stefan
Because millis() does run continuously, capturing its value at some time is equivalent to starting a timer (since comparing the current time with the captured time always gives you the elapsed time after the capture).
I'm a big fan of "teaching how to fish" instead of delivering - by mobil phone - ordered fish ready to eat.
Arduino Programming Course
It is easy to understand and a good mixture between writing about important concepts
and get you going.
best regards Stefan
Dear 74younus,
there are different ways to ask for help. The worst would be
"finish my project - and hurry up!" I'm very sure this is not your way.
From your initial question "How can I stop millis()" the other users can conclude that you are a real beginnner.
It is completely OK to be a real beginner. And you can ask many many many questions in this forum. There are a lot of users that are willing to help. This willingness to help increases if the others can see that you put at least some own effort into your project. And this willingsness increases if you take time to describe in normal words what you want to do. I mean not only some details I mean the whole thing.
I want to give an example from the all day world: A question for a detail might be "how can i efficiently sharpen pencils?"
The answer to this detail-question is: use a sharpener machine driven by an electric motor or use a mechanical pencil.
Oh great I didn't know that there are mechanical pencil that don't need to be sharpened.
The whole thing the questioner is trying to achive is: making the walls of a room look grey. And in this example it is very obvoius that there are completley different ways to "make the walls of a room look grey" than using a pencil.
Have you ever heard about paint and brushes beeing 6 inches wide. Or paint rollers 10 inches wide?
So giving an overview what you want to achive in the end will open up a lot of new opportunities how you can solve your problem.
The second thing to increase willingness to help is to show your own effort in the forum. Posting code even if the code does not compile. The not compiling is a part of the problem(s) you might have.
And if you show in the forum that you yourself take at least small steps of increasing your knowledge about programming you will get a lot of help here.
best regards Stefan
Moderator edit: broken link removed
74younus:
My last request for help
My last time writing this... Do not cross-post.
Hi 74younus,
you posted while I was typing my answer. Well this is a starting point.
I want a program for two LEDs
1: start blink led using millis for 30 secs
2: start blinking another Led for 1 minute
3. I want to interrupt them whenever I want to stop them they should stop.
4. It should be my choice which led to start first.
In a previous post you wrote about
stop at any time when I send a text from phone
if you have alredy code that receives text-messages from a phone please post.
If not it is better to leave this detail away. But still post your code. Even if it does not compile
best regards Stefan
@ Coding badly:
just out of curiosity:
Do you have to opportunity to re-sort postings from different threads or to MOVE to a new thread?
best regards Stefan
I don't understand the question.
StefanL38:
There are a lot of users that are willing to help. This willingness to help increases if the others can see that you put at least some own effort into your project.
In addition to what @StefanL38 has said, willingness to help will rise dramatically if you actually take the trouble to respond to the Replies that you do receive - even if it is only to tell us that you don't understand them.
...R
Now I do not need to stop loop or millis
anymore or , I have set some criteria outside arduino code and that helped me end the loop easily...
Now I do not need to stop loop or millis
anymore or , I have set some criteria outside arduino code and that helped me end the loop easily...