Fast pulse counting with Arduino interrupts and why nested priority really helps

  Рет қаралды 38,300

PaulStoffregen

PaulStoffregen

6 жыл бұрын

Forum thread: forum.pjrc.com/threads/48101-...
An earlier version of this video had a few rookie editing mistakes. I also put readable text in the max frequency test, since the function generator screen has low contrast white-on-blue text.

Пікірлер: 67
@floretan
@floretan 6 жыл бұрын
As you mentioned, it's normal for things to go wrong. There's more to learn from your explanations when things go wrong than from perfect video takes.
@2lefThumbs
@2lefThumbs 2 жыл бұрын
Brilliant, I shelved a teensy 3.2 interrupt-based project around the time you uploaded this (3 yrs ago), because of missed interrupts, but decided to revisit it this week I wish I'd seen this then! (I was also swamping the serial upload to my pc, also solved here🤷‍♂️)
@TravsterOR
@TravsterOR 6 жыл бұрын
Nice work Paul!
@jontscott
@jontscott 6 жыл бұрын
Very nice explanation and demonstration!
@randalnichols7377
@randalnichols7377 3 жыл бұрын
This is exactly what I needed, thank you!
@CameronCobb
@CameronCobb 5 жыл бұрын
Excellent video!
@josephnatal9419
@josephnatal9419 4 жыл бұрын
Very informative!
@jcjensenllc
@jcjensenllc 3 жыл бұрын
On these videos, please start by saying what board you are using. I finally saw it on the forum post but needed to look for that. Thanks for all you do. Much appreciated.
@gilbertosantos684
@gilbertosantos684 2 жыл бұрын
Thank you. Gilbert
@Argosh
@Argosh 6 жыл бұрын
Considering your square wave is closer to a sinewave maybe the counter correctly drops a few where the gatevoltage wasn't reached? Your teensy would be more forgiving.
@FishKungfu
@FishKungfu 6 жыл бұрын
Very cool!
@RinksRides
@RinksRides 4 жыл бұрын
your hitting the capacitive coupling on the breadboard, IIRC breadboards are good till about 10MHz due to stray capacitance on the fingers inside the breadboard (2-20 pF per pin pair)
@jasetitan2959
@jasetitan2959 3 жыл бұрын
pro tip: watch series on flixzone. Been using it for watching loads of movies lately.
@sorenkaden8615
@sorenkaden8615 3 жыл бұрын
@Jase Titan Yea, been watching on Flixzone} for since december myself :D
@tejeshwvardhan1165
@tejeshwvardhan1165 6 жыл бұрын
Max sampling speed of teensy 3.2???
@dioutoroo
@dioutoroo Жыл бұрын
Hi Paul! Thanks for your video and also your library. I have a question, so I am trying to figure out the frequency of my incremental encoder, which has 2 phases(A&B), with arduino UNO. Using the freqCount library then I have to hook one of the phase to the digital pin (6)? Is that right?
@LeckieInstallsLondon
@LeckieInstallsLondon Жыл бұрын
im thinking the capacitance of the cable going to the bk is likely the issue with missed counts at higher freq.
@lautaromarcoporleylopo8349
@lautaromarcoporleylopo8349 4 жыл бұрын
exactly... or we can use the integrated counter un the avr.
@scottgates601
@scottgates601 4 жыл бұрын
Would this work for external switches? For example if I have a toggle switch operating a motor (forward and revers directions) and have two limit switches to bound the distance the motor can move. Can I give the limit switches the highest priority so that the movement of the motor is halted when limit switches are closed ? Thanks!
@whitefields5595
@whitefields5595 3 жыл бұрын
I think vids where something didn’t work are far more useful! It calls into question if the freq counter is accurate...... I use the same line of argument with my wife
@rafaelbachmann3619
@rafaelbachmann3619 6 жыл бұрын
You delay a bit between the serial prints, that might account for some of the discrepancies?
@jerrymk6846
@jerrymk6846 3 жыл бұрын
no prior count will only happen when serial prints if new count changes within the delay(10) then prior count will be different from new count and it will be updated at the next loop cycle. basically 10 milliseconds after new interrupt the last new count value is guaranteed to be the correct value.
@toastrecon
@toastrecon 3 жыл бұрын
@@jerrymk6846 So, if the interrupts are coming in faster than the Serial.print function can be executed, that's when they stop being displayed in the console, correct? It'd still be counting in the background, but wouldn't be printing it out.
@jerrymk6846
@jerrymk6846 3 жыл бұрын
​@@toastrecon The counter is basically always up-to-date (because changing a variable doesn't take any significant time). The value will only stop getting printed when the previously printed value is the same as the current counter. The "previously printed value(prior count in the code)" is guaranteed to update when a serial print happen. If the counter got updated while serial print is running, the counter will be up-to-date, but the "previously printed value" will not be same as the counter, and then the value will be printed out within 10ms after. EDIT: Oh yeah I re-read the code and you are correct. If prior_count = new_count; line is moved to before Serial.printf then it will be less likely to happen. Serial print actually takes quite some time and may lose a lot of counts... Basically an oopsie happens when counts happen within Serial.print function
@danielconnors1121
@danielconnors1121 Жыл бұрын
I am trying to run a similar test with a MKR 1400. I have found the section of code in variants.cpp that shows the mapping between the physical pins on the MKR 1400 and the interrupt numbers. However I don't know how to set the priority for a specific hardware pin, #4, to priority 1 using the function NVIC_SetPriority( (IRQn_Type) int_num, int_prio). Pin #4 maps to PortB10 and extint 10. When I try NVIC_SetPriority( 10, 1) I get a compiler error: invalid conversion from 'int' to 'IRQn_Type {aka IRQn}. Am I missing some function call that gets me to the right type of interrupts? Thank you for your video. Dan
@50Hz
@50Hz 4 жыл бұрын
How you look up what pin is on porta b c etc?
@arvindaditya19
@arvindaditya19 4 жыл бұрын
hey i am trying to build a retrofitting for existing mechanical water meter can u help
@AnalysIR
@AnalysIR 6 жыл бұрын
Termination, reflections....coax vs jumper wires??
@RinksRides
@RinksRides 4 жыл бұрын
breadboards are only good to 10MHz from stray capacitance on the breadboard contacts (2-20 pF per contact)
@tinyboxxx
@tinyboxxx 6 жыл бұрын
Nice Video! Can I use it on Arduino Due? And can I have multiple frequency measure? Thanks in advance!
@PaulStoffregen
@PaulStoffregen 6 жыл бұрын
So far nobody has ported this code to the SAM chip used on Due. In theory it could be done, if someone familiar with that chip's timers puts a lot of work into porting the code. That chip also supports interrupt priorities, but Arduino's core lib and all the other libs for that chip don't make use of the priorities, so to get the interrupt priority part working would require a lot more coding work...
@jalalbukhari1149
@jalalbukhari1149 3 жыл бұрын
i want to use (Sensor generated ) interrupts as a counter for fast moving objects. like seed counter. but i need to know which sensor will be most useful to detect the small grain sized particle to trigger the sensor and cause interrupt.
@itselfembedded2260
@itselfembedded2260 3 жыл бұрын
salam, u can use beaglebone black in which inside there are 2 co-processors independently can be used ..16 interrupts almost..and linux support
@nithinjackson1453
@nithinjackson1453 4 жыл бұрын
can we make a counter like this fast without using software, and only using hardware? Is it possible
@valeriosalvo6567
@valeriosalvo6567 10 ай бұрын
sure, you can build a counter using logic gates! It's quite simple!
@50Hz
@50Hz 4 жыл бұрын
Has anyone tried two pulses at once..
@dozog
@dozog 6 жыл бұрын
Are the priorities of loop, serial etc set to a default ? What is the default and can they be changed arbitrarily?
@PaulStoffregen
@PaulStoffregen 6 жыл бұрын
Details are discussed on the forum thread linked from this video's description.
@dozog
@dozog 6 жыл бұрын
I am sorry, i cant find it in that relatively short link(single page). Maybe you want me to ask the specific question on the forum ? Maybe i can be a bit more specific. The loop and millis interrupt are not declared by me, they are probably done somewhere behind the screens by the IDE? If i want to make sure the interrupt that i define by: attachInterrupt( ...) goes before serial but after loop, what priorities are available to me? Or alternatively what is the syntax for changing the interrupt priorty of loop and millis ? I found default Serial interrupt is 64? USB is 112 ? systick is 0? Then what happens if i set pin_interrupt priority to 0 as well?
@PaulStoffregen
@PaulStoffregen 6 жыл бұрын
SysTick defaults to 32. When an interrupt runs, it blocks all others at the same or higher (lower number) priority. So if you have two interrupts at priority 0 (the highest you can set for normal interrupts), then whichever happens first gets serviced and the other waits until the first completes. The same is true for any pair at the same priority.
@althuelectronics5158
@althuelectronics5158 2 жыл бұрын
Nice your video. Good job. You please helping sir. I have a Mike one project but one problem. Am Arduino programing ( I'm just starting to learn) Unable to write program.?
@PaulStoffregen
@PaulStoffregen 2 жыл бұрын
KZbin comments aren't the place to get this sort of technical help. Forums are where to ask. If whatever board you're using has a forum, ask there. If you have a generic board which doesn't have a support forum, but you're using the Arduino software, ask on Arduino's forum. forum.arduino.cc/
@PaulStoffregen
@PaulStoffregen 2 жыл бұрын
Before you post a forum question, consider there are 3 factors to success when asking random people on the internet for technical help.
@PaulStoffregen
@PaulStoffregen 2 жыл бұрын
#1 - Make a good first impression. The main way is showing you are trying. People want to help others who make a sincere effort, even if that effort is just trying Google searches and not yet knowing the correct words.
@PaulStoffregen
@PaulStoffregen 2 жыл бұрын
#2 - Give context. Explain your goals or what you are trying to accomplish. People who understand your needs can answer much better than if you ask a narrowly focused or overly vague question without any explanation.
@PaulStoffregen
@PaulStoffregen 2 жыл бұрын
#3 - Share details. Say which software, hardware you're using and give screenshots, photos, exact code or error messages. Details matter for solving tech issues. People who see your question will be able to help only if you give enough info.
@pdxfunk
@pdxfunk 4 жыл бұрын
I tried this with an Arduino Uno and it didn't like the NVIC_SET_PRIOIRTY line.. Is this a Teensy-only thing?
@PaulStoffregen
@PaulStoffregen 4 жыл бұрын
Yes, NVIC_SET_PRIORITY is a Teensy only thing. Other 32 bit ARM chips have interrupt priority with their own ways, but Uno's interrupt hardware doesn't have priority capability, so there is no way to do this on Uno.
@pdxfunk
@pdxfunk 4 жыл бұрын
Thanks Paul! Just picked up a couple of 4.0s from you guys to play with :)
@BigHeartBreak93
@BigHeartBreak93 5 жыл бұрын
Here for the Ports on Arduino Mega 2560: harperjiangnew.blogspot.com/2013/05/arduino-port-manipulation-on-mega-2560.html
@lovermonkey
@lovermonkey 3 жыл бұрын
If I needed to do other things along side of counting, how would I go about doing that? (Other things = Display, SD card etc)
@PaulStoffregen
@PaulStoffregen 3 жыл бұрын
Short answer: those things are generally done from your main program code, not from an interrupt.
@PaulStoffregen
@PaulStoffregen 3 жыл бұрын
Longer answer: best to ask on the forum, where you can write a more detailed question which gives context to understand what you're really trying to accomplish with your project, and specific details like the hardware and code you're already using. Comments on social media sites like KZbin are good for only quick questions and simple answers. Forums are where you go for more in depth discussion of how to do a design a project and structure your code.
@lovermonkey
@lovermonkey 3 жыл бұрын
@@PaulStoffregen I have done that before. Normally don't get an answer
@PaulStoffregen
@PaulStoffregen 3 жыл бұрын
@@lovermonkey Well, there are 3 things to do when writing a forum question that greatly improve your chances. 1: make a good 1st inpression, 2: give context about what you're really trying to accomplish, and 3: share specific details so people can at least see and maybe even reproduce whatever technical issue is at hand.
@PaulStoffregen
@PaulStoffregen 3 жыл бұрын
The 2 ways to make a good first impression are 1: demonstrate you've made some effort (photos are best) and 2: appear to be genuinely interested in learning.
@althuelectronics5158
@althuelectronics5158 2 жыл бұрын
Hi
@bowmanzz1
@bowmanzz1 2 жыл бұрын
I count pulses to help go to sleep at night
@Korystuwatch42
@Korystuwatch42 Жыл бұрын
this is not ATMega!
@yalgret
@yalgret 6 жыл бұрын
That doesn't look like an arduino, is it a 32bit board? What MHz is.it?
@dozog
@dozog 6 жыл бұрын
johnDon. Or you watch the video.
@PaulStoffregen
@PaulStoffregen 6 жыл бұрын
It's Teensy 3.2 using ARM Cortex-M4 running at 96 MHz.
@tinyboxxx
@tinyboxxx 6 жыл бұрын
Can I use it on Arduino Due? And can I have multiple frequency measure? Thanks in advance!
@tejeshwvardhan1165
@tejeshwvardhan1165 6 жыл бұрын
Max sampling speed of teensy 3.2???
Arduino Interrupts Revealed
27:02
Lewis Loflin
Рет қаралды 50 М.
Мы никогда не были так напуганы!
00:15
Аришнев
Рет қаралды 6 МЛН
I Can't Believe We Did This...
00:38
Stokes Twins
Рет қаралды 108 МЛН
Red❤️+Green💚=
00:38
ISSEI / いっせい
Рет қаралды 64 МЛН
How Many Balloons Does It Take To Fly?
00:18
MrBeast
Рет қаралды 139 МЛН
Frequency Counter using Arduino Timers
6:27
Anas Kuzechie
Рет қаралды 10 М.
Using Arrays with Arduino
13:51
Programming Electronics Academy
Рет қаралды 28 М.
Understanding Arduino Interrupts | Hardware, Pin Change & Timer Interrupts
48:17
ESP32 Pulse Counter
14:41
John Lauer
Рет қаралды 22 М.
How to Use Arduino Interrupts The Easy Way
33:28
Rachel De Barros
Рет қаралды 78 М.
Reading PPM Receiver Signal with Arduino using Interrupts
8:32
Kamran Ahmad
Рет қаралды 26 М.
Arduino Tutorial: Tachometer (RPM Counter)
3:54
InterlinkKnight
Рет қаралды 271 М.
Level Up Your Arduino Code: External Interrupts
18:55
SparkFun Electronics
Рет қаралды 172 М.
451 Which Processor can kill the ESP32?
11:24
Andreas Spiess
Рет қаралды 340 М.
تجربة أغرب توصيلة شحن ضد القطع تماما
0:56
صدام العزي
Рет қаралды 53 МЛН
Здесь упор в процессор
18:02
Рома, Просто Рома
Рет қаралды 344 М.
Мой инст: denkiselef. Как забрать телефон через экран.
0:54
Klavye İle Trafik Işığını Yönetmek #shorts
0:18
Osman Kabadayı
Рет қаралды 3,9 МЛН