Hey, I am using Capacitive Sensors as buttons with the CapacitiveSensors.h library, 3 sensors in total. Arduino Nano is my platform
I coded a state machine with a timer interrupt. I count in my Timer declaration to 16000 for 1 ms. In my ISR I count to a specific time, depends of what I want to do. For example count=1000 is 1 second.
If i press the first or the second button, i change the counter in ISR to 100 for scaning my buttons every 100ms and then i write digital to a BCD and then to a 7 Segment. Then if the first button was pressed, the counter changes to 1000. My ISR does thw whole logic. The counter change i make in the void loop.
My problem: It doesn’t work well. Sometimes if I tap the buttons, the numbers on the display counts in the right duration. If I keep my finger on the button, the number freezes on the display and it counts internal and after some time a number shows up. If I change the counter to 400ms, then it works better for a reason, but the tapping doesn’t work well.
I hope my english was not awefull an i explained it understandable.
I decreased my code to loop and ISR. You can also download the wohle code(sir4.ino):
#include <CapacitiveSensor.h>
#include <EEPROM.h>
#include <digitalWriteFast.h>
CapacitiveSensor cs_2_A0 = CapacitiveSensor(2,A0); // 10M resistor between pins 2 & A0, pin A0 is sensor pin, add a wire and or foil if desired
CapacitiveSensor cs_2_A1 = CapacitiveSensor(2,A1); // 10M resistor between pins 2 & A1, pin A1 is sensor pin, add a wire and or foil
CapacitiveSensor cs_2_A2 = CapacitiveSensor(2,A2); // 10M resistor between pins 2 & A2, pin A2 is sensor pin, add a wire and or foil
#define BCD_D1 10
#define BCD_C1 11
#define BCD_B1 12
#define BCD_A1 9
#define BCD_D2 7
#define BCD_C2 6
#define BCD_B2 5
#define BCD_A2 8
#define S1 4
#define S2 3
#define DP1 A4
#define DP2 A5
int iAddr = 0;
int iCountleft=0;
int iCountright=0;
int iTimenumber = 0;
int iLeftnumber;
int iRightnumber;
int iCounter=0;
//int iCounter1=0;
//int iCounter2=1000;
int iCount_button=0;
int iCount_buttonstart=0;
int iCount_isr=100;
int iSoundcount = 0;
//int total1, total2, total3;
//int iStorenumber;
//int def_seconds=0;
//int def_min=59;
int iMinutes;
int iSeconds;
int iRandnumber;
//int iDP = HIGH;
boolean bStart= false;
boolean bStop;
boolean bReset = false;
boolean bPlaysound=false;
boolean bPresound=false;
boolean bButton=false;
boolean bPressed = false;
long total1;
long total2;
long total3;
void setup()
{
cs_2_A0.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example
cs_2_A1.set_CS_AutocaL_Millis(0xFFFFFFFF);
cs_2_A2.set_CS_AutocaL_Millis(0xFFFFFFFF);
iTimenumber=EEPROM.read(iAddr);
//********TIMER Declaration***********//
cli();
TCCR1A= 0;
TCCR1B= 0;
OCR1A=16000;
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS10); //Prescaler =1
TIMSK1 |= (1 << OCIE1A);
// enable global interrupts:
sei();
Serial.begin(9600);
}
void loop()
{
if(bBullshit==false)
{
iCount_isr=1000;
}
if(bBullshit==true)
{
iCount_isr=200;
}
to_BCD_left();
to_BCD_right();
if(bPresound==true)
{
presound();
}
if(bPlaysound ==true) //Sound Funktion
{
sound();
bPlaysound = false;
}
}
ISR(TIMER1_COMPA_vect)
{
if(iCounter>=iCount_isr)
{
iRandnumber = random(1,3);
iCounter=0;
total1 = cs_2_A1.capacitiveSensor(30);
total2 = cs_2_A0.capacitiveSensor(30);
total3 = cs_2_A2.capacitiveSensor(30);
if(bStart==true)
{
iTimenumber--;
iLeftnumber = iTimenumber / 10;
iRightnumber = iTimenumber % 10;
digitalWriteFast(DP1, LOW);
iCountright= iRightnumber;
iCountleft = iLeftnumber;
if(total1>6000)
{
bStart=false;
}
if(iTimenumber==20)
{
bPresound=true;
}
if(iTimenumber==0)
{
bStart=false;
bPlaysound=true;
}
}
else
{
digitalWriteFast(DP1, HIGH);
digitalWriteFast(DP2, HIGH);
if(total1>6000)
{
bButton=true;
iCount_buttonstart++;
if(iCount_buttonstart>3)
{
if(iTimenumber!=0)
{
EEPROM.write(iAddr, 0);}
iTimenumber=0;
}
}
if(total1<2000)
{
if(iCount_buttonstart<=3 && iCount_buttonstart>0)
{
bStart= true;
EEPROM.write(iAddr, iTimenumber);
iCount_buttonstart=0;
iCount_isr=1000;
}
}
else if(total2 < 2000 && total3 < 2000)
{
bPressed = false;
}
if (total2 >6000)
{
bPressed=true;
if(iTimenumber>=60)
{
iTimenumber=-1;
}
iTimenumber++;
}
if(total3 >6000)
{
bPressed=true;
if(iTimenumber<0)
{
iTimenumber=61;
}
iTimenumber--;
}
iLeftnumber = iTimenumber / 10;
iRightnumber = iTimenumber % 10;
iCountright= iRightnumber;
iCountleft = iLeftnumber;
}
}
iCounter++;
}
}
sir4.ino (13.1 KB)