I have the following really simple code. On every iteration it'll read the first pin, and if it's high - do some serial IO. Then do the same with the second pin. Nice and simple, works during my tests.
My concern is that (in production), the digital pulse I'm receiving (and trying to mimic during testing) would be too quick for the arduino to read. So, for example, the race condition could be:
- First pin is low Read first pin - result is low.
- First pin is now high Read second pin - result is low.
- Fist pin now low again Read first pin - result is low again (missed the 'high')
How much of an 'edge case' is this? Am I worrying about something that's likely to be a non-issue here? In production, each single pulse would represent a significant amount of product (I multiply it elsewhere) - so even skipping one could throw my data off.
boolean count1High = false;
boolean count2High = false;
int count1 = 7;
int count2 = 8;
int a = 1;
void setup() {
pinMode(count1, INPUT);
pinMode(count2, INPUT);
Serial.begin(9600);
}
void loop()
{
if (digitalRead(count1) == HIGH)
{
if (!count1High)
{
count1High = true;
a++;
Serial.println(a);
}
}
else
{
count1High = false;
}
if (digitalRead(count2) == HIGH)
{
if (!count2High)
{
count2High = true;
a++;
Serial.println(a);
}
}
else
{
count2High = false;
}
}
EDIT:
I know best approach here might be to attach an interrupt but I've been advised to avoid serial IO from within interrupts, I don't see an alternative here.