Sumary:
I have a sensor (switch = nail & spring type) when you flick it..it makes connection.
I tried to use an external interrupt..as I thought that would be the best approach to save other resources for other tasks in my code..
however.. I quickly became riddled with BOUNCE issues from that sensor/switch... as the nail & spring made contact more than once every time you 'flicked it'.. wasnt sure how to handle that using an external interrupt.. I tried to detach and re-attach the interrupt..but didnt work out so well.... I mean I 'guess' it worked.. but it 'sometimes' registered more than one 'contact' per hit.. even though I believe Im using a timer to 'delay' checking for the event for 100ms.. which should be enough time to allow for the bounce? (perhaps not?)
and Im not even sure this is a good way to approach this..since the interrupt is really always firing/changing/setting the variable.. Im really only delaying checking that var every 100ms
int pin = 13;
int pin2 = 2;
volatile int state = LOW;
long previousMillis = 0;
long interval = 100;
long diff = 0;
boolean disable = false;
void setup(){
Serial.begin(9600);
pinMode(pin, OUTPUT);
pinMode(pin2, INPUT);
digitalWrite(pin2, HIGH);
attachInterrupt(0, ISR1, HIGH);
}
void loop(){
diff = millis() - previousMillis;
if (diff > interval) {
//Serial.println("--TIME HAS PASSED--");
//update timer var
previousMillis = millis();
if(state == HIGH){
Serial.println("--EVENT TRIGGERED--");
//tone(8, 350, 50);
digitalWrite(pin, state);
delay(50);
state = LOW;
digitalWrite(pin, state);
}
}
}
void ISR1(){
state = HIGH;
}
so I posted..and was told not to use interrupts at all..and just use a 'timer'.
So now Im trying to using this 'timer' as my debounce solution.. (that is the way to handle it correct?) (no External Interrupt)..
but it seems to ALWAYS be executing the statements for an 'event'.
even when not flicking the sensor..
int ledPin = 13;
int cPin = 2;
int cState = LOW;
long cCheck = 0;
long cInterval = 100;
long cDiff = 0;
void setup(){
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(cPin, INPUT);
//digitalWrite(cPin, HIGH);
}
void loop(){
cDiff = (millis() - cCheck);
if (cDiff > cInterval) {
cCheck = millis();
cState = digitalRead(cPin);
if(cState == HIGH){
Serial.println("--EVENT TRIGGERED--");
//tone(8, 350, 50);
digitalWrite(ledPin, cState);
delay(50);
cState = LOW;
digitalWrite(ledPin, cState);
}
}
}
seems to ALWAYS be reading the cPin as HIGH??
tried to comment out that line so the pin wasnt 'floating'.. but then its always HIGH?
(better with code than I am with hardware)..
this is the demo board I made to test this sensor/switch..

the other two will be checked more or less the same way..except they are ball bearing type sensors/switches..
I basically am looking for a way to 'track/listen' to these 3 sensors.. at all times. (no bounce...not in my house) =)... and execute a 'sound' (or whatever) each time that sensor completes the circuit.
the ballbearing sensors are for X & Y tilt/swing measuring/tracking..
the the nail & spring sensor/switch is to detect a 'hit' event..
these are the base, continuous functions I need to do (a few others as well) perform the whole time my project is powered/on
I think maybe my PIN is floating..cant set it HIGH..because that triggers the event...
digitalWrite(cPin, HIGH);
is how you set the internal pull up resistor..yes?
cant set it LOW..cause it dont work that way...