pico_wiki_driver_03.py

٤Ƴ٤Ĥ
  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
-
|
|
|
|
!
 
 
 
 
 
 
 
 
 
 
 
-
|
|
|
|
-
!
-
!
-
!
|
!
 
-
|
|
|
|
-
|
|
|
-
|
!
|
|
|
|
|
|
!
!
 
 
 
 
 
 
-
-
|
|
!
-
-
|
-
!
!
-
!
-
-
!
-
-
!
!
 
!
|
-
 
-
|
-
-
!
!
-
-
!
-
 
 
-
|
-
|
|
|
|
|
-
 
 
 
-
|
|
|
|
|
-
|
|
|
-
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
|
|
|
|
-
|
|
|
!
!
/**********
DSM501A とarduinoの接続
#3 vcc             5v
#4 vout1          pin8
#5 GND            GND
**********/       
 
int pin = 8;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;
unsigned int i_concentration=0;
#define SLAVE_ADDRESS 0x15
 
void setup() {
  Serial.begin(9600);
  pinMode(8,INPUT);
  starttime = millis();
 
  // initialize i2c as slave
  Wire.begin(SLAVE_ADDRESS);
  //Wire.setClock(400000); //400Khz i2c clock
 
  // define callbacks for i2c communication
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
}
 
void loop() {
  duration = pulseIn(pin, LOW);
  lowpulseoccupancy = lowpulseoccupancy+duration;
 
  if ((millis()-starttime) > sampletime_ms)
  {
    ratio = lowpulseoccupancy/(sampletime_ms*10.0);  // Integer percentage 0=>100
    concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
    i_conecentration=concentration;
    //Serial.print(lowpulseoccupancy);
    // Serial.print(",");
    Serial.print(ratio);
    Serial.print(",");
    Serial.print(concentration);
    Serial.print(" _ ");
    Serial.println(i_concentration);
    lowpulseoccupancy = 0;
    starttime = millis();
  }
}
 
 
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.
 
  } else if(cmd[0]==0x04) { // if read, read LSB of the sample rate,
                            // if write, set LSB of the sample rate.
 
  } 
 
}
 
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= (i_concentration >>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=concentration;
      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=concentration;
      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=concentration;
      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=concentration;
      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]);      
  }
}

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS