I want to get audible feedback when a button is pressed, and the processor is going to be busy counting, so delay is not an option.
I have written this code, which I find straighforward, but it functions in a strange way.
It acts just as if the button has a loose connection. Some few times the response is right, sometimes I get response every odd button press, the response is random and not reliable. There is something basically wrong with the code, but I am blind to it, so any help will be appreciated greatly.
Button is connected to pin 3, and the output is monitored on pin 4
I guess I am doing something so silly that I am overlooking the obvious.
here is the code:
const int beepPin = 4;
const byte interruptPin3 = 3;
unsigned long startMillis = 0;
unsigned long stopMillis = 0;
int val = 0;
int pulselength = 10;
void setup() {
pinMode (beepPin, OUTPUT);
pinMode(interruptPin3, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin3), triggerISP, FALLING);
Serial.begin(9600);
}
void loop() {
if (val == 1)
{
stopMillis = millis();
digitalWrite (beepPin, HIGH);
}
if (stopMillis - startMillis > pulselength)
{
digitalWrite (beepPin, LOW);
val = 0;
}
}
void triggerISP()
{
val = 1;
startMillis = millis ();
}
Cheers, Finn Hammer