#include "fix_fft.h" //library to perfom the Fast Fourier Transform
#include <SPI.h> //SPI library
#include <Wire.h> //I2C library for OLED
#include <Adafruit_GFX.h> //graphics library for OLED
#include <Adafruit_SSD1306.h> //OLED driver
#include <DHT.h>
//#include <C402_Steam_Air_MQ7_CO.h>
#include <MQ7.h>
// OLED
#define OLED_RESET 4 //OLED reset, not hooked up but required by library
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// DHT22 sensor settings
#define DHTPIN 8
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// CO Sensor Pin
#define COSENSORPIN A1
#define VOLTAGE 5
// Audio
#define AUDIOIN A0
//SteamMQ7 mq7(COSENSORPIN);
//MQ7 mq7(COSENSORPIN, VOLTAGE);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //declare instance of OLED library called display
char im[128], data[128]; //variables for the FFT
char x = 2, ylim = 60; //variables for drawing the graphics
int i = 0, val; //counters
int printsize = 84;
int temperature = 0, humidity = 0;
int text_pos_x = 98;
int text_pos_y = 21;
int text_v_offset = 17;
//string temp_co = "";
int temp_ppm = 0;
void setup()
{
//Serial.begin(9600); //serial comms for debuging
//mq7.calibrate();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //begin OLED @ hex addy 0x3C
display.setTextSize(1); //set OLED text size to 1 (1-6)
display.setTextColor(WHITE); //set text color to white
display.clearDisplay(); //clear display
analogReference(DEFAULT); // Use default (5v) aref voltage.
dht.begin();
};
void loop()
{
int min = 1024, max = 0; //set minumum & maximum ADC values
for (i = 0; i < 128; i++) { //take 128 samples
val = analogRead(AUDIOIN); //get audio from Analog 0
data[i] = val / 4 - 128; //each element of array is val/4-128
im[i] = 0; //
if (val > max) max = val; //capture maximum level
if (val < min) min = val; //capture minimum level
};
fix_fft(data, im, 7, 0);
MQ7 mq7(COSENSORPIN, 5.0);
temperature = (dht.readTemperature());
humidity = (dht.readHumidity());
//temp_co = mq7.read();
//temp_ppm = mq7.readPpm();
display.clearDisplay();
display.drawFastHLine(0, 17, 128, WHITE);
display.drawFastVLine(89, 17, 48, WHITE);
display.drawFastHLine(91, 32, 36, WHITE);
display.drawFastHLine(91, 49, 36, WHITE);
for (i = 1; i < printsize; i++) {
int dat = sqrt(data[i] * data[i] + im[i] * im[i]);
//int pos_x = i * 2 + x;
int pos_x = i + x;
int pos_y = ylim;
int height = ylim - dat;
height = height < 20 ? 20 : height;
display.drawLine(pos_x, pos_y, pos_x, height, WHITE);
};
display.setCursor(text_pos_x, text_pos_y);
display.print(temperature);
display.println("C");
display.setCursor(text_pos_x, text_pos_y + text_v_offset);
display.print(temp_ppm);
display.setCursor(text_pos_x, text_pos_y + (text_v_offset * 2));
display.print(humidity);
display.println("%");
//show the buffer
display.display();
};