Skip to content

Commit

Permalink
add pm2.5sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
Bin_Go committed Dec 21, 2016
1 parent 59cb1d3 commit 21f7502
Show file tree
Hide file tree
Showing 5 changed files with 1,534 additions and 17 deletions.
133 changes: 125 additions & 8 deletions makeblock/examples/Firmware_For_mBlock/mbot_firmware/mbot_firmware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
* File Name : mbot_firmware.ino
* Author : Ander, Mark Yan
* Updated : Ander, Mark Yan
* Version : V06.01.106
* Date : 07/06/2016
* Version : V06.01.108
* Date : 21/12/2016
* Description : Firmware for Makeblock Electronic modules with Scratch.
* License : CC-BY-SA 3.0
* Copyright (C) 2013 - 2016 Maker Works Technology Co., Ltd. All right reserved.
* http://www.makeblock.cc/
**************************************************************************/
#include <Wire.h>
#include <MeMCore.h>
#include <SoftwareSerial.h>

Servo servos[8];
MeDCMotor dc;
MeTemperature ts;
//MeTemperature ts;
MeRGBLed led(0,30);
MeUltrasonicSensor us;
Me7SegmentDisplay seg;
Expand All @@ -30,7 +31,7 @@ MeFlameSensor FlameSensor;
MeGasSensor GasSensor;
MeTouchSensor touchSensor;
Me4Button buttonSensor;

MePm25Sensor *pm25sensor = NULL;
typedef struct MeModule
{
int device;
Expand Down Expand Up @@ -62,7 +63,7 @@ const int analogs[12] PROGMEM = {A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11};
#else
const int analogs[8] PROGMEM = {A0,A1,A2,A3,A4,A5,A6,A7};
#endif
String mVersion = "06.01.106";
String mVersion = "06.01.107";
boolean isAvailable = false;

int len = 52;
Expand All @@ -73,6 +74,7 @@ byte modulesLen=0;
boolean isStart = false;
char serialRead;
uint8_t command_index = 0;
long rxruntime = 0;
#define VERSION 0
#define ULTRASONIC_SENSOR 1
#define TEMPERATURE_SENSOR 2
Expand All @@ -99,15 +101,44 @@ uint8_t command_index = 0;
#define FLAMESENSOR 24
#define GASSENSOR 25
#define COMPASS 26
#define TEMPERATURE_SENSOR_1 27
#define DIGITAL 30
#define ANALOG 31
#define PWM 32
#define SERVO_PIN 33
#define TONE 34
#define BUTTON_INNER 35
#define ULTRASONIC_ARDUINO 36
#define PULSEIN 37
#define STEPPER 40
#define LEDMATRIX 41
#define TIMER 50
#define TOUCH_SENSOR 51
#define JOYSTICK_MOVE 52
#define COMMON_COMMONCMD 60
//Secondary command
#define SET_STARTER_MODE 0x10
#define SET_AURIGA_MODE 0x11
#define SET_MEGAPI_MODE 0x12
#define GET_BATTERY_POWER 0x70
#define GET_AURIGA_MODE 0x71
#define GET_MEGAPI_MODE 0x72
#define ENCODER_BOARD 61
//Read type
#define ENCODER_BOARD_POS 0x01
#define ENCODER_BOARD_SPEED 0x02
#define ENCODER_PID_MOTION 62
//Secondary command
#define ENCODER_BOARD_POS_MOTION 0x01
#define ENCODER_BOARD_SPEED_MOTION 0x02
#define ENCODER_BOARD_PWM_MOTION 0x03
#define ENCODER_BOARD_SET_CUR_POS_ZERO 0x04
#define ENCODER_BOARD_CAR_POS_MOTION 0x05
#define PM25SENSOR 63
//Secondary command
#define GET_PM1_0 0x01
#define GET_PM2_5 0x02
#define GET_PM10 0x03

#define GET 1
#define RUN 2
Expand All @@ -118,7 +149,32 @@ int servo_pins[8]={0,0,0,0,0,0,0,0};
unsigned char prevc=0;
boolean buttonPressed = false;
uint8_t keyPressed = KEY_NULL;

/*
* function list
*/
void readButtonInner(uint8_t pin, int8_t s);
void buzzerOn();
void buzzerOff();
unsigned char readBuffer(int index);
void writeBuffer(int index,unsigned char c);
void writeHead();
void writeEnd();
void writeSerial(unsigned char c);
void readSerial();
void parseData();
void callOK();
void sendByte(char c);
void sendString(String s);
void sendFloat(float value);
void sendShort(double value);
void sendDouble(double value);
short readShort(int idx);
float readFloat(int idx);
char* readString(int idx,int len);
uint8_t* readUint8(int idx,int len);
void runModule(int device);
int searchServoPin(int pin);
void readSensor(int device);
void readButtonInner(uint8_t pin, int8_t s)
{
pin = pgm_read_byte(&analogs[pin]);
Expand Down Expand Up @@ -154,6 +210,7 @@ void setup(){
Serial.println(mVersion);
ledMx.setBrightness(6);
ledMx.setColorIndex(1);
rxruntime = millis();
}
int irDelay = 0;
int irIndex = 0;
Expand Down Expand Up @@ -193,6 +250,14 @@ void loop(){
}
}
}
if(millis() - rxruntime>500)
{
rxruntime = millis();
if(pm25sensor != NULL)
{
pm25sensor->rxloop();
}
}
readSerial();
if(isAvailable){
unsigned char c = serialRead&0xff;
Expand Down Expand Up @@ -565,15 +630,15 @@ void readSensor(int device){
sendFloat(value);
}
break;
case TEMPERATURE_SENSOR:{
/* case TEMPERATURE_SENSOR:{
slot = readBuffer(7);
if(ts.getPort()!=port||ts.getSlot()!=slot){
ts.reset(port,slot);
}
value = ts.temperature();
sendFloat(value);
}
break;
break;*/
case LIGHT_SENSOR:
case SOUND_SENSOR:
case POTENTIONMETER:{
Expand Down Expand Up @@ -747,5 +812,57 @@ void readSensor(int device){
sendByte(keyPressed == readBuffer(7));
}
break;
case PM25SENSOR:
{
uint8_t secondorder = readBuffer(7);
uint16_t temp = 0;
static uint16_t tempreserve = 0;
static uint8_t dataflag = 1;
if(pm25sensor == NULL)
{
pm25sensor = new MePm25Sensor(port);
pm25sensor->begin(9600);
pm25sensor->setOutputIntimePeriod(3);//3S
delay(10);
pm25sensor->setOutputIntimePeriod(3);//3S
dataflag = 1;
}
else if(pm25sensor->getPort() != port)//initial
{
delete pm25sensor;
pm25sensor = new MePm25Sensor(port);
pm25sensor->begin(9600);
pm25sensor->setOutputIntimePeriod(3);//3S
delay(10);
pm25sensor->setOutputIntimePeriod(3);//3S
dataflag = 0;
}
else
{
dataflag = 1;
}
if(dataflag)
{
if(secondorder==GET_PM1_0)
{
temp = pm25sensor->readPm1_0Concentration();
}
else if(secondorder==GET_PM2_5)
{
temp = pm25sensor->readPm2_5Concentration();
}
else if(secondorder==GET_PM10)
{
temp = pm25sensor->readPm10Concentration();
}
tempreserve = temp;
sendShort(temp);
}
else
{
sendShort(tempreserve);
}
}
break;
}
}
Loading

0 comments on commit 21f7502

Please sign in to comment.