PicoSense: Capteur Tactile – Module Tactile à 4 Touches – Capteur de contact

Tuto Arduino I: Contrôle tactile des LEDs

Capteur Tactile – Module Tactile Arduino_3
#define nTouches      4
#define nMean         32
#define timeoTouchms  1000

#define T1Pin         A0     
#define T2Pin         A1    
#define T3Pin         A2     
#define T4Pin         A3 

#define LED1Pin       3     
#define LED2Pin       4    
#define LED3Pin       5     
#define LED4Pin       6   
#define LEDTreshInt   120
#define timeoLEDs     500

const int psPins[nTouches]={T1Pin, 
                            T2Pin, 
                            T3Pin, 
                            T4Pin}; 

const int ledPins[nTouches]={LED1Pin, 
                             LED2Pin, 
                             LED3Pin, 
                             LED4Pin}; 

bool ledState[nTouches]={false, 
                         false, 
                         false, 
                         false}; 
struct TouchV{
    double  inst_touch_volt;
    double  touch_integ_volt;
    double  touch_raw_volt;
};

static unsigned int T0; 

bool ledStatus[nTouches]; 

void setup() {
  //Init UART to display values 
  Serial.begin(115200);

  //Init 
  initTouch(ledPins, nTouches); 

  //Init Timer 
  T0=millis();
}

void loop() {
  for(int i=0;i<nTouches;i++){
    //Read Touch (T2, T2, T3 or T4)
    TouchV toch_i=readTouch(i, timeoTouchms, nMean); //T1 

    //Compute Touch Status 
    bool touch_i=toch_i.touch_integ_volt > LEDTreshInt;
    if(millis()>timeoLEDs+T0){
      if(touch_i==true){
        ledState[i]=!ledState[i];  
      }
      T0=millis();
    }

    //Display (Only T1)
    // Serial.println(String(ledState[0])+","+
    //                String(ledState[1])+","+
    //                String(ledState[2])+","+
    //                String(ledState[3]));
    if(i==0)
      Serial.println(String(toch_i.touch_integ_volt)+","+
                     String(toch_i.touch_raw_volt)+","+
                     String(ledState[0]));

    //Set LEDs
    digitalWrite(ledPins[i], touch_i);
  }
  //Loop delay
  delay(25);
}

TouchV readTouch(int touch_pin, int touche_timeo_ms, int num_mean){
  static unsigned int T0=millis(); 
  double touche_inst=0.0; 
  static double touche_integ=0.0; 
  static unsigned int integ_count=0; 
  static TouchV touche_vals; 
  static double mean_val=0.0, sum_mean=0.0;; 
  static int count_mean=0; 
  
  if(integ_count==0){
    touche_vals.touch_integ_volt=0.0;
    integ_count++;
  }

  touche_inst=(double)analogRead(touch_pin)*5.0/1023.0; 
  touche_vals.touch_raw_volt=touche_inst;

  if(num_mean<=0)mean_val=0.0;
  else{
    if(count_mean==num_mean){
      mean_val=sum_mean/(double)num_mean;
      count_mean=0.0;
      sum_mean=0.0;
    }else{
      count_mean++;
      sum_mean+=touche_inst;
    }
  }
  touche_inst=abs(touche_inst-mean_val);
  // Serial.println(mean_val);

  if(millis()>touche_timeo_ms+T0){
    touche_vals.touch_integ_volt=touche_integ; 
    touche_integ=0.0;
    T0=millis();
  }else{
    touche_integ+=touche_inst;
  }
  touche_vals.inst_touch_volt=touche_inst;
  return touche_vals; 
}

void initTouch(int *led_pins, int num_touch){
  for(int i=0;i<num_touch;i++){
    pinMode(led_pins[i], OUTPUT);
    digitalWrite(led_pins[i], LOW);
  }
}

Tuto Arduino II: Alarme Tactile

Capteur Tactile – Module Tactile Arduino_2
#define nTouches      4
#define nMean         32
#define timeoTouchms  1000

#define T1Pin         A0     
#define T2Pin         A1    
#define T3Pin         A2     
#define T4Pin         A3 

#define alarmePin       10     
#define alarmeThreshInt 120     
#define timeoAlarme     100

int psPins[nTouches]={T1Pin, 
                      T2Pin, 
                      T3Pin, 
                      T4Pin}; 

bool touchState[nTouches]={false, 
                         false, 
                         false, 
                         false}; 
struct TouchV{
    double  inst_touch_volt;
    double  touch_integ_volt;
    double  touch_raw_volt;
};

static unsigned int T0; 

bool ledStatus[nTouches]; 

void setup() {
  //Init UART to display values 
  Serial.begin(115200);

  //Init 
  initTouch(alarmePin); 

  //Init Timer 
  T0=millis();
}

void loop() {
  for(int i=0;i<nTouches;i++){
    //Read touch (T2, T2, T3 or T4)
    TouchV toch_i=readTouch(i, timeoTouchms, nMean); 

    //Compute touch status 
    bool touch_i=toch_i.touch_integ_volt > alarmeThreshInt;
    if(millis()>T0+200){
      touchState[i]=touch_i;
      T0=millis();
    }

    //Display
    // Serial.println(String(touchState[0])+","+
    //                String(touchState[1])+","+
    //                String(touchState[2])+","+
    //                String(touchState[3]));

    // if(i==0)
    //   Serial.println(String(toch_i.inst_touch_volt)+","+
    //                  String(toch_i.touch_integ_volt)+","+
    //                  String(touchState[0]));

  }

  //Compute & display alarme status 
  bool alarm_status= touchState[0] | touchState[1] |
                     touchState[2] | touchState[3];
  Serial.println(alarm_status);

  //Update alarme
  digitalWrite(alarmePin, alarm_status);

  //Loop delay
  delay(25);
}

TouchV readTouch(int touch_pin, int touche_timeo_ms, int num_mean){
  static unsigned int T0=millis(); 
  double touche_inst=0.0; 
  static double touche_integ=0.0; 
  static unsigned int integ_count=0; 
  static TouchV touche_vals; 
  static double mean_val=0.0, sum_mean=0.0;; 
  static int count_mean=0; 
  
  if(integ_count==0){
    touche_vals.touch_integ_volt=0.0;
    integ_count++;
  }

  touche_inst=(double)analogRead(touch_pin)*5.0/1023.0; 
  touche_vals.touch_raw_volt=touche_inst;

  if(num_mean<=0)mean_val=0.0;
  else{
    if(count_mean==num_mean){
      mean_val=sum_mean/(double)num_mean;
      count_mean=0.0;
      sum_mean=0.0;
    }else{
      count_mean++;
      sum_mean+=touche_inst;
    }
  }
  touche_inst=abs(touche_inst-mean_val);
  // Serial.println(mean_val);

  if(millis()>touche_timeo_ms+T0){
    touche_vals.touch_integ_volt=touche_integ; 
    touche_integ=0.0;
    T0=millis();
  }else{
    touche_integ+=touche_inst;
  }
  touche_vals.inst_touch_volt=touche_inst;
  return touche_vals; 
}

void initTouch(int alarm_pin){
  pinMode(alarm_pin, OUTPUT);
  digitalWrite(alarm_pin, LOW);
}

Tuto Arduino III: Visualisation de
l’activité électrostatique

#define nTouches      4
#define nMean         32
#define timeoTouchms  1000
#define T1Pin         A0     
#define T2Pin         A1    
#define T3Pin         A2     
#define T4Pin         A3     

int psPins[nTouches]={T1Pin, 
                      T2Pin, 
                      T3Pin, 
                      T4Pin}; 

struct TouchV{
    double  inst_touch_volt;
    double  touch_integ_volt;
    double  touch_raw_volt;
};

void setup() {
  //Init UART to display values 
  Serial.begin(115200);
}

void loop() {
  //Read & Display Touch (T2, T2, T3 or T4) 
  TouchV toch_i=readTouch(T1Pin, timeoTouchms, nMean); //T1 
  // TouchV toch_i=readTouch(T2Pin, timeoTouchms, nMean); //T2 
  // TouchV toch_i=readTouch(T3Pin, timeoTouchms, nMean); //T3 
  // TouchV toch_i=readTouch(T4Pin, timeoTouchms, nMean); //T4 
  Serial.println(String(toch_i.inst_touch_volt)+","+String(toch_i.touch_raw_volt));
  // Serial.println(String(toch_i.inst_touch_volt)+","+
  // String(toch_i.touch_integ_volt)+","+String(toch_i.touch_raw_volt));
  
  //Loop delay
  delay(10);
}

TouchV readTouch(int touch_pin, int touche_timeo_ms, int num_mean){
  static unsigned int T0=millis(); 
  double touche_inst=0.0; 
  static double touche_integ=0.0; 
  static unsigned int integ_count=0; 
  static TouchV touche_vals; 
  static double mean_val=0.0, sum_mean=0.0;; 
  static int count_mean=0; 
  
  if(integ_count==0){
    touche_vals.touch_integ_volt=0.0;
    integ_count++;
  }

  touche_inst=(double)analogRead(touch_pin)*5.0/1023.0; 
  touche_vals.touch_raw_volt=touche_inst;

  if(num_mean<=0)mean_val=0.0;
  else{
    if(count_mean==num_mean){
      mean_val=sum_mean/(double)num_mean;
      count_mean=0.0;
      sum_mean=0.0;
    }else{
      count_mean++;
      sum_mean+=touche_inst;
    }
  }
  touche_inst=abs(touche_inst-mean_val);
  // Serial.println(mean_val);

  if(millis()>touche_timeo_ms+T0){
    touche_vals.touch_integ_volt=touche_integ; 
    touche_integ=0.0;
    T0=millis();
  }else{
    touche_integ+=touche_inst;
  }
  touche_vals.inst_touch_volt=touche_inst;
  return touche_vals; 
}
Accéder aux Codes