i2c-slave-mq3_c2h5oh_sensor_ex1
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
[[FrontPage]]
#code(C){{
/**********
MQ-3 とarduinoの接続
https://bildr.org/2013/10/mq3-arduino/
Arduino Nano MQ-3
GND---+---GND
|
10K ohm
|
A0----+---B1
5V----+---H1
|
+---A1
Arduino Nano I2C
SDA -> A4
SCL -> A5
**********/
#include <Wire.h>
//int pirPin = A0;
//int pirPin = 7;
int mq3_analogPin = 0;
int mq3_value=0;
unsigned long duration;
unsigned long starttime;
unsigned long sampleRate_ms = 500;
int freq;
void setup() {
Serial.begin(9600);
Wire.begin(0x14);// Slave ID #14
Wire.onRequest(requestEvent);
Wire.onReceive(receiveEvent);
// pinMode(pirPin, INPUT);
starttime = millis();
}
void loop() {
mq3_value = analogRead(mq3_analogPin);
Serial.print("mq3=");
Serial.print(mq3_value);
Serial.println("");
delay(sampleRate_ms );
}
byte cmd[5];
int writeFlag;
byte wx[5];
void receiveEvent(int n) {
// Serial.print("receiveEvent, n=");
// Serial.print(n);
// Serial.print(",cmd=");
if(n>1) writeFlag=1; else writeFlag=0;
for(int i=0; i<n; i++){
if(Wire.available()){
cmd[i]=Wire.read();
// Serial.print(cmd[i],HEX);
}
}
// Serial.println();
if(!writeFlag) return;
if(cmd[0]==0x03){ // if read, read MSB of the sample rate,
// if write, set MSB of the sample rate.
unsigned int wi=sampleRate_ms;
wx[3]=wi & 0xff; wi=wi>>8;
wx[2]=wi & 0xff; wi=wi>>8;
wx[1]=wi & 0xff; wi=wi>>8;
wx[0]=wi & 0xff;
wx[2]=cmd[1];
sampleRate_ms=(((((wx[0]<<8)|wx[1])<<8)|wx[2])<<8)<<wx[3];
// Serial.print("sampleRate_ms=");
// Serial.println(sampleRate_ms);
} else if(cmd[0]==0x04) { // if read, read LSB of the sample rate,
// if write, set LSB of the sample rate.
unsigned int wi=sampleRate_ms;
wx[3]=wi & 0xff; wi=wi>>8;
wx[2]=wi & 0xff; wi=wi>>8;
wx[1]=wi & 0xff; wi=wi>>8;
wx[0]=wi & 0xff;
wx[0]=cmd[1];
sampleRate_ms=(((((wx[0]<<8)|wx[1])<<8)|wx[2])<<8)<<wx[3];
// Serial.print("sampleRate_ms=");
// Serial.println(sampleRate_ms);
}
}
void requestEvent(){
byte w;
// Serial.print("requestEvent, cmd=");
// Serial.println(cmd[0],HEX);
if(writeFlag) {
// Serial.println("w, return.");
return;
}
else{
// Serial.println("r, ...");
}
if(cmd[0]==0x00) { // if read, read currentActivity
w= (mq3_value >>8) & 0xff;
Wire.write(w);
// Serial.print("current=");
// Serial.println(w);
} else if(cmd[0]==0x01){ // if read, read MSB of the accumulated activity;
unsigned int wi=mq3_value;
wx[1]=wi & 0xff; wi=wi>>8;
wx[0]=wi & 0xff;
w=wx[0];
Wire.write(w);
// Serial.print("activity=");
// Serial.print(activity);
// Serial.print(", activity[0]=");
// Serial.println(wx[0]);
} else if(cmd[0]==0x02) { // if read, read LSB of the accumulated activity;
unsigned int wi=mq3_value;
wx[1]=wi & 0xff; wi=wi>>8;
wx[0]=wi & 0xff;
w=wx[1];
Wire.write(w);
// Serial.print("activity=");
// Serial.print(activity);
// Serial.print(", activity[1]=");
// Serial.println(wx[1]);
} else if(cmd[0]==0x03){ // if read, read MSB of the sample rate,
unsigned int wi=sampleRate_ms;
wx[3]=wi & 0xff; wi=wi>>8;
wx[2]=wi & 0xff; wi=wi>>8;
wx[1]=wi & 0xff; wi=wi>>8;
wx[0]=wi & 0xff;
w=wx[2];
Wire.write(w);
// Serial.print("sampleRate_ms=");
// Serial.print(sampleRate_ms);
// Serial.print(", sampleRate_ms[2]=");
// Serial.println(wx[2]);
} else if(cmd[0]==0x04) { // if read, read LSB of the sample rate,
unsigned int wi=sampleRate_ms;
wx[3]=wi & 0xff; wi=wi>>8;
wx[2]=wi & 0xff; wi=wi>>8;
wx[1]=wi & 0xff; wi=wi>>8;
wx[0]=wi & 0xff;
w=wx[3];
Wire.write(w);
// Serial.print("sampleRate_ms=");
// Serial.print(sampleRate_ms);
// Serial.print(", sampleRate_ms[3]=");
// Serial.println(wx[3]);
}
}
}}
----
#counter
終了行:
[[FrontPage]]
#code(C){{
/**********
MQ-3 とarduinoの接続
https://bildr.org/2013/10/mq3-arduino/
Arduino Nano MQ-3
GND---+---GND
|
10K ohm
|
A0----+---B1
5V----+---H1
|
+---A1
Arduino Nano I2C
SDA -> A4
SCL -> A5
**********/
#include <Wire.h>
//int pirPin = A0;
//int pirPin = 7;
int mq3_analogPin = 0;
int mq3_value=0;
unsigned long duration;
unsigned long starttime;
unsigned long sampleRate_ms = 500;
int freq;
void setup() {
Serial.begin(9600);
Wire.begin(0x14);// Slave ID #14
Wire.onRequest(requestEvent);
Wire.onReceive(receiveEvent);
// pinMode(pirPin, INPUT);
starttime = millis();
}
void loop() {
mq3_value = analogRead(mq3_analogPin);
Serial.print("mq3=");
Serial.print(mq3_value);
Serial.println("");
delay(sampleRate_ms );
}
byte cmd[5];
int writeFlag;
byte wx[5];
void receiveEvent(int n) {
// Serial.print("receiveEvent, n=");
// Serial.print(n);
// Serial.print(",cmd=");
if(n>1) writeFlag=1; else writeFlag=0;
for(int i=0; i<n; i++){
if(Wire.available()){
cmd[i]=Wire.read();
// Serial.print(cmd[i],HEX);
}
}
// Serial.println();
if(!writeFlag) return;
if(cmd[0]==0x03){ // if read, read MSB of the sample rate,
// if write, set MSB of the sample rate.
unsigned int wi=sampleRate_ms;
wx[3]=wi & 0xff; wi=wi>>8;
wx[2]=wi & 0xff; wi=wi>>8;
wx[1]=wi & 0xff; wi=wi>>8;
wx[0]=wi & 0xff;
wx[2]=cmd[1];
sampleRate_ms=(((((wx[0]<<8)|wx[1])<<8)|wx[2])<<8)<<wx[3];
// Serial.print("sampleRate_ms=");
// Serial.println(sampleRate_ms);
} else if(cmd[0]==0x04) { // if read, read LSB of the sample rate,
// if write, set LSB of the sample rate.
unsigned int wi=sampleRate_ms;
wx[3]=wi & 0xff; wi=wi>>8;
wx[2]=wi & 0xff; wi=wi>>8;
wx[1]=wi & 0xff; wi=wi>>8;
wx[0]=wi & 0xff;
wx[0]=cmd[1];
sampleRate_ms=(((((wx[0]<<8)|wx[1])<<8)|wx[2])<<8)<<wx[3];
// Serial.print("sampleRate_ms=");
// Serial.println(sampleRate_ms);
}
}
void requestEvent(){
byte w;
// Serial.print("requestEvent, cmd=");
// Serial.println(cmd[0],HEX);
if(writeFlag) {
// Serial.println("w, return.");
return;
}
else{
// Serial.println("r, ...");
}
if(cmd[0]==0x00) { // if read, read currentActivity
w= (mq3_value >>8) & 0xff;
Wire.write(w);
// Serial.print("current=");
// Serial.println(w);
} else if(cmd[0]==0x01){ // if read, read MSB of the accumulated activity;
unsigned int wi=mq3_value;
wx[1]=wi & 0xff; wi=wi>>8;
wx[0]=wi & 0xff;
w=wx[0];
Wire.write(w);
// Serial.print("activity=");
// Serial.print(activity);
// Serial.print(", activity[0]=");
// Serial.println(wx[0]);
} else if(cmd[0]==0x02) { // if read, read LSB of the accumulated activity;
unsigned int wi=mq3_value;
wx[1]=wi & 0xff; wi=wi>>8;
wx[0]=wi & 0xff;
w=wx[1];
Wire.write(w);
// Serial.print("activity=");
// Serial.print(activity);
// Serial.print(", activity[1]=");
// Serial.println(wx[1]);
} else if(cmd[0]==0x03){ // if read, read MSB of the sample rate,
unsigned int wi=sampleRate_ms;
wx[3]=wi & 0xff; wi=wi>>8;
wx[2]=wi & 0xff; wi=wi>>8;
wx[1]=wi & 0xff; wi=wi>>8;
wx[0]=wi & 0xff;
w=wx[2];
Wire.write(w);
// Serial.print("sampleRate_ms=");
// Serial.print(sampleRate_ms);
// Serial.print(", sampleRate_ms[2]=");
// Serial.println(wx[2]);
} else if(cmd[0]==0x04) { // if read, read LSB of the sample rate,
unsigned int wi=sampleRate_ms;
wx[3]=wi & 0xff; wi=wi>>8;
wx[2]=wi & 0xff; wi=wi>>8;
wx[1]=wi & 0xff; wi=wi>>8;
wx[0]=wi & 0xff;
w=wx[3];
Wire.write(w);
// Serial.print("sampleRate_ms=");
// Serial.print(sampleRate_ms);
// Serial.print(", sampleRate_ms[3]=");
// Serial.println(wx[3]);
}
}
}}
----
#counter
ページ名: