Vancouver, Canada
Offline
Jr. Member
Karma: 0
Posts: 90
|
 |
« on: October 10, 2012, 05:24:57 pm » |
Hi, I was wondering if this ISR is too much for processing power of Arduino? The interrupt is set at 1kHz ISR(TIMER1_COMPA_vect) { //Interrupt at freq of 1kHz distance = getDistance(Lsensor); pressure = getPressure(Psensor); prSwitch = getPswitch(Pswitch); temp = getTemperature(tmp); Fldtemp = getTemperature(Fluidtmp); Serial.print(drive); Serial.print(" "); Serial.print(distance); Serial.print(" "); Serial.print(pressure); Serial.print(" "); Serial.print(prSwitch); Serial.print(" "); Serial.print(temp); Serial.print(" "); Serial.println(Fldtemp); }
And here are the function calls in the ISR: int getPressure(const int pin) { float ADCpressure = analogRead(pin); ADCpressure = (ADCpressure/1023)*25; return (int) ADCpressure; }
int getPswitch(const int pin) { if(digitalRead(pin) == HIGH) { return 1; } if(digitalRead(pin) == LOW) { return 0; } }
int getTemperature(const int pin) { float ADCtemperature = analogRead(pin); ADCtemperature = ADCtemperature/1.023; return (int) ADCtemperature; }
int getDistance(const int pin) { return analogRead(pin); } Any ideas on how to make this work?
|
|
|
|
« Last Edit: October 10, 2012, 05:27:50 pm by FardinB »
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14091
Lua rocks!
|
 |
« Reply #1 on: October 10, 2012, 05:26:17 pm » |
It's nothing to do with processing power. Don't do Serial.print inside an ISR. http://www.gammon.com.au/interrupts
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14091
Lua rocks!
|
 |
« Reply #2 on: October 10, 2012, 05:26:59 pm » |
Just set a flag, test it in loop, and do your readings and displays there.
|
|
|
|
|
Logged
|
|
|
|
|
Vancouver, Canada
Offline
Jr. Member
Karma: 0
Posts: 90
|
 |
« Reply #3 on: October 10, 2012, 05:29:56 pm » |
I need to send these data every millisecond to the PC.
Any help is appreciated
Thanks
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14091
Lua rocks!
|
 |
« Reply #4 on: October 10, 2012, 05:34:04 pm » |
At what baud rate?
|
|
|
|
|
Logged
|
|
|
|
|
Vancouver, Canada
Offline
Jr. Member
Karma: 0
Posts: 90
|
 |
« Reply #5 on: October 10, 2012, 05:35:59 pm » |
I am using the max: 115200
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 7
Posts: 419
|
 |
« Reply #6 on: October 10, 2012, 05:45:01 pm » |
Haven't you done the math yet?
115200 baud = 115200 bits per second = 11520 bytes per second = 11.5 bytes per millisecond
You can't send that many characters in the 1 mS time frame, it has nothing to do with the "processing power" of the Arduino, but the baud rate.
|
|
|
|
|
Logged
|
|
|
|
|
Vancouver, Canada
Offline
Jr. Member
Karma: 0
Posts: 90
|
 |
« Reply #7 on: October 10, 2012, 05:49:30 pm » |
I am sorry i did not see the last reply you posted. Is there a way to increase the baud rate then. increase it in a way that PC can receive it?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14091
Lua rocks!
|
 |
« Reply #8 on: October 10, 2012, 05:53:27 pm » |
You are getting close to the maximum. How about not sending the spaces? Even then you will be pushing it to condense your data into 11 bytes.
A more reasonable approach might be to do some calculations and send an average every second, or a delta (eg. rather than the temperature, the change in temperature).
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 7
Posts: 419
|
 |
« Reply #9 on: October 10, 2012, 06:02:02 pm » |
You might have to send it as binary instead of characters. That way, for example, 0xFF saves on one character over 255.
How long do all your readings take? that might be more than a millisecond right there!
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 16
Posts: 1036
Arduino rocks
|
 |
« Reply #10 on: October 10, 2012, 06:03:44 pm » |
I managed to get 460800 baud with the uno. Above that, I would just see garbage.
You could also use Serial.write rather than print so you just print 2 bytes rather than somewhere between one and six (and you'd save another because you wouldn't need a deliminator). Then you'd have to write a program on your computer to interpret it, though
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 7
Posts: 419
|
 |
« Reply #11 on: October 10, 2012, 06:06:59 pm » |
According to this: http://www.arduino.cc/en/Reference/AnalogReadAn analog read takes .1 mS, so your three Analog reads take 30% of your 1 mS anyway.
|
|
|
|
|
Logged
|
|
|
|
|
Vancouver, Canada
Offline
Jr. Member
Karma: 0
Posts: 90
|
 |
« Reply #12 on: October 10, 2012, 06:29:11 pm » |
I optimized my analogRead to get the maximum speed out of it. This page (near the bottom) shows you how to get 77 kHz reading speed out of analog read. So the analog read times is not a concern to me. https://sites.google.com/site/measuringstuff/the-arduino@WizenedEE How did you manage to read that speed on the serial monitor of PC?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 16
Posts: 1036
Arduino rocks
|
 |
« Reply #13 on: October 10, 2012, 06:38:36 pm » |
@WizenedEE How did you manage to read that speed on the serial monitor of PC?
screen /dev/ttyACM0 460800
I run GNU/linux
|
|
|
|
|
Logged
|
|
|
|
|
Vancouver, Canada
Offline
Jr. Member
Karma: 0
Posts: 90
|
 |
« Reply #14 on: October 10, 2012, 06:46:00 pm » |
How about this code for ISR? would it run faster? ISR(TIMER1_COMPA_vect) { //Interrupt at freq of 250Hz distance = getDistance(Lsensor); pressure = getPressure(Psensor); prSwitch = getPswitch(Pswitch); temp = getTemperature(tmp); Fldtemp = getTemperature(Fluidtmp); toprint = 0; //reset buffer toprint += drive; toprint += ' '; toprint += distance; toprint += ' '; toprint += pressure; toprint += ' '; toprint += prSwitch; toprint += ' '; toprint += temp; toprint += ' '; toprint += Fldtemp; Serial.println(toprint); }
|
|
|
|
|
Logged
|
|
|
|
|
|