I’m trying to use elapsedMillis as part of a countdown that blinks an LED, plays a buzzer a number of times. I initially had problems when compiling with an error asking for the libraries which I think I have resolved by following another thread. However, with the elapsedMillis.h now included in the project folder I’m getting a number of compiler errors - millis was not declared, micros was not declared etc. in elapsedMillis. I’m also getting a expected declaration before } token, which I’m pretty sure is something to do with me getting out of my beginner depth!
Any help gratefully received.
Thanks
Clive.
#include <elapsedMillis.h>
///////// VARIABLES
const int countDownLed = D7; // Set the countdown LED to pin D7
const int beeper = D0; // Set the beeper pin to D0 - //NOTE Tones only work on certain pins
const int pushButton = D2; // Set the push button to D2
int pushButtonState = LOW; // Sets pushbutton state variable
int countDownLedState = LOW; // Sets LED state variable
int countDownState = 0;
int beeperState = LOW; // Sets beeper state variable
////////// TIMING
unsigned long previousMillis = 0; // Set variable for time check in countdown
unsigned long previousMillisButton = 0; // Set variable for time check in button press check
const long buttonInterval = 500; // Button press check interval
const long interval = 300; // Countdown LED/Beeper interval
////////// BEEPER
int countdownToneLength = 100; // Insert length of countdown beep
int countdownTonePitch = 3000; // Tone/frequency of countdown beep
///////// SET-UP
void setup() {
// set modes for each pin used
pinMode(countDownLed, OUTPUT);
pinMode(beeper, OUTPUT);
pinMode(pushButton, INPUT);
// Set pins to LOW before starting
digitalWrite(beeper, LOW);
digitalWrite(countDownLed, LOW);
// Defines a function to be sent to by Raspberry Pi
Particle.function("countdown",countdownToggle);
}
////////// LOOP
void loop()
{
// Continually check for a button press
checkButtonPress();
}
// Check for commands coming in for the Particle.function countdown
int countdownToggle(String command) {
if (command=="on") {
//digitalWrite(countDownLed, HIGH);
countDownState = 1;
countDown();
return 1; // Returns status
}
else if (command=="off") {
digitalWrite(countDownLed,LOW);
countDownState = 0;
return 0;
}
else {
return -1;
}
}
////////// COUNTDOWN
void countDown()
{
unsigned long currentMillis = millis();
while(countDownState == 1)
{
for (int i = 1; i <= 20; i++)
{
if (currentMillis - previousMillis >= interval) {
// save the last time
previousMillis = currentMillis;
// if the LED/beeper is off turn it on and vice-versa:
if (countDownLedState == LOW) {
countDownLedState = HIGH;
beeperState = HIGH;
} else {
countDownLedState = LOW;
beeperState = LOW;
}
// set the LED state to the pin:
digitalWrite(countDownLed, countDownLedState);
if (beeperState = HIGH) {
// Send the tone to the beeper pin
tone(beeper, countdownTonePitch, countdownToneLength);
}
}
}
}
}
}
////////// CHECK BUTTON
void checkButtonPress() {
unsigned long currentMillisButton = millis();
if (currentMillisButton - previousMillisButton >= buttonInterval) {
previousMillisButton = currentMillisButton;
pushButtonState = digitalRead(pushButton);
if (pushButtonState == HIGH) {
Particle.publish("buttonPressed", "HIGH"); // Send HIGH to Particle Dashboard if button is pressed
pushButtonState == LOW;
}
}
}