// inlcude librqries needed:
#include <SoftwareSerial.h>
#include <Servo.h>
// Initiate Servo oject
Servo window_control;
// These constants won't change:
const int sensorPin = A0; // pin that the sensor is attached to
const int LEDPIN = 9; // pin that the LED is attached to
const int ZU = 10; // Position of the servo motor for start (Zu)
const int AUF = 30; // Position of the servo motor for end - position (a18uf)
const int BAD_AIR = 220; // Default Wert empirisch ermittelt fr schlechte Luft
const int GOOD_AIR = 150; // Default Wert empirisch ermittelt fr gute Luft
const int OPEN = 1; // Define value for "OPEN" using the button
const int CLOSED = 0; // Define value for "CLOSED" using the button
const int TASTE_IN = 7; // define digital input channel to connect the button
// variables:
int window_pos = 0; // Variable controlling the window position (open and closed)
float sensorValue = 0; // the sensor value
float bad_air = BAD_AIR; // Loaad limit value with default
float good_air = GOOD_AIR; // Loaad limit value with default
int window_status = CLOSED; // Variable storing window status (closed or open)
// ---------------------
// Setup routine:
// ---------------------
void setup() {
// declare the Input nd output pins used on Arduino:
pinMode(TASTE_IN, INPUT); // sets the digital pin 7 as input
pinMode(LEDPIN, OUTPUT); // Set external LED control
window_control.attach(10); // declare usage of PWM pin "10"
Serial.begin(9600); // initialize the debugging monitor for development purposes
Window_close(); // Close window at start
start_sensor(); // Sensor ein paar mal einlesen, bis er gute Werte erzeugt.
}
// ---------------------
// endless main- loop:
// ---------------------
void loop() {
digitalWrite(LEDPIN, HIGH); // Light control LED
Read_CO2_Sensor(); // Read in the CO2 senor value
Calibrate(); // Set a new current "good value" to calibrate the mechanism
Serial.println(sensorValue); // send sensor value to debugging window
if(sensorValue < good_air) // In case current sensor value is better as worst "good air" limit
{
if(window_status != CLOSED) // Check if window is not closed already
{
Window_close(); // close the window
window_status = CLOSED; // remember it was closed
}
}
else if (sensorValue > bad_air) // Check if current sensor value is worse as allowed bad air limit
{
if(window_status != OPEN) // Check if window is open already
{
Window_open(); // open the window
window_status = OPEN; // remember it was closed
}
}
digitalWrite(LEDPIN, LOW); // Reset control LED
}
// ---------------------
// Supporting functions:
// ---------------------
// --------------------------------------------------------------------
// Function Checking for settinng new "good air limit"
// --------------------------------------------------------------------
void Calibrate(void)
{
int i;
int taste;
taste = digitalRead(TASTE_IN);
//Serial.print(" Taste: "); // for better redability in debug window
//Serial.print(taste); // send sensor value to debugging window
Serial.print(" Limit: ");
Serial.print(abs(good_air)); // send current good air - limit to debug window
Serial.print(" Limit: ");
Serial.print(abs(bad_air)); // send current good air - limit to debug window
Serial.print(" Act-Value: ");
if(taste == 0)
{
good_air = sensorValue; // set the new good_air limit to the current value (after windos had been opened for a while)
bad_air = good_air + 30; // Set the jysteresis to 40PPM
for(i=0;i<10;i++){ // Start control LED to blink showing new calibratin was made sucessfully
digitalWrite(LEDPIN, LOW);
delay(50);
digitalWrite(LEDPIN, HIGH);
delay(50);
}
}
}
// --------------------------------------------------------------------
// Function to read in analog sensor signal for a while as
// first measurements are not accurate
// --------------------------------------------------------------------
void start_sensor(void)
{
const int NUM_OF_READ = 1000; // number of read cycles not used for "window" control
int i=0; // because the sensor must get warmed up first.
for(i=0;i<NUM_OF_READ;i++) // for loop
{
analogRead(sensorPin); // read the sensor in
delay(10); // delay by 10ms
}
}
// --------------------------------------------------------------------
// Function to read in analog sensor signal and make a "Mittelwert"
// This is because the sensor signal turned out to be very unstable
// --------------------------------------------------------------------
void Read_CO2_Sensor(void)
{
const int NUM_OF_READ = 200;
float new_value = 0.0;
int i=0;
for(i=0;i<NUM_OF_READ;i++) // for loop reading in NUM_OF_READ sensor vlues and make average
{ // as the sensor is not really stable
new_value = analogRead(sensorPin); // read the sensor in
sensorValue = sensorValue + new_value; // add to the last sum of sensor values
delay(10); // delay by 10ms
}
sensorValue = sensorValue/NUM_OF_READ; // devide by number of read ins
}
// --------------------------------------------------------------------
// Function closing the window by control of servo motor
// --------------------------------------------------------------------
void Window_close(void)
{
window_control.attach(10); // declare usage of PWM pin "10"
for(window_pos = ZU; window_pos <= AUF; window_pos++)
{
window_control.write(window_pos);
delay(25);
}
Serial.println(" - OPEN -"); // Show Closing status on debug window
window_control.attach(11); // declare usage of PWM pin to an unused pin "11" to eliminate permanent noise of motor
}
// --------------------------------------------------------------------
// Function opening the window by control of servo motor
// --------------------------------------------------------------------
void Window_open(void)
{
tone(3, 70); // Make sound with the buzzer
window_control.attach(10); // declare usage of PWM pin "10"
for(window_pos = AUF; window_pos >= ZU; window_pos--)
{
window_control.write(window_pos);
delay(25);
}
Serial.println(" - CLOSE -"); // Show Closing status on debug window
window_control.attach(11); // declare usage of PWM pin to an unused pin "11" to eliminate permanent noise of motor
noTone(3); // Stop sound
}
It shows the level of CO2 when the concentration or cognitive abilities decrese.