Thanks everyone,
To the member Cattledog, I did your correction, "attachInterrupt(2, GoodFunRISING, RISING);" you were right.
The results improved, but I still get a count on the RISING condition intermittently. Where am I going wrong?
Regarding why use interrupts?
The results of these counts will be displayed on a TFT that plugs in the MEGA.
https://www.amazon.com/kuman-Display-Screen-Socket-Arduino/dp/B01J3B08P8/ref=sr_1_3?ie=UTF8&qid=1513100324&sr=8-3&keywords=3.5+tft+arduino
The pertinent libraries are:
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
The letters and numbers to be dislpayed take a while, about 400mS.
If I dont use interrupts and use digitalRead:
While the display is refreshing the digitalread does not register. the Mega is busy and skips it.
Also if I do read 2 channels and the pulse comes a the same time, only one is read, intermittently.
So this does work but for a very slow counter.
If I do use interrupts:
Both channels register, even if they come in the same time. Even if the display is refreshing, I dont miss a count. Nice. The display refresh is stopped maybe for a mS , not perceptible.
The problem: My counts are not accurate. A correct debounced count is going on FALLING But another one is registered on RISING>
Can we please build this on the ISR procedure?
Thanks
Here is my latest code.
#define BUTTON_PIN 21 // Blue - pluse of the timer comes from the cord
#define Second_Button 20 //Wire to the Reject switch
#define KnobPushButton 15 // White
#define KeySwPin 16 //Brown
volatile int Good;
float GoodPercentage;
volatile int Rejects;
void setup() {
Serial.begin(9600);
attachInterrupt(2, GoodFun, FALLING); //interrupt 2 is pin 21 on mega-Button
// attachInterrupt(3, RjctFun, FALLING); //interrupt 3 is pin 20 on mega-Second Button
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(Second_Button, INPUT_PULLUP);
Serial.print ("READY");
}
void GoodFun()
{
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
// If interrupts come faster than 10ms, assume it's a bounce and ignore
if (interrupt_time - last_interrupt_time > 20)
{
Good++;
last_interrupt_time = interrupt_time;
//Button is LOW, Preparing for the Raising condition
detachInterrupt(2);
attachInterrupt(2, GoodFunRISING, RISING);
}
}
void GoodFunRISING()
{
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
// If interrupts come faster than 10ms, assume it's a bounce and ignore
if (interrupt_time - last_interrupt_time > 20)
{
//Good=Good; // dont want to ad to value.
// Button Goes to HIGH and Nothing should happen for 10mS Till the Switch settles down.
// then we prepare for the next Falling condition
last_interrupt_time = interrupt_time;
detachInterrupt(2);
attachInterrupt(2, GoodFun, FALLING);
}
}
void loop() {
Serial.print ("Good=");
Serial.print (Good);
Serial.print (" . ");
Serial.print ("Rejects=");
Serial.print (Rejects);
Serial.println (digitalRead (BUTTON_PIN));
}