Create a counter.
Every millis() overflow You reload the millis() comparative variable, then you send the msg and increment the counter. When the counter is greater than the number of messages you want to send, you reload the variable with a higher value and reset the counter.
const unsigned long eventInterval = 10000;
unsigned long previousTime = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
/* Updates frequently */
unsigned long currentTime = millis();
/* This is the event */
if (currentTime - previousTime >= eventInterval) {
/* Event code */
Serial.println("A");
/* Update the timing for the next time around */
previousTime = currentTime;
}
This keeps sending the "A" every 10 seconds.
But how do I add that 2 seconds later it send a "B"
I have tried many different things but I can't figure it out.
The counting on the way through is the increment of index
index++
and the resetting when you've gone too far is when index gets to (or somehow slips past) the number of things to print.
if (index >= <number of things> ) index = 0;
You could use the index or counter to decide how long to pause each step with a bit of extra logic and maybe an array to hold the dwell times in place of the hard-coded 1000.
That's the idiomatic expression for letting the compiler calculate the number of entities in an array.
The size of the entire array divided by the size of one element of the array.
You can stash your delay times in an array like
unsigned long times[] = {2000, 2000, 2000, 5000, 10000};
Obvsly, I hope, you'd need as many as you were going to index into.
Then where you want the time to be variable in that way, write
if (currentTime - previousTime > times[index]) {
If you've used arrays before, you may not have asked the questions, so I assume perhaps you have not. Mark it for learning times, array variables are the bomb.
You have to stop thinking of millis as a block of cryptic code to copy and think about what it means. You're just measuring how much time has passed since some event.
unsigned long previousStartTime;
boolean zerothThingHappened = false;
boolean firstThingHappened = false;
boolean secondThingHappened = false;
boolean thirdThingHappened = false;
boolean fourthThingHappened = false;
void setup() {
Serial.begin(115200);
delay(500);
Serial.println("Lots Of Millis: ");
}
void loop() {
unsigned long currentTime = millis();
// 5 events at 2 second intervals for a ten second cycle.
if (currentTime - previousStartTime >= 10000) {
// ten seconds have passed, so reset the cycle
zerothThingHappened = false;
firstThingHappened = false;
secondThingHappened = false;
thirdThingHappened = false;
fourthThingHappened = false;
previousStartTime = currentTime; // <<<<<< THIS IS THE TIME WE WILL MEASURE FROM
}
if(!zerothThingHappened) { // && currentTime - previousStartTime >= 0 is always true
Serial.println("ZerothThing");
zerothThingHappened = true;
}
if(!firstThingHappened && currentTime - previousStartTime >= 2000){
// It's been 2 seconds since previousStartTime
Serial.println("FirstThing");
firstThingHappened = true;
}
if(!secondThingHappened && currentTime - previousStartTime >= 4000){
// It's been 4 seconds since previousStartTime
Serial.println("SecondThing");
secondThingHappened = true;
}
if(!thirdThingHappened && currentTime - previousStartTime >= 6000){
// It's been 6 seconds since previousStartTime
Serial.println("ThirdThing");
thirdThingHappened = true;
}
if(!fourthThingHappened && currentTime - previousStartTime >= 8000){
// It's been 8 seconds since previousStartTime
Serial.println("FourthThing");
fourthThingHappened = true;
}
}
Okay I will try playing around with the code I have now and see how it works in action.
Then I might make more sense for me.
Maybe I should mention what I am going to use it for.
I have 3 Arduinos and 3 HC12 wireless modules.
1 Arduino as the receiver
2 Arduinos as transmitter
The 2 transmitters is constantly calculating the status of 2 machines, if they are running, in standby or is stuck.
I call the first transmitter A and the other one B
To prevent both transmitters in sending status updates at the same time, the receiver calls A and gets it's value which determine if a Red, Yellow or Green LED should light up.
Then it calls B etc. I plan to add more transmitters later.
I tried using normal delay in my code but it caused the rest of my sketch to stop functioning.
Therefore I discovered millis might be able to what I want because it doesn't interfere with the rest of the sketch.
Your try will not compile. For what ever reason you have changed some variable names but have forgotten some. Why don't you stick to the variables like used in "Blink Without Delay"?
if you really just need A, B, C, D there is no need to hold them in a c-string. Just hold the value in char variable and increment that *)
void sendInInterval(uint32_t currentTime = millis()) {
static char c = 'A'; // the character/letter to print out
static uint32_t previousTime = 0;
//The event should happen every 10 seconds
if (currentTime - previousTime > 10000) {
previousTime = currentTime;
//Print "A" then "B" etc...
Serial.println(c++); // print current letter and afterwards increment for next iteration
if (c > 'D') c = 'A'; // wrap around
}
}
void setup() {
Serial.begin(115200);
}
void loop() {
sendInInterval();
//Rest of the loop functions happens here
}
*) or store 0,1,2,3 and just add +'A' when you print out like already shown.