I have connected the pin 13 of the first arduino to pin 2 of the second arduino.
I am expecting a constant reading at the serial port, because if the pulse is at a constant time delay then it should basically read 2 times the delay value.
Instead what I do see on the serial port is an ever increasing number, but it does not increase in sequence.
You are re-openeing the serial port for each loop. This takes time. Consider looking at a few example scketches on the use of the initialization routine.
You have a low pulse about every 10 miiliseconds. At 9600 baud it takes just about 1 millisecond to send out a character. If your printed strings are greater than 10 characters you will start to fall behind. Add to that the fact you are wasting time with the serial.begin and the overhead in converting a number to a string, and you are likely taking much longer than 10 ms to print out. I would expect something like 2, 4, 5, 7, 9, 13, … not 1,2,3,4,5
Oddly, the values being returned at the serial port were updating so fast that I couldn’t even read them.
Also I played around with different values of delay for the first board, to as much as 1000 ms between high and low states. So that gives 2000 ms between 2 low states, this should be returned at 9600 baud pretty easily, but I still get the same behavior.
I am wondering that I am getting the super fast increasing values because of the initialization of serial.begin in the main loop.
I am expecting a constant value back on the serial port monitor because it would be the time delay between the 2 low values, is this assumption correct?
Also, yes the serial port data does look like what you pasted, I dont have access to that right now, but can provide it in sometime if that would help with the troubleshooting.
The millis() function gives you the current time since the processor booted up. You are subtracting an interval from it not a time. your code should likely be something like:
volatile unsigned long interval,now;
volatile unsigned long lastcount=0;
void setup() {
Serial.begin(115200);
attachInterrupt(0, tachcount, RISING);
}
void loop()
{
Serial.println(interval);
}
void tachcount()
{
now = millis();
interval=now-lastcount;
lastcount=now;
}
And I increased the delay on the other arduino to 10 ms so that there is ample time before the pulse arrives at the one counting it.
And now all I see is a “0” being returned on the serial port.
I am really perplexed as to what is happening and why the code just doesn’t want to return the right value for interval.
The arduino reference pages say that one should declare every variable that is being operated within the function being called in the interrupt be declared as a volatile, I have even done that.