Created
December 26, 2015 16:31
-
-
Save gnawux/ddf252aa5808036c43ce to your computer and use it in GitHub Desktop.
Arduino Program to collect the PM 2.5 data through UART and display it on a 1602 LCD wit I2C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <LCD.h> | |
#include <LiquidCrystal_I2C.h> // F Malpartida's NewLiquidCrystal library | |
#define I2C_ADDR 0x27 // Define I2C Address for controller | |
#define BACKLIGHT_PIN 3 | |
#define En_pin 2 | |
#define Rw_pin 1 | |
#define Rs_pin 0 | |
#define D4_pin 4 | |
#define D5_pin 5 | |
#define D6_pin 6 | |
#define D7_pin 7 | |
#define LED_OFF 0 | |
#define LED_ON 1 | |
const int SetPin = 2; | |
char frame[32]; | |
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin,BACKLIGHT_PIN,POSITIVE); | |
char banner1[17]; | |
char banner2[17]; | |
void banner_init() { | |
for(int i = 0; i<16;i++) { | |
banner1[i] = ' '; | |
banner2[i] = ' '; | |
} | |
banner1[16] = '\0'; | |
banner2[16] = '\0'; | |
} | |
void banner_display() { | |
lcd.setCursor(0,0); | |
lcd.print(banner1); | |
lcd.setCursor(0,1); | |
lcd.print(banner2); | |
} | |
void debug(const char* str) { | |
lcd.setCursor(0,0); | |
lcd.print(str); | |
} | |
void initPanTower() { | |
debug("begin"); | |
while (1) { | |
if (!Serial.available()) { | |
delay(100); | |
debug("wating serial"); | |
continue; | |
} | |
debug("got serial"); | |
int len = 0; | |
int nr = Serial.readBytes(frame, 32); | |
while (len + nr < 32) { | |
len = len + nr; | |
nr = Serial.readBytes(frame + len, 32 - len); | |
} | |
debug("got frame"); | |
for (int diff = 0; diff < 32 ; diff++ ) { | |
if (frame[diff] == 'B' && frame[(diff+1)%32] == 'M') { | |
nr = len = 0; | |
while (len + nr < diff) { | |
len = len +nr; | |
nr = Serial.readBytes(frame + len, diff - len); | |
} | |
digitalWrite(LED_BUILTIN, HIGH); | |
return; | |
} | |
} | |
} | |
} | |
void setup() | |
{ | |
//Initialize LCD | |
lcd.begin (16,2); // initialize the lcd | |
lcd.backlight(); | |
banner_init(); | |
//Enable Monitor Module | |
pinMode(SetPin, OUTPUT); | |
digitalWrite(SetPin, HIGH); | |
//shut the led | |
pinMode(LED_BUILTIN, OUTPUT); | |
digitalWrite(LED_BUILTIN, LOW); | |
//Start Serial Port | |
Serial.begin(9600); | |
initPanTower(); | |
} | |
void loop() | |
{ | |
if (Serial.available()) { | |
int len = 0; | |
int nr = Serial.readBytes(frame, 32); | |
while (len + nr < 32) { | |
len = len + nr; | |
nr = Serial.readBytes(frame + len, 32 - len); | |
} | |
int d1 = ((int)frame[6] << 8) + frame[7]; | |
int d2 = ((int)frame[12] << 8) + frame[13]; | |
snprintf(banner1, 16, "std: %d ug/m3 ", d1); | |
snprintf(banner2, 16, "atm: %d ug/m3 ", d2); | |
banner_display(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment