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
//#define DEBUG true
class CapaSensor {
  private:
  int vCtrlPin;
  int vSensingPin;
  int vTryNumber;
  int vThreshold;
  
  public:
  CapaSensor(int aCtrlPin, int aSensingPin, int aTryNumber) {
    pinMode(aCtrlPin, OUTPUT);
    pinMode(aSensingPin, INPUT);
    vCtrlPin = aCtrlPin;
    vSensingPin = aSensingPin;
    vTryNumber = aTryNumber;
    vThreshold = (int)(read() * 1.2);
#if DEBUG    
    Serial.print("CapaSensor[");
    Serial.print(vCtrlPin);  
    Serial.print("-");
    Serial.print(vSensingPin);  
    Serial.print("x");
    Serial.print(aTryNumber);  
    Serial.print(">");
    Serial.println(vThreshold);
#endif
  }
  int read() {
    int ret = 0;
    for(int i = 0; i < vTryNumber; i++) {
      int cnt = 0;
      digitalWrite(vCtrlPin, HIGH);
      while(digitalRead(vSensingPin) < HIGH) {
        cnt++;
      }
      if (ret < cnt) ret = cnt;
      digitalWrite(vCtrlPin, LOW);
      delay(5);
    }
    return ret;
  }
  int touched() {
    int ret = 0;
    for(int i = 0; i < vTryNumber; i++) {
      int cnt = 0;
      digitalWrite(vCtrlPin, HIGH);
      while(digitalRead(vSensingPin) < HIGH) {
        cnt++;
        if (cnt > vThreshold) {
          return true;
          #if DEBUG
            Serial.print("@-");
            Serial.print(vThreshold);
            Serial.print(" <? ");
            Serial.println(cnt);
          #endif
        }
      }
      if (ret < cnt) ret = cnt;
      digitalWrite(vCtrlPin, LOW);
      delay(5);
    }
#if DEBUG
    Serial.print(ret > vThreshold ? "@ " : "  ");
    Serial.print(vThreshold);
    Serial.print(" <? ");
    Serial.println(ret);
#endif
    return ret > vThreshold;
  }
};
 
int oLed = 13;
CapaSensor* capa;
void setup() {
  Serial.begin(9600);
  Serial.print("Setup");
  analogWrite(oLed, 0);
  capa = new CapaSensor(8,9,10);
}
 
void loop() {
  analogWrite(oLed, capa->touched()?128:0);
  delay(100);
}
  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
#ifndef CapaSensorHpp
#define CapaSensorHpp
 
#include "Arduino.h"
 
class CapaSensor {
  private:
    int mDebug = false;
 
  public:
    CapaSensor(int aCtrlPin, int aDataPin, int aTryNumber) {
      pinMode(aCtrlPin, OUTPUT);
      pinMode(aDataPin,  INPUT);
       
      mCtrlPin = aCtrlPin;
      mDataPin = aDataPin;
      mTryNumber = aTryNumber;
      mThreshold = (int)(read() * 1.2);
 
      if (mDebug) {
        Serial.print("CapaSensor["); 
        Serial.print(mCtrlPin); 
        Serial.print(","); 
        Serial.print(mDataPin); 
        Serial.print("]x"); 
        Serial.print(mTryNumber); 
        Serial.print(">"); 
        Serial.print(mThreshold); 
        Serial.println("x"); 
      }
    }
    int read() {
      int ret = 0;
      fReset();
      for (int i = 0; i < mTryNumber; i ++) {
        digitalWrite(mCtrlPin, HIGH);
        for (int cnt = 0; digitalRead(mDataPin) < HIGH; cnt++) {
          if (ret < cnt) ret = cnt;
        }
        fReset();
      }
      return ret;
    }
    bool touched() {
      fReset();
      for (int i = 0; i < mTryNumber; i ++) {
        digitalWrite(mCtrlPin, HIGH);
        for (int cnt = 0; digitalRead(mDataPin) < HIGH; cnt++) {
          if (cnt > mThreshold) {
            if (mDebug) {
              Serial.print("  Capa = ");
              Serial.print(cnt);
              Serial.print(" >= ");
              Serial.println(mThreshold);
            }
            return true; 
          }
        }
        fReset();
      }
      return false;
    }
  private:
    int mCtrlPin;
    int mDataPin;
    
    int mTryNumber;
    int mThreshold;
 
    void fReset() {
      digitalWrite(mCtrlPin, LOW);
      while(digitalRead(mDataPin) > LOW) {;}
      delay(2);
    }
};
 
#endif
 
リロード   新規 編集 凍結 差分 添付 複製 改名   技術 一覧 検索 最新 バックアップ リンク元   ヘルプ   最終更新のRSS
Last-modified: 2017.0112 (木) 1644.2200 (256d)