Timer Interrupt and Serial conflict?

Hello everybody!
I am new to Arduino so I am sorry if I ask something it may be obvious to you.
I have an Arduino Due and I need to read a weight sensor at a fix cycle time (10ms).
I used an interrupt declared in the setup as you see below to call the ReadWeight function (that takes A2 and A3 pins to get the value. The library came with the sensor module.)
I also want to "feel" commands given on the serial (convert a string to a float for calibrating the weight).
The problem is that the serial seems not to work in this way (feel nothing), while if I give up to the interrupt and I put the call to ReadWeight function in the Loop section with a delay(1000) it works.
My question is: is there any "conflict" between the timer interrupt and the Serial.Available()-Serial.read() instructions?
Maybe 10ms is too short or I am doing something wrong, I don't know.
Please help me
Many thanks

void ReadWeight(){
float MyWeight=scale.getGram(); //function coming from an external module
}

String inString = "";

Setup section

Serial.begin(9600);
Timer3.attachInterrupt(ReadWeight);
Timer3.start(10000); // Calls every 10ms

Loop section

float MyValue;
char floatbuf[10];

while (Serial.available() > 0) {
int inChar = Serial.read();
if ((isDigit(inChar)) || (((char)inChar)=='.')) {
// convert the incoming byte to a char
// and add it to the string:
inString += (char)inChar;
}
if (inChar == '\r') {
Serial.print("value:");
inString.toCharArray(floatbuf,sizeof(floatbuf));
MyValue=atof(floatbuf);
Serial.println(MyValue);
inString = "";
}
}
// ReadWeight();
// delay(1000);

You can't use ANY interrupt driven functions from within an ISR, This includes Serial and analogRead().

Mark

thank you Mark.
Assuming that I must keep alive the timer interrupt in order to have high time accuracy (I really need it), how can I "interact" with Arduino if the Serial does not work? I was thinking to write a simple VB program as interface... Any suggestions?
Thank you

Do the minimum of what you must in the ISR.

Handle the Serial interface in main/loop; queue data to be send back from the ISR to the host and send them back in the loop, receive commands via the Serial interface and influence the ISR from there.

I ran into the same issue - I ended up writing to a ringbuffer in the ISR, and reading values out of it in the serial monitor.

Also, SerialUSB is probably much higher performance than Serial, and has less ISR jitter.

Do the minimum of what you must in the ISR.

-> my ISR is only reading the weight

Handle the Serial interface in main/loop; queue data to be send back from the ISR to the host and send them back in the loop, receive commands via the Serial interface and influence the ISR from there.

-> what do you mean with "influence the ISR from the loop"? the ISR should run on his way every 10ms... An example, please?

I ran into the same issue - I ended up writing to a ringbuffer in the ISR, and reading values out of it in the serial monitor.

-> in my application is fundamental to read the weight every 10ms. My problem is writing data to Arduino via serial. Is there any other "tool" rather than Serial objects?

Thank you

I have no problem using analogRead() in my ISRs on the Due. That's not interrupt-driven. That's not the problem.

Printing via Serial in the main/loop functions is not your problem, that's perfectly safe.

Scale.getGram is apparently doing something that is not interrupt-safe. Without knowing the library code, it's hard to debug, but my guess would be that it is writing to the Serial port? (Otherwise, your example sketch would not be doing anything with myWeight ...) That won't work, you need to rework the library.

Thank you I-mb for your comments/suggestions.
Actually the Scale.getgram function belongs to the library of the weighing module. It is based on the HX711 IC and it takes two inputs to read the weight. It does not call a analogRead instruction. The code assumed two analog inputs and I used it as it was. Right now I cannot post the code of the library.Sorry.
My problem is the malfunction of the reading of the weight and the reception of data on the serial only if I call the ReadWeight attached to a timer interrupt, doesn't matter the frequency.
I will make some change to the sketch and I will post it all to give you more information.
Thank you

Hello everybody!
Finally I can tell you what I found about my problem.
Actually it is not true that a Timer interrupt makes conflict with Serial communication.
The wrong weight reading was really generated by the interrupt but because the reading itself took too long to provide a value (average of many readings) and the timer interrupt was breaking the "reading process".
I had to go deep into the 3rd party library and to understand how it worked.
That's it.
Timer interrupts and Serial don't hurt each others, but be careful with the timing!! :slight_smile:

niuron:
Hello everybody!
Finally I can tell you what I found about my problem.
Actually it is not true that a Timer interrupt makes conflict with Serial communication.
The wrong weight reading was really generated by the interrupt but because the reading itself took too long to provide a value (average of many readings) and the timer interrupt was breaking the "reading process".
I had to go deep into the 3rd party library and to understand how it worked.
That's it.
Timer interrupts and Serial don't hurt each others, but be careful with the timing!! :slight_smile:

Hello Niuron, I am new to Arduino
maybe, i am on same problem with interrupt and serial USB comm conflict. this is like about losing data transfer in serial comm (maybe).
thanks a lot..
So, would you to share your knowledge about how to use NewSoftSerial lib (may simple sample code) and tech me how ad why ?
i need your help.. :slight_smile: