Arduino too slow HELP needed!!

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.

delay (0); //delay for debouncing

Doesn't this look pretty useless to you? If you don't want a delay, delete the line.

There's a whole lot of ugly looking code there, and not a single Serial.print() statement in sight. What IS your program doing? How do you know?

I thought that the delay was causing the prob so i put zero in there but actually i need it also i am not good at coding so your help will be really appreciated :)

i am not good at coding

Looking up the format of the Serial.begin() function, and looking at some examples that use it, is not rocket science. Even a lousy programmer can do that.

Looking up the format of the Serial.print() function, and looking at some examples that use it, is not rocket science. Even a lousy programmer can do that.

Looking up the format of the Serial.println() function, and looking at some examples that use it, is not rocket science. Even a lousy programmer can do that.

The hard part will be determining what to print. What is your program reading from the sensors? You don't know? So, print the value. When you are sure that the value(s) make sense, you can comment out the print()s.

Either make the delay() value reasonable, or get rid of it. I seem to recall that there is an issue with delay(0) causing problems. Seeing as how it is a useless thing to do, the urgency for fixing the problem is not really there.

I had implemented the serial commands but deleted them coz I thought that it is too much load on the processor plz help man thanks

I had implemented the serial commands but deleted them coz I thought that it is too much load on the processor plz help man thanks

OK. Because is not spelled coz. Please is not spelled plz.

You thought wrong. The processor runs full out all the time, doing something useful or doing nothing. Serial output will slow the processor down, because it has something to do. But, your program is hardly challenging the processor. So, put them back until you figure out why the program takes so long to react to a change.

Doing that for 4 switches at once is silly. Dump three of them.

A single switch works great but when the rest r added it gets too slow is it because the loop function is too long?

1 more thing is that when i use a single switch the serial monitor's numbers scroll real fast and when i use 4 switches there is a 5 second delay between numbers appearing HELP will be appreciated :)

1 more thing is that when i use a single switch the serial monitor's numbers scroll real fast and when i use 4 switches there is a 5 second delay between numbers appearing HELP will be appreciated

So, we have two data points. 1 switch and the numbers are real fast. No clue what they are, but they scroll by fast.

4 switches causes a 5 second delay between numbers. No clue what they are, but there is a delay.

So, two switches? What are these numbers?

the numbers are not random they are the amount of capacitance and with 2 switches it is still 5 seconds.

the numbers are not random

No one said they were. You made some code changes that you haven't showed us. You get some output that you haven't showed us. The balls in your court.

Here is the code with one switch.

#include <CapacitiveSensor.h>
#define MAINSpin 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 val =0;
int old_val = 0;
int state = 0;
int Touch = 0;

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,6);        // Your resistor goes between pins 4 & 6. Your pad of paint should be connected to pin 6
void setup()                    
{ 

pinMode(MAINSpin, OUTPUT);
digitalWrite(MAINSpin, LOW);  
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     
Serial.begin(9600);
}
void loop()                    
{      
val = Touch;                     
long total1 =  cs_4_2.capacitiveSensor(100);  

Serial.println(total1);                  
Serial.println(" ");                       
       

if (total1 > threshold){      
Touch = 1;    
} else {      
Touch = 0;    
}      

if ((val == HIGH)&& (old_val==LOW)){   
state = 1-state;      

delay (50); //delay for debouncing  
}    

old_val = val;     

if (state == 1) {    
digitalWrite(MAINSpin, HIGH); //turn LED ON  
} else {    
digitalWrite(MAINSpin, LOW);  
} 
  }

here is the serial print window output
0 60 0 0 0 60 0

And the code with two switches

#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);
Serial.begin(9600);
Serial.println(total2);


  
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);  
//} 
  }

heres the output
0 [5 secs] 0 [5 secs] 60

Here is the code with one switch.

delay (50); //delay for debouncing

And the code with two switches

delay (0); //delay for debouncing  
delay (0); //delay for debouncing  
//delay (0); //delay for debouncing  
//delay (0); //delay for debouncing

So, the one switch case is fast, and the 2 switch case is slow. I wonder why?

the one with 50 is the oldest piece of code but even with the delay its faster.

the one with 50 is the oldest piece of code but even with the delay its faster.

It's faster than the code with the 0 value.

Doesn't that tell you anything? Here. Here's a clue by four. Smack yourself with it a couple of times. Then, change the 0 to something else OR delete the delay() call. Your choice, but do something or quit whining.

checked it thrice no change

Help guys Help PLease

checked it thrice no change

I have no clue what this means.

Help guys Help PLease

Homework's due today, huh?

I have no clue what this means.

changed the delay to 50!!

Homework's due today, huh?

Maybe...

The only thing that I can see that might (not does) explain why two switches are order of magnitude slower than one is that you are using the same output pin for all of them. You might try changing to use 4 different output pins. Nothing in the code suggests why this might be a problem.

One other thing to try is to see if it is one particular pin that is causing a big delay. If each pin by itself is fast, then I don't know why two or more are slow, unless using the same output pin is a problem.

Maybe...

We have no problem helping with homework. It's better if you say so up front.