The Sensometer: 22+ Sensors

This is the “Sensometer”, a great example of IoT! Get all sensors working at once!

The Sensometer 22+ Sensors

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
× 1
Arduino Mega 2560 & Genuino Mega 2560
Arduino Mega 2560 & Genuino Mega 2560
× 1
Real Time Clock (RTC)
Real Time Clock (RTC)
× 1
ProtoCentral Pulse Oximeter & Heart Rate Sensor based on MAX30100
ProtoCentral Pulse Oximeter & Heart Rate Sensor based on MAX30100
× 1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor – HC-SR04 (Generic)
× 1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
× 1
MyOctopus i2c Barometric Air Pressure Sensor BMP280
MyOctopus i2c Barometric Air Pressure Sensor BMP280
× 1
Photo resistor
Photo resistor
× 1
3D Magnetic Sensor 2Go
Infineon 3D Magnetic Sensor 2Go
× 1
IR receiver (generic)
× 1
IR transmitter (generic)
× 1
UVM-30A UV sensor
× 1
TCS3200 Color Sensor
× 1
Hydrogen Gas Sensor – MQ-8
× 1
Alcohol Gas Sensor – MQ-3
× 1
Carbon Monoxide Sensor – MQ-9
× 1

Software apps and online services

Cayenne
myDevices Cayenne

Story

Triggers & Alerts

This project has (right now) two Triggers, but I could easily put 22 triggers/alerts for every sensor.

  • UV Index: It pulls my blinds down when there are high levels of UV Index
  • Illuminance of Sun: It also pull my blinds down when there is high illuminance (yes, I know UV Index and illumicance of the Sun are proportional, but this is just in case the UV sensor fails; I don’t want a sunburn).

Triggers & Alerts

This project has (right now) two Triggers, but I could easily put 22 triggers/alerts for every sensor.

  • UV Index: It pulls my blinds down when there are high levels of UV Index
  • Illuminance of Sun: It also pull my blinds down when there is high illuminance (yes, I know UV Index and illumicance of the Sun are proportional, but this is just in case the UV sensor fails; I don’t want a sunburn).

Code

Arduino MKR1000

Arduino

//#define CAYENNE_DEBUG         // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMKR1000.h>

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "**********";
// Your network name and password.
char ssid[] = "********";
char password[] = "*****";

void setup()
{
  Serial.begin(9600);
  Cayenne.begin(token, ssid, password);
}

void loop()
{
  Cayenne.run();
  Serial.println("8");
  delay(250);
  if (Serial.available() > 0) {
    Cayenne.virtualWrite(V23, Serial.read());
  }
  Serial.println("9");
  delay(250);
  if (Serial.available() > 0) {
    Cayenne.virtualWrite(V17, Serial.read());
  }
  Serial.println("11");
  delay(250);
  if (Serial.available() > 0) {
    Cayenne.virtualWrite(V20, Serial.read());
  }
  Serial.println("12");
  delay(250);
  if (Serial.available() > 0) {
    Cayenne.virtualWrite(V21, Serial.read());
  }
  Serial.println("13");
  delay(250);
  if (Serial.available() > 0) {
    Cayenne.virtualWrite(V19, Serial.read());
  }
  Serial.println("14");
  delay(250);
  if (Serial.available() > 0) {
    Cayenne.virtualWrite(V18, Serial.read());
  }
  Serial.println("15");
  delay(250);
  if (Serial.available() > 0) {
    Cayenne.virtualWrite(V22, Serial.read());
  }
  Serial.println("16");
  delay(250);
  if (Serial.available() > 0) {
    Cayenne.virtualWrite(V15, Serial.read());
  }
  Serial.println("17");
  delay(250);
  if (Serial.available() > 0) {
    Cayenne.virtualWrite(V16, Serial.read());
  }
  Serial.println("18");
  delay(250);
  if (Serial.available() > 0) {
    Cayenne.virtualWrite(V11, Serial.read());
  }
  Serial.println("19");
  delay(250);
  if (Serial.available() > 0) {
    Cayenne.virtualWrite(V14, Serial.read());
  }
  Serial.println("20");
  delay(250);
  if (Serial.available() > 0) {
    Cayenne.virtualWrite(V24, Serial.read());
  }
  Serial.println("21");
  delay(250);
  if (Serial.available() > 0) {
    Cayenne.virtualWrite(V5, Serial.read());
    Cayenne.virtualWrite(V6, Serial.read());
    Cayenne.virtualWrite(V7, Serial.read());
  }
  Serial.println("25");
  delay(250);
  if (Serial.available() > 0) {
    Cayenne.virtualWrite(V0, Serial.read());
  }
  Serial.println("26");
  delay(250);
  if (Serial.available() > 0) {
    Cayenne.virtualWrite(V1, Serial.read());
  }
  Serial.println("27");
  delay(250);
  if (Serial.available() > 0) {
    Cayenne.virtualWrite(V2, Serial.read());
  }
  Serial.println("28");
  delay(250);
  if (Serial.available() > 0) {
    Cayenne.virtualWrite(V3, Serial.read());
  }
  Serial.println("29");
  delay(250);
  if (Serial.available() > 0) {
    Cayenne.virtualWrite(V4, Serial.read());
  }
 
}
CAYENNE_IN(V8)
{
int value = getValue.asInt();
if (value == 1){
Serial.println("22");  
}
}
CAYENNE_IN(V9)
{
int value = getValue.asInt();
if (value == 1){
Serial.println("23");  
}
}
CAYENNE_IN(V10)
{
int value = getValue.asInt();
if (value == 1){
Serial.println("24");  
}
}

Arduino Mega

Arduino

//SENSOMETER V1.0 REV2
//Project made by @Neoxelox
//----------------------------------------------------|
// LIBRARY SECTION  LIBRARY SECTION  LIBRARY SECTION  |
//----------------------------------------------------|
// [KY-015 | DHT11]
#include <DHT.h>
#include <DHT_U.h>

// [GY-91 | BMP280/MPU9250]
#include <Wire.h>
#include "i2c.h"
#include "i2c_BMP280.h"
BMP280 bmp280;

// [DS3231 | RTC]
#include <avr/pgmspace.h>
#include <SoftwareWire.h>
#include <RtcDS3231.h>

// [KY-022 | IR RECEIVER]
#include <IRLremote.h>

// [KY-005 | IR EMITTER]
#include <IRremote.h>
#include <IRremoteInt.h>

//---------------------------------------------------------------------|
// QUICK SETTINGS   QUICK SETTINGS   QUICK SETTINGS   QUICK SETTINGS   |
//---------------------------------------------------------------------|
//-----------------------------[PINS]----------------------------------|
#define pulsePin A0 // [KY-039 | Pulse Sensor]
#define dhtPin 3 // [KY-015 | DHT11]
SoftwareWire myWire(6, 7); // [DS3231 | RTC] (SDA, SCL) NOT the real I2C pins
#define ultraTrigPin 4 // [HC-SR04 | Ultrasonic Sensor]
#define ultraEchoPin 5 // [HC-SR04 | Ultrasonic Sensor]
#define photoPin A1 // [PHOTORESISTOR]
#define magneticPin A2 // [KY-024 | Magnetic Field]
#define uvPin A3 // [UVM 30A | UltraViolet]
#define soundPin A4 // [HXJ-17 | Sound Sensor]
#define irPin 0 // [KY-022 | IR RECEIVER] 0 = digital pin 2
#define emfPin A5 // [EMF SENSOR] 
#define S0 9 // [TCS320 | Color Sensor]
#define S1 8 // [TCS320 | Color Sensor]
#define S2 11 // [TCS320 | Color Sensor]
#define S3 12 // [TCS320 | Color Sensor]
#define sensorOut 10 // [TCS320 | Color Sensor]
#define irePin 13 // [KY-005 | IR EMITTER]
#define mq8Pin A7 // [MQ8 | Hydrogen Gasses]
#define mq3Pin A8 // [MQ3 | Alcohol/Ethanol Gasses]
#define mq9Pin A9 // [MQ9 | CO Gasses]
#define mq2Pin A10 // [MQ2 | Fuel Gasses]
#define mq135Pin A11 // [MQ135 | CO2/AIR QUALITY Gasses]
//---------------------[TIMER (for DebugMode)]-------------------------|
#define timeDht 1000 // [KY-015 | DHT11]
#define timeRtc 1000 // [DS3231 | RTC]
#define timeUltra 1000 // [HC-SR04 | Ultrasonic Sensor]
#define timePhoto 1000 // [PHOTORESISTOR]
#define timeMagnetic 1000 // [KY-024 | Magnetic Field]
#define timeGy91 1000 // [GY-91 | BMP280/MPU9250]
#define timeUv 1000 // [UVM 30A | UltraViolet]
#define timeSound 1000 // [HXJ-17 | Sound Sensor]
#define timeEmf 1000 // [EMF SENSOR] 
#define timeTcs 1000 // [TCS320 | Color Sensor]
#define timeMq8 1000 // [MQ8 | Hydrogen Gasses]
#define timeMq3 1000 // [MQ3 | Alcohol/Ethanol Gasses]
#define timeMq9 1000 // [MQ9 | CO Gasses]
#define timeMq2 1000 // [MQ2 | Fuel Gasses]
#define timeMq135 1000 // [MQ135 | CO2/AIR QUALITY Gasses]
//-----------------------------[DEBUG]---------------------------------|
#define debugMode false
//----------------------------------------------------------|
// VARIABLES SECTION  VARIABLES SECTION  VARIABLES SECTION  |
//----------------------------------------------------------|
// [KY-039 | Pulse Sensor]
float alpha = 0.75;
int period = 50;
float max = 0.0;
int realbpm = 0;
int shbpm = 0;
int lastbpm = 0;

// [DS3231 | RTC]
RtcDS3231<SoftwareWire> Rtc(myWire);
char weekDay[][4] = {"Dom", "Lun", "Mar", "Mie", "Jue", "Vie", "Sab" };

// [KY-015 | DHT11]
DHT dht (dhtPin, DHT11);

// [HC-SR04 | Ultrasonic Sensor]
long ultraduration;
float ultradistance;
int ultraspeed1;
int ultraspeed2;
int ultraspeed3;
int ultraspeed4;
int ultrarealspeed;
int timersp;

// [KY-024 | Magnetic Field]
int rawfield = 0;
long readedgauss = 0;
#define NOFIELD 535
#define TOMILLIGAUSS 1.953125

// [UVM 30A | UltraViolet]
int uvvalue = 0;
int uvvoltage = 0;
int uvindex = 0;

// [HXJ-17 | Sound Sensor]
int soundvalue = 0;
int soundrefine = 0;
int sounddb = 0;

// [KY-022 | IR RECEIVER]
uint8_t protocolo = 0;
uint16_t direccion = 0;
uint32_t tecla = 0;
uint8_t oldSREG;

// [EMF SENSOR] 
#define emfsample 300          
float emfvalue;                                                          
int emfarray[emfsample];                    
unsigned long emfaveraging;

// [TCS320 | Color Sensor]
int tcsfrequency = 0;

// [KY-005 | IR EMITTER]
IRsend irsend;
IRrecv irrecv(irePin);
int khz = 38;
unsigned int PullBlindsUp[] = {6000, 4200, 560, 760, 560, 560, 360, 1690, 560, 560, 860, 560, 560, 560, 560, 560, 560, 260, 560, 1690, 560, 1890, 560, 560, 560, 1390, 160, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 39416, 9000, 2210, 560 }; 
unsigned int PullBlindsDown[] = {6000, 5600, 860, 760, 560, 560, 360, 2690, 560, 560, 860, 660, 560, 560, 460, 560, 560, 560, 560, 1690, 560, 1890, 560, 560, 560, 1690, 160, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 39416, 9000, 2210, 560 }; 
unsigned int StopBlinds[] = {2000, 3200, 960, 760, 560, 560, 360, 1790, 560, 560, 860, 560, 760, 560, 560, 560, 560, 960, 560, 1690, 560, 1890, 560, 560, 560, 1490, 160, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 39416, 9000, 2210, 560 };
//----------------------------------------------|
// SETUP SECTION  SETUP SECTION  SETUP SECTION  |
//----------------------------------------------|
void setup() {
Serial.begin(9600);

// [HC-SR04 | Ultrasonic Sensor]
pinMode(ultraTrigPin, OUTPUT);
pinMode(ultraEchoPin, INPUT);

// [DS3231 | RTC]
Rtc.Begin();

// [KY-015 | DHT11]
dht.begin();

// [GY-91 | BMP280/MPU9250]
bmp280.initialize();
bmp280.setEnabled(0);
bmp280.triggerMeasurement();

// [PHOTORESISTOR] 
pinMode(photoPin, INPUT);

// [KY-024 | Magnetic Field]
pinMode(magneticPin, INPUT); 

// [UVM 30A | UltraViolet]
pinMode(uvPin, INPUT);

// [HXJ-17 | Sound Sensor]
pinMode(soundPin, INPUT);

// [KY-022 | IR RECEIVER]
IRLbegin<IR_ALL>(irPin);

// [EMF SENSOR]
pinMode(emfPin,INPUT);

// [TCS320 | Color Sensor]
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(sensorOut, INPUT);
  digitalWrite(S0,HIGH);
  digitalWrite(S1,LOW);
// [MQ8 | Hydrogen Gasses]
pinMode(mq8Pin,INPUT);
// [MQ3 | Alcohol/Ethanol Gasses]
pinMode(mq3Pin,INPUT);
// [MQ9 | CO Gasses]
pinMode(mq9Pin,INPUT);
// [MQ2 | Fuel Gasses]
pinMode(mq2Pin,INPUT);
// [MQ135 | CO2/AIR QUALITY Gasses]
pinMode(mq135Pin,INPUT);  
}
//-------------------------------------------|
// LOOP SECTION  LOOP SECTION  LOOP SECTION  |
//-------------------------------------------|
void loop() {
waitForRequest();

}
//----------------------------------------------------|
// REQUEST SECTION  REQUEST SECTION  REQUEST SECTION  |
//----------------------------------------------------|
void waitForRequest(){
if (debugMode == false) {
// [KY-039 | Pulse Sensor]  
shbpm = getbpm();

// [KY-022 | IR RECEIVER]
oldSREG = SREG;
cli();
if (Serial3.available() > 0) {
switch (Serial3.read()) {
  // [DS3231 | RTC]
  case 1: 
  //RtcDateTime timerino = Rtc.GetDateTime();
  //Serial3.println(timerino.Second());
  case 2: 
  //RtcDateTime timerino = Rtc.GetDateTime();
  //Serial3.println(timerino.Minute());
  case 3: 
  //RtcDateTime timerino = Rtc.GetDateTime();
  //Serial3.println(timerino.Hour());
  case 4: 
  //RtcDateTime timerino = Rtc.GetDateTime();
  //Serial3.println(timerino.Day());
  case 5: 
  //RtcDateTime timerino = Rtc.GetDateTime();
  //Serial3.println(weekDay[timerino.DayOfWeek()]);
  case 6: 
  //RtcDateTime timerino = Rtc.GetDateTime();
  //Serial3.println(timerino.Month());
  case 7: 
  //RtcDateTime timerino = Rtc.GetDateTime();
  //Serial3.println(timerino.Year());
  // [KY-039 | Pulse Sensor]
  case 8:  
  if((shbpm > 0) && (shbpm != lastbpm)){
  Serial3.println(shbpm);
  lastbpm = shbpm;
  }
  // [HC-SR04 | Ultrasonic Sensor]  
  case 9:
  Serial3.println(getdistance());
  case 10:
  // Serial.print(getspeed());
  // [KY-015 | DHT11]
  case 11:
  Serial3.println(dht.readTemperature());
  case 12:
  Serial3.print(dht.readHumidity());
  // [GY-91 | BMP280/MPU9250]
  case 13:
  bmp280.awaitMeasurement();
  float pascal;
  bmp280.getPressure(pascal);
  bmp280.triggerMeasurement();
  Serial3.println(pascal);
  case 14:
  bmp280.awaitMeasurement();
  static float meters;
  bmp280.getAltitude(meters);
  bmp280.triggerMeasurement();
  Serial3.println(meters);
  // [PHOTORESISTOR]
  case 15:
  Serial3.println(int(Light(analogRead(photoPin))));
  // [KY-024 | Magnetic Field]
  case 16:
  Serial3.println(readMagnetic());
  // [UVM 30A | UltraViolet]
  case 17:
  Serial3.println(readUV());
  // [HXJ-17 | Sound Sensor]
  case 18:
  Serial3.println(readSound());
  // [KY-022 | IR RECEIVER]
  case 19:
  if(protocolo!=0){
   Serial3.println(protocolo);
   Serial3.println(direccion);
   Serial3.println(tecla);
    protocolo=0;
  }
  SREG = oldSREG;
  // [EMF SENSOR]
  case 20:
  Serial3.println(readEmf());
  // [TCS320 | Color Sensor]
  case 21:
  Serial3.println(readColorRed());
  Serial3.println(readColorGreen());
  Serial3.println(readColorBlue());
  // [KY-005 | IR EMITTER]
  case 22:
  setBlindsUP();
  case 23:
  setBlindsDOWN();
  case 24:
  setBlindsSTOP();
  // [MQ8 | Hydrogen Gasses]
  case 25:
  Serial3.println(analogRead(mq8Pin), DEC);
  // [MQ3 | Alcohol/Ethanol Gasses]
  case 26:
  Serial3.println(analogRead(mq3Pin), DEC);
  // [MQ9 | CO Gasses]
  case 27:
  Serial3.println(analogRead(mq9Pin), DEC);
  // [MQ2 | Fuel Gasses]
  case 28:
  Serial3.println(analogRead(mq2Pin), DEC);
  // [MQ135 | CO2/AIR QUALITY Gasses]
  case 29:
  Serial3.println(analogRead(mq135Pin), DEC);
}
}
}
else if (debugMode == true) {
// [DS3231 | RTC]
static unsigned long RTC1read = millis();
if (millis() >= RTC1read + timeRtc) {
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
Serial.println();

//RtcTemperature temp = Rtc.GetTemperature();
//Serial.print(temp.AsFloat());
//Serial.println("C");
 RTC1read = millis();
}
// [KY-039 | Pulse Sensor]  
shbpm = getbpm();
if((shbpm > 0) && (shbpm != lastbpm)){
  Serial.print("BPM: ");
  Serial.println(shbpm);
  lastbpm = shbpm;
}
// [KY-022 | IR RECEIVER]
oldSREG = SREG;
cli();

if(protocolo!=0){
    Serial.print("COMUNICACION INFRAROJOS: ");
    Serial.print("Protocolo: ");
    Serial.print(protocolo);
    Serial.print(" Direccion: ");
    Serial.print(direccion, HEX);
    Serial.print(" Tecla: ");
    Serial.println(tecla);
    protocolo=0;
  }

  SREG = oldSREG;
// [HC-SR04 | Ultrasonic Sensor]
static unsigned long ultraread = millis();
if (millis() >= ultraread + timeUltra) {
  Serial.print("Distance: ");
  Serial.print(getdistance());
  Serial.println(" cm");
 // Serial.print("Speed: ");
 // Serial.print(getspeed());
 // Serial.println("m/s");
   ultraread = millis();
  }
// [KY-015 | DHT11] 
static unsigned long dhtread = millis();
if (millis() >= dhtread + timeDht) {
    Serial.print("Temperature: ");
    Serial.print(dht.readTemperature());
    Serial.println(" C");
    Serial.print("Humidity: ");
    Serial.print(dht.readHumidity());
    Serial.println("%");
    dhtread = millis();
  }
// [GY-91 | BMP280/MPU9250]
static unsigned long gy91read = millis();
if (millis() >= gy91read + timeGy91) {
  
 bmp280.awaitMeasurement();

    float temperature;
    bmp280.getTemperature(temperature);

    float pascal;
    bmp280.getPressure(pascal);

    static float meters;
    bmp280.getAltitude(meters);
    

    bmp280.triggerMeasurement();

    Serial.print("Height: ");
    Serial.print(meters);
    Serial.print(" m");
    Serial.print(" Pressure: ");
    Serial.print(pascal);
    Serial.print(" Pa; T: ");
    Serial.print(temperature);
    Serial.println(" C");
    gy91read = millis();
}
// [PHOTORESISTOR] 
static unsigned long photoread = millis();
if (millis() >= photoread + timePhoto) {
Serial.print(int(Light(analogRead(photoPin))));
Serial.println(" lux");
  photoread = millis();
}
// [KY-024 | Magnetic Field]
static unsigned long magneticread = millis();
if (millis() >= magneticread + timeMagnetic) {
 readedgauss = readMagnetic();
 Serial.print(readedgauss);
  Serial.print(" Gauss ");

  if (readedgauss > 0)     Serial.println("(POLO SUR)");
  else if(readedgauss < 0) Serial.println("(POLO NORTE)");
  else               Serial.println();
  magneticread = millis();
}
// [UVM 30A | UltraViolet]
static unsigned long ultravioletread = millis();
if (millis() >= ultravioletread + timeUv) {
  Serial.print("INDICE UV: ");
  Serial.println(readUV());
  ultravioletread = millis();
}
// [HXJ-17 | Sound Sensor]
static unsigned long soundread = millis();
if (millis() >= soundread + timeSound) {
  Serial.print(readSound());
  Serial.println(" Db");
 soundread = millis();
}
//
static unsigned long emfread = millis();
if (millis() >= emfread + timeEmf) {
  Serial.print(readEmf());
  Serial.println(" µV/m");
  emfread = millis();
}
// [TCS320 | Color Sensor]
static unsigned long colorread = millis();
if (millis() >= colorread + timeTcs) {
  Serial.print("ROJO: ");
  Serial.println(readColorRed());
  Serial.print("VERDE: ");
  Serial.println(readColorGreen());
  Serial.print("AZUL: ");
  Serial.println(readColorBlue());
  colorread = millis();
}
// [MQ8 | Hydrogen Gasses]
static unsigned long mq8read = millis();
if (millis() >= mq8read + timeMq8) {
  Serial.print(analogRead(mq8Pin), DEC);
  Serial.println(" ppm");
  mq8read = millis();
}
// [MQ3 | Alcohol/Ethanol Gasses]
static unsigned long mq3read = millis();
if (millis() >= mq3read + timeMq3) {
  Serial.print(analogRead(mq3Pin), DEC);
  Serial.println(" ppm");
  mq3read = millis();
}
// [MQ9 | CO Gasses]
static unsigned long mq9read = millis();
if (millis() >= mq9read + timeMq9) {
  Serial.print(analogRead(mq9Pin), DEC);
  Serial.println(" ppm");
  mq9read = millis();
}
// [MQ2 | Fuel Gasses]
static unsigned long mq2read = millis();
if (millis() >= mq2read + timeMq2) {
  Serial.print(analogRead(mq2Pin), DEC);
  Serial.println(" ppm");
  mq2read = millis();
}
// [MQ135 | CO2/AIR QUALITY Gasses]
static unsigned long mq135read = millis();
if (millis() >= mq135read + timeMq135) {
  Serial.print(analogRead(mq135Pin), DEC);
  Serial.println(" ppm");
  mq135read = millis();
}
}
}
//----------------------------------------------|
// PULSE SECTION  PULSE SECTION  PULSE SECTION  |
//----------------------------------------------|
int getbpm(){
  
  static float oldValue = 500;
  static unsigned long bpmMills = millis();
  static int bpm = 0;
  static unsigned long timeBetweenBeats = millis();
  int minDelayBetweenBeats = 400;
  int rawValue = analogRead((unsigned char) pulsePin);

  // MATHS FOR READING A PEAK
  float value = alpha * oldValue + (1 - alpha) * rawValue;
  float change = value - oldValue;
  oldValue = value;

   if ((change >= max) && (millis() > timeBetweenBeats + minDelayBetweenBeats)) {

    max = change;
    
    timeBetweenBeats = millis();
    bpm++;
  }
  else {
  }
  max = max * 0.97;

  if (millis() >= bpmMills + 15000) {
    realbpm = bpm * 4;
    realbpm = realbpm - 20;
    bpm = 0;
    bpmMills = millis();
  }
  delay(period);
  return(realbpm);
}
//-------------------------------------------------------------|
// ULTRASONIC SECTION  ULTRASONIC SECTION  ULTRASONIC SECTION  |
//-------------------------------------------------------------|
float getdistance(){
  digitalWrite(ultraTrigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(ultraTrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(ultraTrigPin, LOW);
  ultraduration = pulseIn(ultraEchoPin, HIGH);
  ultradistance = ultraduration*0.034/2;
  return(ultradistance);
}

/*float getspeed(){
  while(timersp != 1000){
    
    timersp++;
    }
  digitalWrite(ultraTrigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(ultraTrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(ultraTrigPin, LOW);
  ultraduration = pulseIn(ultraEchoPin, HIGH);

  
  ultraspeed1 = getdistance();
  ultraspeed1 = ultraspeed1/100;
  delay(500);
  ultraspeed2 = getdistance();
  ultraspeed2 = ultraspeed2/100;
  ultrarealspeed = (ultraspeed2 - ultraspeed1)/(500/1000);
  return(ultrarealspeed);
}*/
//----------------------------------------------------------------------|
// PHOTORESISTOR SECTION  PHOTORESISTOR SECTION  PHOTORESISTOR SECTION  |
//----------------------------------------------------------------------|
double Light (int RawADC0){
double Vout=RawADC0*0.0048828125;
int lux=(2500/Vout-500)/10;
return lux;
}
//-------------------------------------------------------------------------|
// MAGNETIC FIELD SECTION  MAGNETIC FIELD SECTION  MAGNETIC FIELD SECTION  |
//-------------------------------------------------------------------------|
long readMagnetic(){
rawfield = analogRead(magneticPin);

  long compensated = rawfield - NOFIELD;
  long gauss = compensated * TOMILLIGAUSS;
  return(gauss);  
}
//----------------------------------------------------------|
// DS3231RTC SECTION  DS3231RTC SECTION  DS3231RTC SECTION  |
//----------------------------------------------------------|
#define countof(a) (sizeof(a) / sizeof(a[0]))

void printDateTime(const RtcDateTime& dt)
{
    char datestring[21];

    snprintf_P(datestring, 
            countof(datestring),
            PSTR("%02u/%02u/%04u %02u:%02u:%02u "),
            dt.Day(),
            dt.Month(),
            dt.Year(),
            dt.Hour(),
            dt.Minute(),
            dt.Second() );
    Serial.print(datestring);
    Serial.print(weekDay[dt.DayOfWeek()]);
}
//----------------------------------------------------------------|
// UltraViolet SECTION  UltraViolet SECTION  UltraViolet SECTION  |
//----------------------------------------------------------------|
int readUV(){
  uvvalue = analogRead(uvPin);
  uvvoltage = (uvvalue * (5.0 / 1023.0)) * 1000;
  if(uvvoltage<227){
    uvindex = 0;
  }
  if(uvvoltage>=227){
    uvindex = 1;
  }
  if(uvvoltage>=318){
    uvindex = 2;
  }
  if(uvvoltage>=408){
    uvindex = 3;
  }
  if(uvvoltage>=503){
    uvindex = 4;
  }
  if(uvvoltage>=606){
    uvindex = 5;
  }
  if(uvvoltage>=669){
    uvindex = 6;
  }
  if(uvvoltage>=795){
    uvindex = 7;
  }
  if(uvvoltage>=881){
    uvindex = 8;
  }
  if(uvvoltage>=976){
    uvindex = 9;
  }
  if(uvvoltage>=1079){
    uvindex = 10;
  }
  if(uvvoltage>=1170){
    uvindex = 11;
  }
  return(uvindex);
}
//-------------------------------------------------------------------|
// Sound Sensor SECTION  Sound Sensor SECTION  Sound Sensor SECTION  |
//-------------------------------------------------------------------|
int readSound(){
  soundvalue = analogRead(soundPin);
  soundrefine = abs(536 - soundvalue);
  sounddb = 17 + (soundrefine * 15);
  return(sounddb);
}
//----------------------------------------------------------------|
// IR Receiver SECTION  IR Receiver SECTION  IR Receiver SECTION  |
//----------------------------------------------------------------|
void IREvent (uint8_t protocol, uint16_t address, uint32_t command){
  protocolo = protocol;
  direccion = address;
  tecla = command;
}
//-------------------------------------------------------------|
// EMF Sensor SECTION  EMF Sensor SECTION  EMF Sensor SECTION  |
//-------------------------------------------------------------|
int readEmf(){                   
for(int i = 0; i < emfsample; i++){              
  emfarray[i] = analogRead(emfPin);      
  emfaveraging += emfarray[i];  
  }              
  emfvalue = emfaveraging / emfsample;                  
  emfvalue = constrain(emfvalue, 0, 100);                                                    
  emfaveraging = 0;    
  return(emfvalue);              
}
//-------------------------------------------------------------------|
// Color Sensor SECTION  Color Sensor SECTION  Color Sensor SECTION  |
//-------------------------------------------------------------------|
int readColorRed(){
  digitalWrite(S2,LOW);
  digitalWrite(S3,LOW);
  tcsfrequency = pulseIn(sensorOut, LOW);
  tcsfrequency = map(tcsfrequency, 25,72,255,0);
  delay(100);
  return(tcsfrequency);
}
int readColorGreen(){
  digitalWrite(S2,HIGH);
  digitalWrite(S3,HIGH);
  tcsfrequency = pulseIn(sensorOut, LOW);
  tcsfrequency = map(tcsfrequency, 30,90,255,0);
  delay(100);
  return(tcsfrequency);
}
int readColorBlue(){
  digitalWrite(S2,LOW);
  digitalWrite(S3,HIGH);
  tcsfrequency = pulseIn(sensorOut, LOW);
  tcsfrequency = map(tcsfrequency, 25,70,255,0);
  delay(100);
  return(tcsfrequency);
}
//-------------------------------------------------------------|
// IR Emitter SECTION  IR Emitter SECTION  IR Emitter SECTION  |
//-------------------------------------------------------------|
void setBlindsUP(){
  irsend.sendRaw(PullBlindsUp, sizeof(PullBlindsUp) / sizeof(PullBlindsUp[0]), khz);
  }
void setBlindsDOWN(){
  irsend.sendRaw(PullBlindsUp, sizeof(PullBlindsUp) / sizeof(PullBlindsUp[0]), khz);
  }
void setBlindsSTOP(){
  irsend.sendRaw(PullBlindsDown, sizeof(PullBlindsDown) / sizeof(PullBlindsDown[0]), khz);
  }

About The Author

Ibrar Ayyub

I am an experienced technical writer with a Master's degree in computer science from BZU Multan University. I have written for various industries, mainly home automation and engineering. My writing style is clear and simple, and I am skilled in using infographics and diagrams. I am a great researcher and am able to present information in a well-organized and logical manner.

Follow Us:
LinkedinTwitter
Scroll to Top