Question about using millis() efficiently
Serious problem with millis()
esp32 - I am using millis () function but after one iteration it does not check the condition and enters the loop. What should I do? - Arduino Stack Exchange
How to test millis() rollover on ESP32
Videos
If your code includes many function on the main loop which call millis() for various timing needs, is it better to have a global variable which updates once per cycle with the value of millis() or is calling millis() seperatly many times perfectly fine?
We are talking 10-12 millis() calls per cycle at the moment.
The MCU is esp32
I want to make the pin high as soon as the code enters into the millis function and turn it off after some microseconds and again turn it on after some milliseconds. here I can create a microsecond delay using delayMcirosecond but how can create the delay in milliseconds as we can not use delay inside millis
Code: int Ad[] = {20, 7, 6, 9, 1, 7, 8, 1, 1, 1, 30, 8, 7}; float Af[] = {1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 23}; int Ap[] = {15, 25, 50, 50, 50, 90, 50, 50, 70, 50, 50, 50, 50};
int xa = sizeof(Ad) / sizeof(int); float ya = sizeof(Af) / sizeof(float); int za = sizeof(Ap) / sizeof(int);
void setup() { // put your setup code here, to run once: Serial.begin(115200); pinMode(26,OUTPUT);
} void w_a(int Ad1[], int x , float Af1[] , int y, int Ap1[], int z) { unsigned long previousMillis = 0; int i; for (i = 0; i < x; i++) { int d = Ad1[i] * 1000; float f = round((1 / Af1[i]) * 1000); int p = Ap1[i];
unsigned long currentMillis = millis();
previousMillis = currentMillis;
while (currentMillis - previousMillis < d)
{
digitalWrite(26, HIGH);
delayMicroseconds(p);
digitalWrite(26, LOW);
delay(f);
currentMillis = millis();
}
previousMillis = currentMillis;} }
void channel1_1() { int j = 0; for ( j = 0; j < 2; j++) { w_a(Ad, xa, Af, xa, Ap, xa); // A } }
void loop() { channel1_1(); }