Go Down

Topic: Sensor Delay Problem (Read 348 times) previous topic - next topic

I've been working on this multiple channel capacitive touch sensor project and I've seemed to hit a bump. The code is written to turn on two different leds from two different switches but it seems to be rather slow and lag. Looking for some advice as to what I should do as I have messed with all the variables and have come to no avail.

Code: [Select]


#include <CapacitiveSensor.h>
// pin 2/4 sends electrical energy
// pin 3/5 senses a change
CapacitiveSensor   capSensor1 = CapacitiveSensor(2,3);       
CapacitiveSensor   capSensor2 = CapacitiveSensor(4,5);       

// threshold for turning the LEDs on
int  threshold1 = 10;
int  threshold2 = 10;

// pin the LEDs are connected to
const int ledPin1 = 8;
const int ledPin2 = 9;

void setup() {
   // set the LED pin as an output
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);}



void loop() {
  // store the value reported by the sensors in a variable
  short sensor1Value = capSensor1.capacitiveSensor(30);
  short sensor2Value = capSensor2.capacitiveSensor(30);

  // if the value is greater than the threshold
  if(sensor1Value > threshold1) {
    // turn the LED on
    digitalWrite(ledPin1, HIGH);
    delay(500);
    // turn the LED off
    digitalWrite(ledPin1, LOW);
}

  // if the value is greater than the threshold
  if(sensor2Value > threshold2) {
    // turn the LED on
    digitalWrite(ledPin2, HIGH);
    delay(500);
    // turn the LED off
    digitalWrite(ledPin2, LOW);
  }}



jremington

Every time you turn on an LED, the computer sits in a delay loop for 1/2 second. So, yes, it will be slow. See the "Blink without Delay" example for how to avoid this very situation.

Go Up