Hi, I am using millis() function to program with something. What I would like to achieve is a periodic operation like the followings:
0 - 600 ms : print A = 0
600 - 1000 ms; print A = 2;
1000 - 1600 ms: print A = 1;
1600 - 2000ms:print A = 2;
2000 - 2600 ms : print A = 0
......
It will repeat the process above and 2000ms as a whole period.
Could you please provide some suggestions on how I can achieve this? I successfully completed two mode switching but not sure how to deal with this. Appreciated a lot.
Some time ago we had a discussion about delaying / scheduling.
One of the suggestions was to use a scheduler that allows scheduled tasks to schedule other tasks. This mechanism can be (partially?) used to do what you suggest. For an example, see the following simulation.
Hi, it looks like what I need. I am wondering whether it can be integrated with millis() function to implement continuous switching multi-tasks. But I am not able to test it immediately. I will let you know once I tested it on my Arduino board.
but of course this could be anything else you want to do at the end of the current sequence.
you could also add code after incrementing the currentSequence variable if you need to do something at the start of a sequence too
Thank you very much for identifying this error. I have my full code attached here. Could you please help to check why the period() function doesn't run?
unsigned long millis1;
unsigned long millis2;
int state = 1;
int pressure = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
if ( (millis() - millis1) > 1000) {
millis1 = millis();
if (state == 0) {
state = 1;
} else {
state = 0;
}
}
period();
Serial.print("Pressure: ");
Serial.println(pressure);
Serial.print("State: ");
Serial.println(state);
}
void period() {
millis2 = millis();
if ((millis() - millis2) >= 600 && (millis() - millis2)<=1000) {
pressure = 200;
}else if((millis() - millis2) >= 1600 && (millis() - millis2)<=2000){
pressure = 100;
}else{}
}
Apart to incorrect condition syntax - this condition never met. You assign millis2 to the millis and just next line compare it with millis itself. The difference between millis and millis2 will never reach 600ms, it is unlikely to even be different from zero
millis2 is initialized to the value of millis… what do you think is the result of millis() - millis2 a few nanoseconds later? Any chance it’s above 600?
Your code describes values of pressure in two intervals : 600-1000ms and 1600-2000ms. But what is beyond it? How much should be pressure before 600ms or between 1000 and 1600?
When planning a program, you must foresee all situations. According to your code, it means that the pressure will remain 200 from 1000ms to 1600. Then why limit the interval to 600-1000, write 600-1600 right away.
I hope the idea is clear? - you must set the pressure for all points of the cycle, not just for small intervals
Thanks for sharing your experience. I will modify all these mentioned problems. One more question, please how can I reset millis() to 0 so that the code can run repeatedly?
No way.
In order to run the code repeatedly, you don't need it.
Just remember the millis value at the start of the cycle and count all your intervals from it