Created
February 4, 2015 11:24
-
-
Save next-marianmoldovan/2866adb99febf32f16e0 to your computer and use it in GitHub Desktop.
Office monitor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <LedControl.h> | |
#include <AirQuality.h> | |
#include <Arduino.h> | |
#include <Firmata.h> | |
#include <Boards.h> | |
#include <math.h> | |
#include <DHT.h> | |
#define RED 12 | |
#define GREEN 13 | |
#define SOUND_SENSOR A4 | |
#define LIGHT_SENSOR 0 | |
#define POLLUTION_SENSOR A0 | |
#define DHTPIN A2 | |
#define DHTTYPE DHT11 | |
AirQuality airqualitysensor; | |
int current_quality =-1; | |
/* | |
MAX72XX | |
pin 10 is connected to the DataIn | |
pin 8 is connected to the CLK | |
pin 9 is connected tp CS | |
*/ | |
LedControl lc=LedControl(12,10,11,1); | |
DHT dht(DHTPIN, DHTTYPE); | |
float lightSensor; | |
int staticValues[] = { | |
B00000000, | |
B10000000, | |
B11000000, | |
B11100000, | |
B11110000, | |
B11111000, | |
B11111100, | |
B11111110, | |
B11111111 | |
}; | |
// the setup function runs once when you press reset or power the board | |
void setup() { | |
//pinMode(RED, OUTPUT); | |
//pinMode(GREEN, OUTPUT); | |
pinMode(SOUND_SENSOR, INPUT); | |
//pinMode(LIGHT_SENSOR, INPUT); | |
airqualitysensor.init(POLLUTION_SENSOR); | |
dht.begin(); | |
lc.shutdown(0,false); | |
lc.setIntensity(0,8); | |
lc.clearDisplay(0); | |
Serial.begin(9600); | |
} | |
// the loop function runs over and over again forever | |
void loop() { | |
tempHumedad(); | |
sound(); | |
pollution(); | |
//tone(10, 440, 1000); | |
/*int sensorValue = analogRead(LIGHT_SENSOR); | |
lightSensor = (float)(1023-sensorValue)*10/sensorValue; | |
Serial.print(sensorValue); | |
Serial.println(" light");*/ | |
delay(10000); | |
} | |
void pollution(){ | |
current_quality=airqualitysensor.slope(); | |
if (current_quality >= 0)// if a valid data returned. | |
{ | |
if (current_quality==0){ | |
Serial.println("High pollution! Force signal active"); | |
lc.setRow(0,6,10); | |
lc.setRow(0,7,10); | |
} | |
else if (current_quality==1){ | |
Serial.println("High pollution!"); | |
lc.setRow(0,6,8); | |
lc.setRow(0,7,8); | |
} | |
else if (current_quality==2){ | |
Serial.println("Low pollution!"); | |
lc.setRow(0,6,4); | |
lc.setRow(0,7,4); | |
} | |
else if (current_quality ==3){ | |
Serial.println("Fresh air"); | |
lc.setRow(0,6,0); | |
lc.setRow(0,7,0); | |
} | |
} | |
} | |
ISR(TIMER2_OVF_vect) | |
{ | |
if(airqualitysensor.counter==122)//set 2 seconds as a detected duty | |
{ | |
airqualitysensor.last_vol=airqualitysensor.first_vol; | |
airqualitysensor.first_vol=analogRead(POLLUTION_SENSOR); | |
Serial.print(airqualitysensor.first_vol); | |
Serial.println("Poll"); | |
airqualitysensor.counter=0; | |
airqualitysensor.timer_index=1; | |
PORTB=PORTB^0x20; | |
} | |
else | |
{ | |
airqualitysensor.counter++; | |
} | |
} | |
void sound(){ | |
int soundValue = analogRead (SOUND_SENSOR); | |
Serial.print(soundValue); | |
Serial.println(" sound"); | |
int value = soundValue / 100; | |
lc.setRow(0,4,staticValues[value]); | |
lc.setRow(0,5,staticValues[value]); | |
} | |
void tempHumedad(){ | |
float h = dht.readHumidity(); | |
float t = dht.readTemperature(); | |
// check if returns are valid, if they are NaN (not a number) then something went wrong! | |
if (isnan(t) || isnan(h)) { | |
Serial.println("Failed to read from DHT"); | |
} | |
else { | |
Serial.print(t); | |
Serial.println(" temp"); | |
Serial.print(h); | |
Serial.println(" humi"); | |
int value = 0; | |
if(t > 24) | |
value = t - 24; | |
else if(t < 17) | |
value = 17 - t; | |
lc.setRow(0,0,staticValues[value]); | |
lc.setRow(0,1,staticValues[value]); | |
value = 0; | |
if(h > 50) | |
value = 50 - h; | |
else if(h < 40) | |
value = 40 - h; | |
value = value / 2; | |
lc.setRow(0,2,staticValues[value]); | |
lc.setRow(0,3,staticValues[value]); | |
} | |
} | |
void paintCollumns(int col1, int col2, int value){ | |
for(int i = 0; i < value; i++){ | |
lc.setLed(0,col1,i,true); | |
lc.setLed(0,col2,i,true); | |
} | |
for(int i = value; i < 8; i++){ | |
lc.setLed(0,col1,i,false); | |
lc.setLed(0,col2,i,false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment