Hi everyone, I'm having a strange problem that I can't figure out. I have a veriable declared but when I try to use it in an if() statement in a loop() I get "Compilation error: 'previusMillis' was not declared in this scope"
My code so far is:
#include <Wire.h>
#include "MAX30105.h"
#include "heartRate.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
MAX30105 particleSensor;
const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE]; //Array of heart rates
byte rateSpot = 0;
long lastBeat = 0; //Time at which the last beat occurred
float beatsPerMinute;
int beatAvg;
float temperature;
int temperatureCycleCounter = 0;
int bpmXPixel = 5;
int bpmYPixel = 5;
int i;
unsigned long previousMillis = 0;
const long interval = 5000;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display)
const unsigned char PROGMEM frame0 [] = {0x00, 0x00, 0x00, 0x00, 0x1E, 0x78, 0x21, 0x84, 0x40, 0x02, 0x40, 0x02, 0x40, 0x02, 0x20, 0x04, 0x20, 0x04, 0x10, 0x08, 0x08, 0x10, 0x04, 0x20, 0x02, 0x40, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00};
const unsigned char PROGMEM frame1 [] = {0x00, 0x00, 0x00, 0x00, 0x0E, 0x70, 0x11, 0x88, 0x20, 0x04, 0x20, 0x04, 0x21, 0x84, 0x21, 0x84, 0x11, 0x88, 0x08, 0x10, 0x04, 0x20, 0x02, 0x40, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
const unsigned char PROGMEM frame2 [] = {0x00, 0x00, 0x00, 0x00, 0x0E, 0x70, 0x11, 0x88, 0x20, 0x04, 0x23, 0xC4, 0x27, 0xE4, 0x17, 0xE8, 0x13, 0xC8, 0x09, 0x90, 0x04, 0x20, 0x02, 0x40, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
const unsigned char PROGMEM frame3 [] = {0x00, 0x00, 0x1E, 0x78, 0x21, 0x84, 0x40, 0x02, 0x46, 0x62, 0x4F, 0xF2, 0x4F, 0xF2, 0x47, 0xE2, 0x47, 0xE2, 0x23, 0xC4, 0x11, 0x88, 0x08, 0x10, 0x04, 0x20, 0x02, 0x40, 0x01, 0x80, 0x00, 0x00};
const unsigned char PROGMEM frame4 [] = {0x00, 0x00, 0x00, 0x00, 0x1E, 0x78, 0x3F, 0xFC, 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE, 0x3F, 0xFC, 0x3F, 0xFC, 0x1F, 0xF8, 0x0F, 0xF0, 0x07, 0xE0, 0x03, 0xC0, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00};
const unsigned char PROGMEM frame5 [] = {0x00, 0x00, 0x1E, 0x78, 0x3F, 0xFC, 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE, 0x3F, 0xFC, 0x3F, 0xFC, 0x1F, 0xF8, 0x0F, 0xF0, 0x07, 0xE0, 0x03, 0xC0, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00};
const unsigned char PROGMEM frame6 [] = {0x00, 0x00, 0x1E, 0x78, 0x3F, 0xFC, 0x7F, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFE, 0x7F, 0xFE, 0x3F, 0xFC, 0x1F, 0xF8, 0x0F, 0xF0, 0x07, 0xE0, 0x03, 0xC0, 0x01, 0x80, 0x00, 0x00};
const unsigned char PROGMEM frame7 [] = {0x00, 0x00, 0x00, 0x00, 0x1E, 0x78, 0x21, 0x84, 0x46, 0x62, 0x4F, 0xF2, 0x4F, 0xF2, 0x27, 0xE4, 0x27, 0xE4, 0x13, 0xC8, 0x09, 0x90, 0x04, 0x20, 0x02, 0x40, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00};
const unsigned char PROGMEM frame8 [] = {0x00, 0x00, 0x00, 0x00, 0x1E, 0x78, 0x21, 0x84, 0x40, 0x02, 0x43, 0xC2, 0x47, 0xE2, 0x27, 0xE4, 0x23, 0xC4, 0x11, 0x88, 0x08, 0x10, 0x04, 0x20, 0x02, 0x40, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00};
const unsigned char PROGMEM frame9 [] = {0x00, 0x00, 0x00, 0x00, 0x1E, 0x78, 0x21, 0x84, 0x40, 0x02, 0x40, 0x02, 0x41, 0x82, 0x21, 0x84, 0x21, 0x84, 0x10, 0x08, 0x08, 0x10, 0x04, 0x20, 0x02, 0x40, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00};
const unsigned char PROGMEM frame10 [] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
static const unsigned char PROGMEM temp[] =
{
0b11100011, 0b10000000,
0b10100100, 0b01000000,
0b11100100, 0b00000000,
0b00000100, 0b00000000,
0b00000100, 0b00000000,
0b00000100, 0b01000000,
0b00000011, 0b10000000,
};
void setup()
{
Serial.begin(9600);
Serial.println("Initializing...");
// Initialize sensor
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
{
Serial.println("MAX30105 was not found. Please check wiring/power. ");
while (1);
}
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
while (1); // Don't proceed, loop forever
}
particleSensor.setup(); //Configure sensor with default settings
particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
temperature = particleSensor.readTemperature();
display.setTextColor(WHITE);
display.setTextSize(1);
display.display();
delay(1500); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
}
void loop() {
unsigned long currentMillis = millis();
long irValue = particleSensor.getIR();
if (irValue > 50000 && checkForBeat(irValue) == true) {
heartBeat();
long delta = millis() - lastBeat;
lastBeat = millis();
beatsPerMinute = 60 / (delta / 1000.0);
if (beatsPerMinute < 255 && beatsPerMinute > 20) {
rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
rateSpot %= RATE_SIZE; //Wrap variable
//Take average of readings
beatAvg = 0;
for (byte x = 0 ; x < RATE_SIZE ; x++)
beatAvg += rates[x];
beatAvg /= RATE_SIZE;
}
}
if (irValue < 50000) {
beatAvg = 0;
}
if (currentMillis - previusMillis >= interval) {
previousMillis = currentMillis;
display.clearDisplay();
display.drawBitmap(bpmXPixel, bpmYPixel,frame0,16,16, 1);
display.setCursor(40,10);
display.print(beatAvg);
display.drawBitmap(5, 25,temp,16,7, 1);
display.setCursor(40, 25);
display.print(temperature, 1);
display.display();
}
}
void heartBeat() {
for (i = 0;i < 4;i++) {
display.drawBitmap(bpmXPixel, bpmYPixel,frame10,16,16, 0);
display.drawBitmap(bpmXPixel, bpmYPixel,frame0,16,16, 1);
display.display();
}
display.drawBitmap(bpmXPixel, bpmYPixel,frame10,16,16, 0);
display.drawBitmap(bpmXPixel, bpmYPixel,frame1,16,16, 1);
display.display();
display.drawBitmap(bpmXPixel, bpmYPixel,frame10,16,16, 0);
display.drawBitmap(bpmXPixel, bpmYPixel,frame2,16,16, 1);
display.display();
display.drawBitmap(bpmXPixel, bpmYPixel,frame10,16,16, 0);
display.drawBitmap(bpmXPixel, bpmYPixel,frame3,16,16, 1);
display.display();
for (i = 0; i < 11;i++) {
display.drawBitmap(bpmXPixel, bpmYPixel,frame10,16,16, 0);
display.drawBitmap(bpmXPixel, bpmYPixel,frame4,16,16, 1);
display.display();
}
display.drawBitmap(bpmXPixel, bpmYPixel,frame10,16,16, 0);
display.drawBitmap(bpmXPixel, bpmYPixel,frame5,16,16, 1);
display.display();
display.drawBitmap(bpmXPixel, bpmYPixel,frame10,16,16, 0);
display.drawBitmap(bpmXPixel, bpmYPixel,frame6,16,16, 1);
display.display();
display.drawBitmap(bpmXPixel, bpmYPixel,frame10,16,16, 0);
display.drawBitmap(bpmXPixel, bpmYPixel,frame5,16,16, 1);
display.display();
display.drawBitmap(bpmXPixel, bpmYPixel,frame10,16,16, 0);
display.drawBitmap(bpmXPixel, bpmYPixel,frame4,16,16, 1);
display.display();
display.drawBitmap(bpmXPixel, bpmYPixel,frame10,16,16, 0);
display.drawBitmap(bpmXPixel, bpmYPixel,frame3,16,16, 1);
display.display();
display.drawBitmap(bpmXPixel, bpmYPixel,frame10,16,16, 0);
display.drawBitmap(bpmXPixel, bpmYPixel,frame7,16,16, 1);
display.display();
display.drawBitmap(bpmXPixel, bpmYPixel,frame10,16,16, 0);
display.drawBitmap(bpmXPixel, bpmYPixel,frame8,16,16, 1);
display.display();
display.drawBitmap(bpmXPixel, bpmYPixel,frame10,16,16, 0);
display.drawBitmap(bpmXPixel, bpmYPixel,frame9,16,16, 1);
display.display();
for (i = 0;i < 3;i++) {
display.drawBitmap(bpmXPixel, bpmYPixel,frame10,16,16, 0);
display.drawBitmap(bpmXPixel, bpmYPixel,frame0,16,16, 1);
display.display();
}
}
this variable creates the error
unsigned long previousMillis = 0;
if (currentMillis - previusMillis >= interval) {
what is the underlying problem.