First time using millis(); instead of delay (); and having some issues

So I'm writing code for a simple robot with an OLED display for a "face" and a piezo buzzer for sounds. I'm trying to make a series of beeps with the buzzer, and originally I used delay ();
but quickly realized that that was causing the OLED display to shut off while the beeping was happening. I googled for a solution, and found the millis (); function. I tried to use it in my code but now the buzzer doesn't beep at all. Anyone know what's wrong? Here's my code:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

int mil;

//Make screen object
Adafruit_SSD1306 display(128, 64, &Wire, -1);

void setup() {
//Do the adafruit thing
 display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
 display.clearDisplay();
 display.display();

}

void loop() {

//Display Eyes
display.fillCircle (32, 32, 10, WHITE);
display.fillCircle (96, 32, 10, WHITE);
display.display();
delay (3000);

//Display blinky eyes
display.clearDisplay();
display.display();
display.drawFastHLine (22, 32, 20, WHITE);
display.drawFastHLine (86, 32, 20, WHITE);
display.display();
delay (100);
display.clearDisplay();
display.display();

//Play morse code on buzzer
tone (2, 500); //play beep
mil = millis(); //start time tracking
while (mil = 100) { //turn off beep when 100 ms has passed
noTone (2);  
}
mil = 0;
mil = millis();
tone (2, 500);
mil = millis();
while (mil = 100) {
noTone (2);  
}
mil = 0;
mil = millis();
tone (2, 500);
mil = millis();
while (mil = 100) {
noTone (2);  
}
mil = 0;
mil = millis();
tone (2, 500);
mil = millis();
while (mil = 300) {
noTone (2);  
}
mil = 0;
mil = millis();
tone (2, 500);
mil = millis();
while (mil = 100) {
noTone (2);  
}
}

P.S. I know this isn’t what the examples that pop up when you google this question say, but I was just trying to see if this method would work. It seemed easier to me and I feel like it should work but I just don’t know why it’s not. I have no problem with changing the code to the stuff I know would actually work but I figured I’d ask what was wrong with this and see if I could salvage it. If I can fix it, cool! If I can’t, fine with me. You learn by making mistakes!

Hello crabbo007
Take a view into the BLINKWITHOURDELAY example of the IDE to gain the knowledge how to design timers.

1 Like

A) you want == not =
B) that construct will block like delay()

1 Like

Why does it still block?

Take a view here.

Thanks to both of you!!!

Thanks, this example is a lot less confusing than others I’ve read.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.