Interrupt issue

Hello All

I am new to Arduinos but not to programming as such.

I have 2 Arduino UNOs and am basically trying to get one of them to work as a shift light.

I am using one arduino to create a pulse signal which the other one would read for testing purposes.

Code on first arduino is…

void setup() {                
  pinMode(13, OUTPUT);   
}

void loop() {
  digitalWrite(13, HIGH);
  delay(5);
  digitalWrite(13, LOW); 
  delay(5);  
}

On the other one I have this code…

unsigned long interval;
unsigned long lastcount=0;
void setup() {                
attachInterrupt(0, tachcount, LOW);  
}


void loop() 
{
Serial.begin(9600);
Serial.println(interval);
}

void tachcount()
{
  interval=millis()-lastcount;
  lastcount=interval;
}

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.

Any help on this would be appreciated.

I see two issues.

  1. 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.

  2. 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

Woops, my bad, goofed up on the serial.begin()

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.

void tachcount()

{

interval=millis()-lastcount;

lastcount=interval;

}

So now I see a third issue!

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:

void tachcount()

{

now = millis();

interval=now-lastcount;

lastcount=now;

}

I have updated the code to

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.

Well I don’t have an answer, but I do have a few experiments for you to run.

Inside the ISR, simply increment the value of “Interval” and see what you get.

Initialize the value of “Interval” to something like 123 at the top of the file and see what you get.

I suspect now that the ISR is not getting hit.

Found the issue, stupid me, didn’t realize both the arduinos had to share the digital ground.

Once I did that it started working like a charm.

Ground references are always important! Glad its working.