#include // for I2C #include // for SSD1306 driver OLED display #include // for timer #include // for watchdog #define DS1307_ADDRESS 0x68 // DS1307 RTC device address #define eeprom24c16 0x50 // EEPROM device address B01010000(80) #define SSD1306_ADDRESS 0x3C // SSD1306 OLED display device address U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0); // I2C / TWI const int measint = 2; // default measurement interval in seconds const int logint = 2; // default logging interval to EEPROM in seconds const int DS_OUT = A0, BAT_SENS = A1, SW1 = A2, SW2 = A3; const byte SW3 = 2, BATS_EN = 3, DISP_EN = 4, BT_EN = 5, DS_EN = 6, DS_LEDEN = 7, LED = 8, CHRG_STS = 9, intled = 13; // Arduino board LED (shared with SPI) unsigned long seccount = 1; // counts intervals in seconds unsigned long prevMillis = 0; // will store last measurement time int timeRTC[7]; // RTC time (second, minute, hour, weekDay, monthDay, month, year) int dustval = 0; boolean intTimer = LOW, SW1st = LOW, SW2st = LOW, SW3st = LOW; // Timer operation routine void count() { if (seccount > 0) // decrements to 0 { seccount--; } else { seccount = measint; intTimer = HIGH; } } // Battery voltage measurement int batvoltage() { int bv; digitalWrite(BATS_EN,HIGH); delay(10); bv = (analogRead(BAT_SENS)); // reads battery voltage digitalWrite(BATS_EN,LOW); return(bv); } // Dust sensor measurement void dustsense() { int dustTmpVal[10]; // 10times measurement repetition byte i; for (i = 0; i < 10; i++) { digitalWrite(DS_EN,LOW); // dust sensor supply ON digitalWrite(LED,LOW); delay(100); digitalWrite(DS_LEDEN,LOW); // dust sensor TX LED ON delayMicroseconds(280); dustTmpVal[i] = analogRead(DS_OUT); // reads dust sensor analog output delayMicroseconds(40); digitalWrite(DS_LEDEN,HIGH); digitalWrite(DS_EN,HIGH); digitalWrite(LED,HIGH); delay(10); dustval = dustval + dustTmpVal[i]; } dustval = dustval/10; // average value from 10 measurements readRTC(); Serial.print(timeRTC[6]); Serial.print("/"); Serial.print(timeRTC[5]); Serial.print("/"); Serial.print(timeRTC[4]); Serial.print(" "); Serial.print(timeRTC[2]); Serial.print(":"); Serial.print(timeRTC[1]); Serial.print(":"); Serial.print(timeRTC[0]); Serial.print(" Dust value: "); Serial.print(dustval); // vypis zmerene hodnoty Serial.print(" Bat value: "); Serial.println(batvoltage()); // vypis zmerene hodnoty } // Convert binary coded decimal to normal decimal numbers byte bcdToDec(byte val) { return ( (val/16*10) + (val%16) ); } // Convert normal decimal numbers to binary coded decimal byte decToBcd(byte val){ return ( (val/10*16) + (val%10) ); } // Read RTC time/date from DS1307 void readRTC() { byte i; byte zero = 0x00; // Reset the register pointer Wire.beginTransmission(DS1307_ADDRESS); Wire.write(zero); Wire.endTransmission(); Wire.requestFrom(DS1307_ADDRESS, 7); for (i = 0; i < 7; i++){ timeRTC[i] = bcdToDec(Wire.read()); } } void draw(void) { // graphic commands to redraw the complete screen should be placed here u8g.setFont(u8g_font_unifont); //u8g.setFont(u8g_font_osb21); u8g.setPrintPos(0, 10); u8g.print("Dust Measurement"); u8g.setPrintPos(0, 21); u8g.print(timeRTC[5]); u8g.print("/"); u8g.print(timeRTC[4]); u8g.print(" "); u8g.print(timeRTC[2]); u8g.print(":"); u8g.print(timeRTC[1]); u8g.print(":"); u8g.print(timeRTC[0]); u8g.setPrintPos(0, 38); u8g.print("Dust value: "); u8g.setFont(u8g_font_fur20); u8g.setPrintPos(0, 62); u8g.print(dustval); // vypis zmerene hodnoty } void setDateTime(){ byte second = 0; //0-59 byte minute = 0; //0-59 byte hour = 0; //0-23 byte weekDay = 1; //1-7 byte monthDay = 1; //1-31 byte month = 1; //1-12 byte year = 15; //0-99 Wire.beginTransmission(DS1307_ADDRESS); Wire.write(0x00); //stop Oscillator Wire.write(decToBcd(second)); Wire.write(decToBcd(minute)); Wire.write(decToBcd(hour)); Wire.write(decToBcd(weekDay)); Wire.write(decToBcd(monthDay)); Wire.write(decToBcd(month)); Wire.write(decToBcd(year)); Wire.write(0x00); //start Wire.endTransmission(); } // the setup routine runs once when you press reset: void setup() { delay(1000); // initialize the digital pin as an output. pinMode(2, INPUT); // SW3 input pinMode(3, OUTPUT); // BATS_EN battery voltage measurement enable pinMode(4, OUTPUT); // DISP_EN display supply enable pinMode(5, OUTPUT); // BT_EN bluetooth supply enable pinMode(6, OUTPUT); // DS_EN dust sensor supply enable pinMode(7, OUTPUT); // DS_LEDEN dust sensor LED enable pinMode(8, OUTPUT); // LED status output pinMode(9, INPUT); // CHRG_STS charge status input // pinMode(10, OUTPUT); // SPI // pinMode(11, OUTPUT); // pinMode(12, OUTPUT); // pinMode(13, INPUT); pinMode(A0, INPUT); // DS_OUT output from dust sensor pinMode(A1, INPUT); // BAT_SENS battery voltage measurement input pinMode(A2, INPUT); // SW1 input pinMode(A3, INPUT); // SW2 input // pinMode(A4, OUTPUT); // I2C // pinMode(A5, OUTPUT); analogReference(INTERNAL); // 1V1 reference // default output settings digitalWrite(BATS_EN, LOW); digitalWrite(DISP_EN, LOW); digitalWrite(BT_EN, HIGH); digitalWrite(DS_EN, HIGH); digitalWrite(DS_LEDEN, HIGH); digitalWrite(LED, LOW); Wire.begin(); Serial.begin(9600); // assign default color value if ( u8g.getMode() == U8G_MODE_R3G3B2 ) { u8g.setColorIndex(255); // white } else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) { u8g.setColorIndex(3); // max intensity } else if ( u8g.getMode() == U8G_MODE_BW ) { u8g.setColorIndex(1); // pixel on } else if ( u8g.getMode() == U8G_MODE_HICOLOR ) { u8g.setHiColorByRGB(255,255,255); } //MsTimer2::set(1000, count); // 1s timer set //MsTimer2::start(); } // the loop routine runs over and over again forever: void loop() { // measurement interval control unsigned long currMillis = millis(); if (currMillis < prevMillis) // reset when millis counter overflow prevMillis = currMillis; if (currMillis - prevMillis > measint * 1000) { prevMillis = currMillis; dustsense(); } // picture loop u8g.firstPage(); do { draw(); } while( u8g.nextPage() ); delay(100); if (digitalRead(SW1) == LOW) { SW1st = !SW1st; digitalWrite(BT_EN, SW1st); delay(500); } if (digitalRead(SW2) == LOW) { SW2st = !SW2st; digitalWrite(DISP_EN, SW2st); delay(500); } if (digitalRead(SW3) == LOW) { SW3st = !SW3st; setDateTime(); delay(500); } }