Hi! I'm doing a school project but I'm stuck on using Millis for multiple delays. I already checked the forums and BlinkWithoutDelay but still couldn't fix it. I just don't understand ;(
The reason why I'm using tone() instead of melody is, because for some kind of reason, I keep getting an error so I decided to play music by using tone(). I need to use millis because I want to rotate a servo at the same time and with delay that's not going to happen. This is my code so far and it all works except for the music part. What am I doing wrong here?
Thank you!
#include <elapsedMillis.h>
elapsedMillis timeElapsed;//declare global if you don't want it reset every time loop runs
unsigned int interval1 = 100;
unsigned int interval2 = 150;
unsigned int interval3 = 200;
unsigned int interval4 = 300;
unsigned int interval5 = 550;
unsigned int interval6 = 575;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
// constants won't change. They're used here to set pin numbers:
const int buttonPin = D3; // the number of the pushbutton pin
const int piezoPin = D8; // the number of the piezo
// variables will change:
int piezoState = LOW;
int buttonState; // variable for reading the pushbutton status
int lastButtonState = LOW; // the previous reading from the input pin
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
// initialize the piezo pin as an output:
pinMode(piezoPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
myservo.attach(D5); // attaches the servo on
// set initial piezo state
digitalWrite(piezoPin, piezoState);
Serial.begin(115200);
}
void loop() {
// read the state of the pushbutton into a local value:
int reading = digitalRead(buttonPin);
int pos;
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only play music if the new button state is HIGH
if (buttonState == HIGH) {
Serial.println(buttonState);
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
// tones van https://gist.github.com/gskielian/6135641
tone(piezoPin,660,100);
if (timeElapsed > interval2) {
tone(piezoPin,660,100);
timeElapsed = 0;
} if (timeElapsed > interval4) {
tone(piezoPin,660,100);
timeElapsed = 0;
} if (timeElapsed > interval2) {
tone(piezoPin,510,100);
timeElapsed = 0;
} if (timeElapsed > interval1) {
tone(piezoPin,660,100);
timeElapsed = 0;
} if (timeElapsed > interval4) {
tone(piezoPin,770,100);
timeElapsed = 0;
} if (timeElapsed > interval5) {
tone(piezoPin,380,100);
timeElapsed = 0;
} if (timeElapsed > interval6) {
tone(piezoPin,510,100);
timeElapsed = 0;
}
//delay(450);
//tone(piezoPin,380,100);
//delay(400);
//tone(piezoPin,320,100);
//delay(500);
//tone(piezoPin,440,100);
//delay(300);
//tone(piezoPin,480,80);
//delay(330);
//tone(piezoPin,450,100);
//delay(150);
//tone(piezoPin,430,100);
//delay(300);
//tone(piezoPin,380,100);
//delay(200);
//tone(piezoPin,660,80);
//delay(200);
//tone(piezoPin,760,50);
//delay(150);
//tone(piezoPin,860,100);
//delay(300);
//tone(piezoPin,700,80);
//delay(150);
//tone(piezoPin,760,50);
//delay(350);
//tone(piezoPin,660,80);
//delay(300);
//tone(piezoPin,520,80);
//delay(150);
//tone(piezoPin,580,80);
//delay(150);
//tone(piezoPin,480,80);
//delay(500);
//
//tone(piezoPin,510,100);
//delay(450);
//tone(piezoPin,380,100);
//delay(400);
//tone(piezoPin,320,100);
//delay(500);
//tone(piezoPin,440,100);
//delay(300);
//tone(piezoPin,480,80);
//delay(330);
//tone(piezoPin,450,100);
//delay(150);
//tone(piezoPin,430,100);
//delay(300);
//tone(piezoPin,380,100);
//delay(200);
//tone(piezoPin,660,80);
//delay(200);
//tone(piezoPin,760,50);
//delay(150);
//tone(piezoPin,860,100);
//delay(300);
//tone(piezoPin,700,80);
//delay(150);
//tone(piezoPin,760,50);
//delay(350);
//tone(piezoPin,660,80);
//delay(300);
//tone(piezoPin,520,80);
//delay(150);
//tone(piezoPin,580,80);
//delay(150);
//tone(piezoPin,480,80);
//delay(500);
//
//tone(piezoPin,500,100);
//delay(300);
//
//tone(piezoPin,760,100);
//delay(100);
//tone(piezoPin,720,100);
//delay(150);
//tone(piezoPin,680,100);
//delay(150);
//tone(piezoPin,620,150);
//delay(300);
//
//tone(piezoPin,650,150);
//delay(300);
//tone(piezoPin,380,100);
//delay(150);
//tone(piezoPin,430,100);
//delay(150);
//
//tone(piezoPin,500,100);
//delay(300);
//tone(piezoPin,430,100);
//delay(150);
//tone(piezoPin,500,100);
//delay(100);
//tone(piezoPin,570,100);
//delay(220);
//
//tone(piezoPin,500,100);
//delay(300);
//
//tone(piezoPin,760,100);
//delay(100);
//tone(piezoPin,720,100);
//delay(150);
//tone(piezoPin,680,100);
//delay(150);
//tone(piezoPin,620,150);
//delay(300);
//
//tone(piezoPin,650,200);
//delay(300);
//
//tone(piezoPin,1020,80);
//delay(300);
//tone(piezoPin,1020,80);
//delay(150);
//tone(piezoPin,1020,80);
//delay(300);
//
//tone(piezoPin,380,100);
//delay(300);
//tone(piezoPin,500,100);
//delay(300);
//
//tone(piezoPin,760,100);
//delay(100);
//tone(piezoPin,720,100);
//delay(150);
//tone(piezoPin,680,100);
//delay(150);
//tone(piezoPin,620,150);
//delay(300);
//
//tone(piezoPin,650,150);
//delay(300);
//tone(piezoPin,380,100);
//delay(150);
//tone(piezoPin,430,100);
//delay(150);
//
//tone(piezoPin,500,100);
//delay(300);
//tone(piezoPin,430,100);
//delay(150);
//tone(piezoPin,500,100);
//delay(100);
//tone(piezoPin,570,100);
//delay(420);
//
//tone(piezoPin,585,100);
//delay(450);
//
//tone(piezoPin,550,100);
//delay(420);
//
//tone(piezoPin,500,100);
//delay(360);
}
}
}
// set the LED:
digitalWrite(piezoPin, piezoState);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}