I am new to the arduino and am using the capsense library too make touch buttons but it is too slow abt 5 secs delay between sensing and and turning led on here is the code below
#include <CapacitiveSensor.h>
//make sure to install the CapSense Library then upload to your Arduino.
//for more information on the basic workings of this sketch check out http://bareconductive.com/capacitance-sensor#define Mpin 10
#define Apin 11
#define Ipin 12
#define Npin 13
const int threshold = 0; //This is the threshold that you should adjust after watching the serial port. The MAINSpin is only set high if “total 1” is greater than this number
int val1 =0;
int old_val1 = 0;
int state1 = 0;
int Touch1 = 0;
int val2 =0;
int old_val2 = 0;
int state2 = 0;
int Touch2 = 0;
int val3 =0;
int old_val3 = 0;
int state3 = 0;
int Touch3 = 0;
int val4 =0;
int old_val4 = 0;
int state4 = 0;
int Touch4 = 0;CapacitiveSensor cs_M = CapacitiveSensor(4,2); // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil
CapacitiveSensor cs_A = CapacitiveSensor(4,3); // 10 megohm resistor between pins 4 & 3, pin 3 is sensor pin, add wire, foil
CapacitiveSensor cs_I = CapacitiveSensor(4,5); // 10 megohm resistor between pins 4 & 5, pin 5 is sensor pin, add wire, foil
CapacitiveSensor cs_N = CapacitiveSensor(4,6); // 10 megohm resistor between pins 4 & 6, pin 6 is sensor pin, add wire, foil
void setup()
{pinMode(Mpin, OUTPUT);
digitalWrite(Mpin, LOW);
cs_M.set_CS_AutocaL_Millis(0xFFFFFFFF);
pinMode(Apin, OUTPUT);
digitalWrite(Apin, LOW);
cs_A.set_CS_AutocaL_Millis(0xFFFFFFFF);
pinMode(Ipin, OUTPUT);
digitalWrite(Ipin, LOW);
cs_I.set_CS_AutocaL_Millis(0xFFFFFFFF);
pinMode(Npin, OUTPUT);
digitalWrite(Npin, LOW);
cs_N.set_CS_AutocaL_Millis(0xFFFFFFFF);}
void loop()
{
val1 = Touch1;
val2 = Touch2;
val3 = Touch3;
val4 = Touch4;
long total1 = cs_M.capacitiveSensor(10);
long total2 = cs_A.capacitiveSensor(10);
long total3 = cs_I.capacitiveSensor(10);
long total4 = cs_N.capacitiveSensor(10);if (total1 > threshold){
Touch1 = 1;
} else {
Touch1 = 0;
}if ((val1 == HIGH)&& (old_val1==LOW)){
state1 = 1-state1;delay (0); //delay for debouncing
}old_val1 = val1;
if (state1 == 1) {
digitalWrite(Mpin, HIGH); //turn LED ON
} else {
digitalWrite(Mpin, LOW);
}
if (total2 > threshold){
Touch2 = 1;
} else {
Touch2 = 0;
}if ((val2 == HIGH)&& (old_val2==LOW)){
state2 = 1-state2;delay (0); //delay for debouncing
}old_val2 = val2;
if (state2 == 1) {
digitalWrite(Apin, HIGH); //turn LED ON
} else {
digitalWrite(Apin, LOW);
}
if (total3 > threshold){
Touch3 = 1;
} else {
Touch3 = 0;
}if ((val3 == HIGH)&& (old_val3==LOW)){
state3 = 1-state3;delay (0); //delay for debouncing
}old_val3 = val3;
if (state3 == 1) {
digitalWrite(Ipin, HIGH); //turn LED ON
} else {
digitalWrite(Ipin, LOW);
}
if (total4 > threshold){
Touch4 = 1;
} else {
Touch4 = 0;
}if ((val4 == HIGH)&& (old_val4==LOW)){
state4 = 1-state4;delay (0); //delay for debouncing
}old_val4 = val4;
if (state4 == 1) {
digitalWrite(Npin, HIGH); //turn LED ON
} else {
digitalWrite(Npin, LOW);
}
}
Moderator edit: Please DON’T use “format for forum” or whatever it is called.