Hi,. im making a code where i need up to 12 capasitive buttons. on the CapasitiveSensor library.
the problem is that when i put more than 6 buttons, the program starts to do a delay. i dont know if this library has a limit of how many capasitive Sensors can you use or what.
this is my code:
#include <LiquidCrystal.h>
#include <CapacitiveSensor.h>
LiquidCrystal lcd(22, 24, 26, 28, 30, 32);
//--------------------------------------------Timer controll------
long previousMillis1 = 0;
long interval = 1000;
//----------------------------------------------------------------END
int counter = 0; // Caunter for Menu selector up / down
int counter2 = 0;
int pot1 = 0;
//----------------------------------------------------------------CS
CapacitiveSensor CSbutton1 = CapacitiveSensor(11,41); // the first number (#, n) is the comong ground pin of all the buttons
CapacitiveSensor CSbutton2 = CapacitiveSensor(11,43);
CapacitiveSensor CSbutton3 = CapacitiveSensor(11,45);
CapacitiveSensor CSbutton4 = CapacitiveSensor(11,47);
CapacitiveSensor CSbutton5 = CapacitiveSensor(12,33);
CapacitiveSensor CSbutton6 = CapacitiveSensor(12,35);
CapacitiveSensor CSbutton7 = CapacitiveSensor(12,37);
CapacitiveSensor CSbutton8 = CapacitiveSensor(12,39);
CapacitiveSensor CSbutton9 = CapacitiveSensor(11,49);
CapacitiveSensor CSbutton10 = CapacitiveSensor(11,51);
CapacitiveSensor CSbutton11 = CapacitiveSensor(12,53);
CapacitiveSensor CSbutton12 = CapacitiveSensor(12,31);
//------------------------------------------------------------------END
void setup()
{
Serial.begin(9600);
lcd.begin(20, 4);
/*
Servos
/
/
CapacitiveSensor CSbutton1 = CapacitiveSensor(11,41); // the first number (#, n) is the comong ground pin of all the buttons
CapacitiveSensor CSbutton2 = CapacitiveSensor(11,43);
CapacitiveSensor CSbutton3 = CapacitiveSensor(11,45);
CapacitiveSensor CSbutton4 = CapacitiveSensor(11,47);
CapacitiveSensor CSbutton5 = CapacitiveSensor(12,33);
CapacitiveSensor CSbutton6 = CapacitiveSensor(12,35);
CapacitiveSensor CSbutton7 = CapacitiveSensor(12,37);
CapacitiveSensor CSbutton8 = CapacitiveSensor(12,39);
CapacitiveSensor CSbutton9 = CapacitiveSensor(11,49);
CSbutton10.set_CS_AutocaL_Millis(0xFFFFFFFF); // CS
CSbutton11.set_CS_AutocaL_Millis(0xFFFFFFFF); // CS
CSbutton12.set_CS_AutocaL_Millis(0xFFFFFFFF); // CS
*/
menu_0();
menu_0_1();
menu_0_2();
menu_0_3();
menu_0_4();
menu_1();
menu_2();
menu_3();
}
void loop()
{
//----------------------------------------------------- —Capasitive Sensor Buttons Void
long start1 = millis();
long button1 = CSbutton1.capacitiveSensor(20);
long button2 = CSbutton2.capacitiveSensor(20);
long button3 = CSbutton3.capacitiveSensor(20);
long button4 = CSbutton4.capacitiveSensor(20);
long button9 = CSbutton9.capacitiveSensor(20);
long button10 = CSbutton10.capacitiveSensor(20);
unsigned long currentMillis1 = millis();
//---------------------------------------------Menu Up Down : Counter#1
// The next if is For The Menu, to go up
if(button9 > 1000) // total stands for the value of the capasitive Sensor AKA Button
{
if(currentMillis1 - previousMillis1 > interval / 3) // Set the time interval
{
previousMillis1 = currentMillis1; // mandatory statement
counter = counter - 1;
}
}
// The next if is For The Menu, to go Down
if(button10 > 1000) // total stands for the value of the capasitive Sensor AKA Button
{
if(currentMillis1 - previousMillis1 > interval / 3) // Set the time interval
{
previousMillis1 = currentMillis1; // mandatory statement
counter = counter + 1;
}
}
//--------------------------------------------------------------------------------IF statements.
counter = constrain(counter, 0, 3); // this sets the MIN and MAX of the counter
switch (counter)
{
case 0:
menu_0();
break;
case 1:
menu_1();
break;
case 2:
menu_2();
if(button2 > 600) // total stands for the value of the capasitive Sensor AKA Button
{
if(currentMillis1 - previousMillis1 > interval / 3) // Set the time interval
{
previousMillis1 = currentMillis1; // mandatory statement
pot1 = pot1 - 100;
}
}
// The next if is For The Menu, to go Down
if(button3 > 600) // total stands for the value of the capasitive Sensor AKA Button
{
if(currentMillis1 - previousMillis1 > interval / 3) // Set the time interval
{
previousMillis1 = currentMillis1; // mandatory statement
pot1 = pot1 + 100;
}
}
break;
case 3:
menu_3();
break;
}
if(button1 >= 600 && counter == 0)
{
menu_0_1();
}
else if(button2 >= 600 && counter == 0)
{
menu_0_2();
}
else if (button3 >= 600 && counter == 0)
{
menu_0_3();
}
else if (button4 >= 600 && counter == 0)
{
menu_0_4();
}
Serial.print("counter = “);
Serial.print(counter);
Serial.print(” Button#1 = “);
Serial.print(button1);
Serial.print(” Button#2 = “);
Serial.print(button2);
Serial.print(” Button#3 = ");
Serial.print(button3);
Serial.println();
}
void menu_0()
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print(“OPTION#1”);
lcd.setCursor(1,1);
lcd.print(“OPTION#2”);
lcd.setCursor(1,2);
lcd.print(“OPTION#3”);
lcd.setCursor(1,3);
lcd.print(“OPTION#4”);
}
void menu_0_1()
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print(“OPTION#1”);
}
void menu_0_2()
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print(“OPTION#2”);
}
void menu_0_3()
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print(“OPTION#3”);
}
void menu_0_4()
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print(“OPTION#4”);
}
//--------------------------------------------------------Menu #1
void menu_1()
{
lcd.clear();
lcd.setCursor(7,0);
lcd.print(“Menu #1”);
}
//----------------------------------------------------------END
//--------------------------------------------------------Menu #2
void menu_2()
{
lcd.clear();
lcd.setCursor(7,0);
lcd.print(“Menu #2”);
lcd.setCursor(5,1);
lcd.print(pot1);
}
//----------------------------------------------------------END
//--------------------------------------------------------Menu #3
void menu_3()
{
lcd.clear();
lcd.setCursor(7,0);
lcd.print(“Menu #3”);
}
//----------------------------------------------------------END