Because you are using a for()
loop, I am unsure if you can call a timer in it and have the loop “wait” (without blocking) until the timer triggers, before advancing the loop count
I think you would need to redesign your preset count loop with a preset count timer… as per my example here using such a timer - C++ Blynk - Code Examples for Basic Tasks (Work in Progress)
Perhaps something like this in Lambda form?
This is completely untested…
void SomeLoopFunction) {
i=0;
timer.setTimer(500, []() { // Run this timer every 500ms
i++; // Count up
srada.write(10+i*10);
distance1[i] = getDistance();
Serial.print("Gia tri: ");
Serial.println(distance1[i]);
if(distance1[i]>=4 & distance1[i]<=10){
d = d+i*10;
dem2++;
Serial.println(d);
//phan biet mau
color = readColor();
}
}, 17); // Run this timer 17 times
}