~alienagain/future_wear

0076be71e6ca98cd23e9de27d1d587f195fdde3a — paula 2 months ago 4154e02 master
final code
2 files changed, 136 insertions(+), 63 deletions(-)

A letsbreathtogether/final.ino
D letsbreathtogether/sensor.ino
A letsbreathtogether/final.ino => letsbreathtogether/final.ino +136 -0
@@ 0,0 1,136 @@
/*********************************************************************************************************
*
* File                : DustSensor
* Hardware Environment: 
* Build Environment   : Arduino
* Version             : V1.0.5-r2
* By                  : WaveShare
*
*                                  (c) Copyright 2005-2011, WaveShare
*                                       http://www.waveshare.net
*                                       http://www.waveshare.com   
*                                          All Rights Reserved
*
*********************************************************************************************************/
#define        COV_RATIO                       0.2            //ug/mmm / mv
#define        NO_DUST_VOLTAGE                 400            //mv
#define        SYS_VOLTAGE                     5000           


/*
I/O define
*/
const int iled = 7;                                            //drive the led of sensor
const int vout = 0;                                            //analog input

/*
variable
*/
float density, voltage;
int   adcvalue;

/*
private function
*/
int Filter(int m)
{
  static int flag_first = 0, _buff[10], sum;
  const int _buff_max = 10;
  int i;
  
  if(flag_first == 0)
  {
    flag_first = 1;
    for(i = 0, sum = 0; i < _buff_max; i++)
    {
      _buff[i] = m;
      sum += _buff[i];
    }
    return m;
  }
  else
  {
    sum -= _buff[0];
    for(i = 0; i < (_buff_max - 1); i++)
    {
      _buff[i] = _buff[i + 1];
    }
    _buff[9] = m;
    sum += _buff[9];
    
    i = sum / 10.0;
    return i;
  }
}

#include <Servo.h>

Servo servo;  // create servo object to control a servo
int mypollu;

void setup(void)
{
  pinMode(iled, OUTPUT);
  digitalWrite(iled, LOW);     //iled default closed

  servo.attach(9);  // attaches the servo on pin 9 to the servo objectÆ°
  servo.write(0);   // rotate slowly servo to 0 degrees immediately
  //int mypollu = 1;
  
  Serial.begin(9600);                                         //send and receive at 9600 baud
  Serial.print("*********************************** WaveShare ***********************************\n");
}

void loop(void)
{
  
  if (mypollu < 1) {
    for (int pos = 0; pos <= 180; pos += 1) {  // rotate slowly from 0 degrees to 180 degrees, one by one degree
    // in steps of 1 degree
    servo.write(pos);  // control servo to go to position in variable 'pos'
    delay(10);         // waits 10ms for the servo to reach the position
  }

  for (int pos = 180; pos >= 0; pos -= 1) {  // rotate from 180 degrees to 0 degrees, one by one degree
    servo.write(pos);                        // control servo to go to position in variable 'pos'
    delay(10);                               // waits 10ms for the servo to reach the position
  }
   }
  /*
  get adcvalue
  */
  digitalWrite(iled, HIGH);
  delayMicroseconds(280);
  adcvalue = analogRead(vout);
  digitalWrite(iled, LOW);
  
  adcvalue = Filter(adcvalue);

  /*
  covert voltage (mv)
  */
  voltage = (SYS_VOLTAGE / 1024.0) * adcvalue * 11;

  /*
  voltage to density
  */
  if(voltage >= NO_DUST_VOLTAGE)
  {
    voltage -= NO_DUST_VOLTAGE;
    
    density = voltage * COV_RATIO;
  }
  else
    density = 0;
    
  /*
  display the result
  */
  Serial.print("The current dust concentration is: ");
  Serial.print(density);
  Serial.print(" ug/m3\n");

  if (density < 100 ){mypollu = 0;}
  
  delay(1000);
}

D letsbreathtogether/sensor.ino => letsbreathtogether/sensor.ino +0 -63
@@ 1,63 0,0 @@
/*

 Interfacing Sharp Optical Dust Sensor GP2Y1014AU0F with Arduino

*/

int measurePin = 0; //Connect dust sensor to Arduino A0 pin

//#define ledPower = 7;   //Connect 3 led driver pins of dust sensor to Arduino D2

int samplingTime = 280; // time required to sample signal coming out   of the sensor

int deltaTime = 40; // 

int sleepTime = 9680;

float voMeasured = 0;

float calcVoltage = 0;

float dustDensity = 0;

void setup(){

  Serial.begin(9600);

//  pinMode(ledPower,OUTPUT);

}

void loop(){

//  digitalWrite(ledPower,LOW); // power on the LED

  delayMicroseconds(samplingTime);

  voMeasured = analogRead(measurePin); // read the dust value

  delayMicroseconds(deltaTime);

//  digitalWrite(ledPower,HIGH); // turn the LED off

  delayMicroseconds(sleepTime);

  // 0 - 5V mapped to 0 - 1023 integer values

  // recover voltage

  calcVoltage = voMeasured * (5.0 / 1024.0);

  // linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/

  // Chris Nafis (c) 2012

  dustDensity = 170 * calcVoltage - 0.1;

  Serial.println(dustDensity); // unit: ug/m3

  delay(1000);

}