Hello,
I am working in a lab and have "inherited" the code for two air sensors from a past student. We are working with two sensors, the Shinyei and Sharp. We have the code working that allows us to record data using the Sharp, but we are unable to make it work for the Shinyei.
This is the code for the Shinyei that isn't working
We added the section right where I put all of the asteriks
```
*/ Grove - Dust Sensor Demo v1.0
Interface to Shinyei Model PPD42NS Particle Sensor
Program by Christopher Nafis
Written April 2012
http://www.seeedstudio.com/depot/grove-dust-sensor-p-1050.html
http://www.sca-shinyei.com/pdf/PPD42NS.pdf
JST Pin 1 (Black Wire) => Arduino GND
JST Pin 3 (Red wire) => Arduino 5VDC
JST Pin 4 (Yellow wire) => Arduino Digital Pin 8
*/
#include <SPI.h>
#include <SD.h>
const int cs=7; //chip select
int pin = 8;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 15000;//sampe 30s ;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;
void setup() {
Serial.begin(9600);
pinMode(8,INPUT);
starttime = millis();//get the current time;
RTC_init();
//month(1-12), day(1-31), year(0-99), hour(0-23), minute(0-59), second(0-59)
SetTimeDate(7,2,15,15,00,55);
}
void loop() {
duration = pulseIn(pin, LOW);
lowpulseoccupancy = lowpulseoccupancy+duration;
if ((millis()-starttime) > sampletime_ms)//if the sampel time == 30s
{
ratio = lowpulseoccupancy/(sampletime_ms10.0); // Integer percentage 0=>100
concentration = 1.1pow(ratio,3)-3.8pow(ratio,2)+520ratio+0.62; // using spec sheet curve
// Serial.print("concentration = ");
Serial.print(ReadTimeDate());
Serial.print(",");
Serial.println(concentration);
delay(1000);
// Serial.println(" pcs/0.01cf");
// Serial.println("\n");
lowpulseoccupancy = 0;
starttime = millis();
}
}
//=====================================
int RTC_init(){
pinMode(cs,OUTPUT); // chip select
// start the SPI library:
SPI.begin(); SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE1); // both mode 1 & 3 should work
//set control register
digitalWrite(cs, LOW);
SPI.transfer(0x8E);
SPI.transfer(0x60); //60= disable Osciallator and Battery SQ wave @1hz, temp compensation, Alarms disabled
digitalWrite(cs, HIGH);
delay(10);
}
//=====================================
int SetTimeDate(int d, int mo, int y, int h, int mi, int s){
int TimeDate [7]={s,mi,h,0,d,mo,y};
for(int i=0; i<=6;i++){
if(i==3)
i++;
int b= TimeDate[i]/10;
int a= TimeDate[i]-b10;
if(i==2){
if (b==2)
b=B00000010;
else if (b==1)
b=B00000001;
}
TimeDate[i]= a+(b<<4);
digitalWrite(cs, LOW);
SPI.transfer(i+0x80);
SPI.transfer(TimeDate[i]);
digitalWrite(cs, HIGH);
}
}
//=====================================
String ReadTimeDate(){
String temp;
int TimeDate [7]; //second,minute,hour,null,day,month,year
for(int i=0; i<=6;i++){
if(i==3)
i++;
digitalWrite(cs, LOW);
SPI.transfer(i+0x00);
unsigned int n = SPI.transfer(0x00);
digitalWrite(cs, HIGH);
int a=n & B00001111;
if(i==2){
int b=(n & B00110000)>>4; //24 hour mode
if(b==B00000010)
b=20;
else if(b==B00000001)
b=10;
TimeDate[i]=a+b;
}
else if(i==4){
int b=(n & B00110000)>>4;
TimeDate[i]=a+b10;
}
else if(i==5){
int b=(n & B00010000)>>4;
TimeDate[i]=a+b10;
}
else if(i==6){
int b=(n & B11110000)>>4;
TimeDate[i]=a+b10;
}
else{
int b=(n & B01110000)>>4;
TimeDate[i]=a+b*10;
}
}
temp.concat(TimeDate[4]);
temp.concat("/") ;
temp.concat(TimeDate[5]);
temp.concat("/") ;
temp.concat(TimeDate[6]);
temp.concat(" ") ;
temp.concat(TimeDate[2]);
temp.concat(":") ;
temp.concat(TimeDate[1]);
temp.concat(":") ;
temp.concat(TimeDate[0]);
return(temp);
}
//============================================================
String dataString = "";
for (int analogPin = 0; analogPin < 1; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
/* if (analogPin < 0)
dataString += ",";
*/
}
//******************************
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if (dataFile)
dataFile.println(dataString);
dataFile.close();
//Serial.println(dataString);
// else
// Serial.println("error opening datalog.txt");
}**
```
It then gives the following errors:
Arduino: 1.8.5 (Windows 10), Board: "Arduino/Genuino Uno"
Shinyei_DustDensity_RTC_WORKING_CODE_2_July_2015:145: error: expected unqualified-id before 'for'
for (int analogPin = 0; analogPin < 1; analogPin++) {
^
Shinyei_DustDensity_RTC_WORKING_CODE_2_July_2015:145: error: 'analogPin' does not name a type
for (int analogPin = 0; analogPin < 1; analogPin++) {
** ^**
Shinyei_DustDensity_RTC_WORKING_CODE_2_July_2015:145: error: 'analogPin' does not name a type
for (int analogPin = 0; analogPin < 1; analogPin++) {
** ^**
Shinyei_DustDensity_RTC_WORKING_CODE_2_July_2015:156: error: 'dataFile' does not name a type
dataFile.println(dataString);
^
Shinyei_DustDensity_RTC_WORKING_CODE_2_July_2015:157: error: 'dataFile' does not name a type
dataFile.close();
^
Shinyei_DustDensity_RTC_WORKING_CODE_2_July_2015:163: error: expected declaration before '}' token
}
^
exit status 1
expected unqualified-id before 'for'
Any help would be appreciated. Thank you!