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);
}
}