Go Down

Topic: Using 2 attachInterrupt to read two data inputs (Read 691 times) previous topic - next topic

Bacanitus

Hello

First of all I want to congratulate this forum for a excellent job on helping other's. I've been reading some post and I see that everybody wants' to help.
I too have a little problem using attachInterrupt.
I want to create a code that would print the digital data coming from a Digimatic Indicator (IDS). The device isn't important. I've already succeeded printing the data from one device trough the serial pot using attachInterrupt function but know for two devices the problem is that the data coming from each one are all scrambled..
Both data sources have a clock signal and a data signal, i have to read the data signal when the clock signal is rising.  So i used a interrupt to sample the data from the data line. For just one device the code that I've used was the following:
Code: [Select]
int dadosA = 4;
// ClockA is digital pin 2 on the Duemilenove.
int clockA = 2;


void setup() {
 Serial.begin (9600);
 //Inputs
 pinMode (dadosA, INPUT);
 pinMode (clockA, INPUT);
// cicleA is a function which reads from digital pin 2 when triggered
 attachInterrupt(0, cicleA,RISING);

}

void loop(){
}

void cicleA(){
     if (digitalRead (dadosA) ==LOW){
     Serial.write("A");
     Serial.write("0");
     }
     else{
     Serial.write("A");
     Serial.write("1");
     }
}

It works fine and I don't have any problems..
For two devices the code I'm trying to implement is something like this:

Code: [Select]
int dadosA = 4;
int dadosB = 5;
// ClockA is digital pin 2 on the Duemilenove.
int clockA = 2;
// ClockB is digital pin 3 on the Duemilenove.
int clockB = 3;

void setup() {
 Serial.begin (9600);
 //Inputs
 pinMode (dadosA, INPUT);
 pinMode (clockA, INPUT);
 pinMode (dadosB, INPUT);
 pinMode (clockB, INPUT);
// cicleA is a function which reads from digital pin 2 when triggered
 attachInterrupt(0, cicleA,RISING);
// cicleB is a function which reads from digital pin 3 when triggered
 attachInterrupt(1, cicleB,RISING);
}

void loop(){
}

void cicleA(){
     if (digitalRead (dadosA) ==LOW){
     Serial.write("A");
     Serial.write("0");
     }
     else{
     Serial.write("A");
     Serial.write("1");
     }
}
void cicleB(){
     if (digitalRead (dadosB) ==LOW){
     Serial.write("B");
     Serial.write("0");
     }
     else{
     Serial.write("B");
     Serial.write("1");
     }
}


The data that I receive is all scramble and sometimes it doesn't prints all the bites  that it should.
I'm using Arduino Duemilanove ATMEGA328?
Thanks for any help?
Regards

kg4wsv

Pretty much every interrupt discussion you can find here will tell you "don't do serial I/O (Serial.write(), etc) inside your interrupt handler."

So, now it's your turn: don't do serial I/O inside your interrupt handler.

:)

-j

Bacanitus

thanks for the tip... I'm going to try another routine...

Go Up