FrontPage

٤Ƴ٤Ĥ
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
!
 
 
 
-
|
!
 
 
 
 
 
 
 
 
-
|
|
|
|
|
-
!
|
!
 
-
|
|
|
|
|
|
!
 
 
 
 
 
-
-
|
|
!
-
-
|
-
!
!
-
!
-
-
!
|
|
|
|
|
|
-
|
-
-
!
 
 
 
 
 
 
-
|
!
|
!
|
-
|
-
|
-
-
!
!
-
-
!
-
|
|
-
|
-
|
|
|
|
|
-
|
|
|
-
|
|
|
|
|
-
|
|
|
-
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
|
|
|
|
-
|
|
|
!
!
|
/**********
 
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: 121, today: 2, yesterday: 1

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2019-10-16 (水) 15:31:03 (1663d)