/* ホコリセンサー
* 2014/05/08 (C) H.Ishikawa
*/
#include <wiringPi.h> //(1)
#include <time.h>
#include <stdio.h>
#define GPIO17 17 //1micro small_particle //(2)
#define GPIO22 22 //2.5micro large_particle //(2)
int main (void)
{
time_t timer;
struct tm *date;
char date_time[256];
FILE *fp;
char *fname = "dustsensor/dustsensor.txt";
if (wiringPiSetupGpio() == -1) return 1 ;//BCM GPIO pin numbers //(3)
pinMode(GPIO17, INPUT) ; //(4)
pinMode(GPIO22, INPUT) ; //(4)
long count_small_particle,count_large_particle,total;
int small_particle,large_particle;
float lowpulse_small_particle = 0.0;
float lowpulse_large_particle = 0.0;
timer = time(NULL);
strftime(date_time, 255, "%Y/%m/%d %H:%M:%S", localtime(&timer));
printf("measurement start %s\n",date_time);
while (1){//(5)
count_small_particle = 0;
count_large_particle = 0;
total = 0;
fp = fopen( fname, "a" ); //追記形
while (total < 331000) { //(8)
total = total + 1;
small_particle = digitalRead (GPIO17); //(6)
if (small_particle == 0){ //(7)
count_small_particle = count_small_particle + 1;
}
large_particle = digitalRead (GPIO22); //(6)
if (large_particle == 0){ //(7)
count_large_particle = count_large_particle + 1;
}
delayMicroseconds(100);//(8) 0.1ms
}
timer = time(NULL);
strftime(date_time, 255, "%Y/%m/%d %H:%M:%S", localtime(&timer));
lowpulse_small_particle = (float)count_small_particle / (float)total;
lowpulse_large_particle = (float)count_large_particle / (float)total;
fprintf( fp, "%s,%f,%f \n", date_time,lowpulse_small_particle,lowpulse_large_particle); //(9)
printf("%s,%f,%f \n",date_time,lowpulse_small_particle,lowpulse_large_particle);
fclose( fp );
}
return 0;
}
|