Langues

Répondre au commentaire

TouchSensor.h

Here is a small class that I implemented which works GREAT for detecting touches. It also explains the circuit that needs to be built.

/*
 * TouchSensor.h
 * 
 * (c) 2008 Sofian Audry (info __AT__ sofianaudry __DOT__ com)
 *
 * This code was adapted from a technique by Pontus Frössander described at
 * http://www.e.kth.se/~pontusf/touchsensor.html
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
#ifndef TOUCH_SENSOR_INC
#define TOUCH_SENSOR_INC

/*

Circuit schematics
------------------


                ~0.5 M 
  PIN_OUT -----/\/\/\----*
                         |
  PIN_IN  ---------------*------------- SENSOR
                         |
                         = 100 pF
                         |
                        GND

The sensor is a simple copper plate, with or without insulation.
The 100pF capacitor can be dropped if the circuit is grounded, it is
especially recommended for battery-operated systems that are not plugged
to ground.

Example Arduino code
------------------------------
#include "TouchSensor.h"

#define SIGNAL_OUT 5
#define SIGNAL_IN 6

#define N_SAMPLES 8
#define THRESHOLD 0.2f

TouchSensor sensor(SIGNAL_OUT, SIGNAL_IN, N_SAMPLES);

void setup() {
  sensor.init();
  Serial.begin(9600);
}

void loop() {
  float s = sensor.sense();
  if (s >= THRESHOLD) {
    Serial.print("Touch: "); Serial.print(round(s*100)); Serial.println("%");
  }
}
  
*/
class TouchSensor {
  byte _sendPin;
  byte _receivePin;
  int _nSamples;
  
public:
  TouchSensor(byte sendPin, byte receivePin, int nSamples = 8)
    : _sendPin(sendPin), _receivePin(receivePin), _nSamples(nSamples) {}
    
  void init() {
    pinMode(_sendPin, OUTPUT);
    pinMode(_receivePin, INPUT);
  }
  
  boolean detect(float threshold = 0.5f) {
    return (sense() >= threshold);
  }
  
  boolean senseOne() {
    digitalWrite(_sendPin, LOW);
    delay(2);
    digitalWrite(_sendPin, HIGH);
    delayMicroseconds(5);
    return (digitalRead(_receivePin) != HIGH);
  }
  
  float sense() {
    float nTouches = 0;
    for (int i=0; i<_nSamples; i++) {
      if (senseOne())
         nTouches++;
    }
    return nTouches / (float)_nSamples;
  }
  
};

#endif

Répondre

Le contenu de ce champ ne sera pas montré publiquement.
  • Les adresses de pages web et de messagerie électronique sont transformées en liens automatiquement.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <h3> <h4> <h5> <h6>
  • Les lignes et les paragraphes vont à la ligne automatiquement.

Plus d'informations sur les options de formatage

CAPTCHA
Cette question permet de vérifier si vous êtes un visiteur humain afin d'empêcher les envois automatisés de pourriel.
Fill in the blank
By submitting this form, you accept the Mollom privacy policy.