※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

/***
  呼吸-LED1
  pwmを用いた
  ***/
 
//sekibun
  int i = 0;
  int ad;
  int valS[101];
  long sum = 0;
  int heikin1, heikin2;
  int now;
//maxx
  int maxx;
  int widthh;
  char line1, line2;
  long t = 0;
  long time1, time2;
 
//LED
  int ledPin3 = 11; // LED connected to digital pin 11
  int ledVal = 1;
  int PW1 = 1;
  int PW2 = 4;
  int k = 0;
 
//その他
  int k1 = 0;
  int k2 = 0;
 
void setup()
{
  Serial.begin(9600);
  // sets the digital pin as output
  pinMode(ledPin3, OUTPUT);  
}
 
void loop()
{
  sum = 0;
  heikin1 = heikin2;
  // ここで100幅分の平均値を計算
  for(i=0 ; i<100 ; i++){
      ad = analogRead(0);
      valS[i] = ad;
      sum += valS[i];
 
      if(k == 1){ // 下向きのベクトルのとき(温度下降時)
        PW1 = 1;
        if(PW2 <= 20){
           PW2 = PW2 + 1 ; // LEDだんだん明るく
        }
        if(PW2 > 20){
           PW1 = 0;
        }
      }
      if(k == 0){ // 上向きのベクトルのとき(温度上昇時)
         PW1 = 1;
         if(PW2 >= 1){
           PW2 = PW2 - 1;// LEDだんだん明るく
         }
      }
      // pwm操作
      digitalWrite(ledPin3, HIGH);  // sets the LED on
      delay(PW1);                  // waits for 10-PW 
      digitalWrite(ledPin3, LOW);  // sets the LED off
      delay(PW2);      // waits for PW (PW=1000 -> 1sec)
    }
    heikin2 = sum/100;
  }
 
  //* Down *//
  if(heikin1 > heikin2){
    if((line1 == 'U' && line2 == 'F') 
    || (line1 == 'F' && line2 == 'U') 
    || (line1 == 'U' && line2 == 'U')){
      maxx = heikin1;  // 極大値の更新
      time1 = time2;
      time2 = t;
      widthh = time2 - time1;      // 振幅
    line1 = line2;
    line2 = 'D';
 
    k = 1;
    }
 
    else if(line1 == 'F' && line2 == 'F'){
      line1 = line2;
      line2 = 'D';
 
      k = 1;
    }
    else{
      line1 = line2;
      line2 = 'F';
    }
}
 
  //* Up *//
  else if(heikin1 < heikin2){
    if((line1 == 'D' && line2 == 'F') 
    || (line1 == 'F' && line2 == 'D') 
    || (line1 == 'D' && line2 == 'D') 
    || (line1 == 'F' && line2 == 'F')){
      line1 = line2;
      line2 = 'U';
 
      k = 0;
    }
    else{
      line1 = line2;
      line2 = 'F';
    }
  }
 
  //* Flat *//
  else {
    line1 = line2;
    line2 = 'F';
  }
  Serial.println(line2);
}
 
 
----