#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1331.h>
//Font
#include "/Users/shigu/Library/CloudStorage/Dropbox/Arduino_inDropbox/libraries/Adafruit_GFX_Library/Fonts/FreeSans9pt7b.h"
//TextColorããµã³ãã« ï¼16-bitã§æå® 5-6-5 Colorï¼
#define SSD1331_BLACK 0x0000
// #define SSD1331_BLUE 0xF800
// #define SSD1331_YELLOW 0x07FF
#define SSD1331_WHITE 0xFFFF
// Declaration for SSD1331 display connected using software SPI (default case):
#define cs D7 //Connect SSD1315 CS ChipSelect SlaveSelect
#define dc D3 //Connect SSD1315 D/C Data/Command control
#define rst D2 //Connect SSD1331 RES Reset
#define mosi D10 //Connect SSD1315 D1 MasterOut SlaveIn
#define sclk D8 //Connect SSD1315 D0 Serial Clock
Adafruit_SSD1331 disply( cs, dc, mosi, sclk, rst);
void setup() {
// put your setup code here, to run once:
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(115200);
disply.begin( );
disply.fillScreen( SSD1331_BLACK );
disply.setFont( &FreeSans9pt7b );
}
int reading = 2222, co2Int = 0;
float temp = 2623, tempInt = 0;
void loop() {
// step 1: wake sensor
Wire.beginTransmission(byte(0x68)); // transmΩit to device 0x68=104
Wire.endTransmission(); // stop transmitting
// put your main code here, to run repeatedly:
Serial.println("");
// step 3: instruct sensor to return a particular echo reading
Wire.beginTransmission(byte(0x68)); // transmit to device #104
Wire.write(byte(0x06)); // sets register pointer to echo #1 register (0x06)
Wire.endTransmission(); // stop transmitting
// step 4: request reading from sensor
Wire.requestFrom(byte(0x68), 2); // request 2 bytes from slave device #104
// step 5: receive reading CO2 from sensor
if (2 <= Wire.available()) { // if two bytes were received
reading = Wire.read(); // receive high byte (overwrites previous reading)
reading = reading << 8; // shift high byte to be high 8 bits
reading |= Wire.read(); // receive low byte as lower 8 bits
if (co2Int != reading) {
co2Int = reading;
disply.fillRoundRect(0, 4, 94, 19, 1, disply.color565(20, 20, 20) );
disply.setTextSize(1); // Normal 1:1 pixel scale
disply.setTextColor(disply.color565(200, 200, 120));
disply.setCursor(0,20); // Start at top-left corner
disply.print(F(" CO2:") );
disply.setCursor(50, 20); // print OLED
disply.print(reading);
}
//for debug
Serial.print(reading); // print Serial
Serial.println(" CO2");
}
// step 6: read temperature
Wire.beginTransmission(byte(0x68)); // transmit to device
Wire.write(byte(0x08)); // sets register pointer to echo #1 register (0x08)
Wire.endTransmission(); // stop transmitting
Wire.requestFrom(byte(0x68), 2); // request 2 bytes from slave device
if (2 <= Wire.available()) { // if two bytes were received
reading = Wire.read(); // receive high byte (overwrites previous reading)
reading = reading << 8; // shift high byte to be high 8 bits
reading |= Wire.read(); // receive low byte as lower 8 bits
temp = reading;
if (temp != tempInt) {
tempInt = temp;
disply.fillRoundRect(0, 33, 94, 19, 1, disply.color565(20, 20, 20) );
disply.setCursor(3,48); // Start at top-left corner
disply.print(F("temp:") );
disply.setCursor(53, 48); // print OLED
disply.print((temp / 100), 1);
}
//for debug
Serial.print((temp / 100), 1); // print Serial
Serial.println(" C");
}
delay(5000);
}