How to use millis instead of delay

long previousMillis = 0;    // Store last update
long interval = 500;    
void setup() {
  pinMode(9,OUTPUT);
}
void loop() {
  for(int i=500;i<=1000;i++) { 
    tone(9,i);    /
    delay(4);    
   }
  for(int i=1000;i>=500;i--) {
    tone(9,i);
    delay(3);
  }
}

Basically this is my code to tone a melody. However, may someone let me know how to convert it to millis() instead of delay?TQ

If you had a watch (millis()), a pencil (the assignment operator), and a pad of paper (some variables), how would YOU turn the tone function on and off at the required time?

There are hundreds of tutorials on how to use millis() to write non-blocking code.

YOU need to do some research, not expect to be spoonfed.

Look at the top of this forum where you posted the question. There's a good tutorial up there you must have missed. It's called "Using millis() for timing...." which is a bit of a hint.

Steve

here is a great thread by UKHeliBob that explains it very well:

–DH

slipstick: Look at the top of this forum where you posted the question. There's a good tutorial up there you must have missed. It's called "Using millis() for timing...." which is a bit of a hint.

Right at the top of the forum. You'd think it would be hard to miss: Using millis() for timing. A beginners guide

Notwithstanding the advice given above, what would you actually hope to accomplish? Replacing delay with millis is done to free up the processor to do other things while you wait. You typically find this in scenarios similr to blinking the LED off and on every second. Why wait that second in a do-nothing loop? In your case, you've got a 3ms and a 4ms delay that appear to be a part of how you want your tone exposed. How is changing this going to improve your code? Delays are, at times necessary, much the same as other maligned commands (GOTO??), or at least they make coding somewhat more straightforward. Replace it if you must, but before ever self-optimizing code, ask yourself why, and if you can't come up with a decent answer, don't. If it ain't broke, don't fix it.