I need to use YUN as a pulse counter.
I noticed a strange behavior of interrupts INT0 and INT1 of Arduino YUN (and I think also Arduino Leonardo) compared to an Arduino UNO .
I've done following tests :
- UNO receives on pin 2 (INT0) and pin 3 (INT1) pulses transmit from YUN pin 12 and 13. The YUN pin 12 sends about 226 pulses every 5 seconds, the pin 13 about 43 pulses every 5 seconds. The UNO card correctly reads the number of pulses received , 226 on pin 2 (INT0) and 43 on pin 3 (INT1)
The YUN serial output is:
SEND pin 12 = 226 - pin 13 = 43
The ONE serial output is:
RECEIVE INT0 = 226 - INT1 = 43
- YUN receives on pin 3 (INT0) and pin 2 (INT1) pulses from UNO pin 12 and 13. The UNO pin 12 sends about 226 pulses every 5 seconds, the pin 13 about 43 every 5 seconds. The YUN correctly reads the number of pulses received on pin 3 (INT0), but on pin 2 (INT1) YUN seems to read 43 pulses from UNO pin 13 of the UNO card plus half of 226 pulses from UNO pin 12!
The ONE serial output is:
SEND pin 12 = 226 - pin 13 = 43
The YUN serial output is:
RECEIVE INT0 = 226 - INT1 = 155
3)Same of test 2, but the ONE pin 12 and pin 13 both transmit about 226 pulses every 5 seconds. YUN correctly reads the number of pulses received.
The YUN serial output is:
RECEIVE INT0 = 226 - INT1 = 226
- Same of test 2 using YUN INT4 on pin 7 instead of INT1. YUN correctly reads the number of pulses received.
The YUN serial output is:
RECEIVE INT0 = 226 - INT4 = 43
Actually i'm not skilled with the whole Arduino interrupts thing. How can I use YUN interrupts without those problems?
This is sketch i used to send pulses
//send
long mills12 = 0;// will store last time pin 12 was updated
long mills13 = 0;// will store last time pin 13 was updated
long period12 = 10; //pulse period in milliseconds on pin 12
long period13 = 57; //pulse period on pin 13
long count12 = 0;
long count12p = 0;
long count13 = 0;
long count13p = 0;
unsigned long curtime = 0;
long timez = 0;// will store last time send count out
long period = 5000;// every 5 seconds out count value
void setup()
{
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
curtime= millis();
if(curtime - mills12 > period12) {
digitalWrite(12, !digitalRead(12));
mills12 = millis();
count12++;
}
if(curtime - mills13 > period13) {
digitalWrite(13, !digitalRead(13));
mills13 = millis();
count13++;
}
if(curtime - timez > period) {
Serial.print("SEND pin 12 = ");
Serial.print((count12 - count12p)/2);
Serial.print(" - pin 13 = ");
Serial.println((count13 - count13p)/2);
timez = millis();
count12p = count12;
count13p = count13;
}
}
This is sketch i used to read pulses
//receive
volatile long countINT1 = 0;
volatile long countINT1p = 0;
volatile long countINT0 = 0;
volatile long countINT0p = 0;
int intINT0 = 0;
int intINT1 = 0;
unsigned long curtime = 0;
long timez = 0;// will store last time send count out
long period = 5000;// every 5 seconds out count value
void setup()
{
Serial.begin(9600);
attachInterrupt(0, addcountINT0, FALLING);
attachInterrupt(1, addcountINT1, FALLING);
}
void loop()
{
curtime = millis();
if(curtime - timez > period) {
intINT0 = countINT0 - countINT0p;
intINT1 = countINT1 - countINT1p;
Serial.print("RECEIVE INT0 = ");
Serial.print(intINT0);
Serial.print(" - INT1 = ");
Serial.println(intINT1);
timez = millis();
countINT0p = countINT0;
countINT1p = countINT1;
}
}
void addcountINT0() {
countINT0++;
}
void addcountINT1() {
countINT1++;
}