4 ms·
> When I told my Arduino to sleep for 51 microseconds, it was generally sleeping for either 56.2 or 61.3 microseconds. That seemed to easily be bad enough to ca
by marcan_42 5y ago
> When I told my Arduino to sleep for 51 microseconds, it was generally sleeping for either 56.2 or 61.3 microseconds. That seemed to easily be bad enough to cause lots of problems.
This is why I've never quite liked the Arduino ecosystem, the way it exists. Sure, it works and it's accessible to newcomers... but it doesn't encourage understanding the inner workings of the platform enough, which is something that is extremely valuable and accessible when you're working with simple 8-bit microcontrollers. Learning about hardware registers and clocks and cycle accuracy really isn't that scary, that's the point of using a tiny (and frankly obsolete) 8-bit platform!
But when you give people an API that takes an integer number of microseconds, and then approximates it, people are going to think they can bit bang an async serial protocol in C using it, and they will be very confused when it doesn't work (as happened here), and you end up in cargo cultish land of trying to work with bad data instead of developing an actual understanding of the hardware you're working with.
I've seen so many people thinking they need to design their bespoke board around an Arduino shield connector because the thought of just throwing on the ATMega directly scares them, even though the thing only needs 5V, GND, and crystal (if that), because what would they do outside the comfortable ecosystem? And it just makes me sad. If you're at the point you're designing your own semi complex boards and you haven't graduated from Arduino yet, you're not letting yourself grow.
(Or maybe I'm just an old millennial and I think these zoomers should be learning microcontrollers by programming in assembly like I did when I was 10 and using the PIClist delay routine generator and counting my cycles before Arduino existed, now get off my lawn? You decide :-) )
- spenczar5 5y agoRight, it's complicated, isn't it? Like, I couldn't (or at least wouldn't) have gotten started on this project if I had to read the ATMega reference manuals from the very first step. But I did feel very frustrated that the Arduino "delayMicroseconds" function is so far off from accurate, and I felt frustrated also whenever I looked for deeper explanations of almost anything. It's a very copy-and-paste culture. I eventually did find programming things more directly to be quite rewarding and worthwhile. My code is still in a ".ino" file, and uses some Arduino stuff, but it's basically built around ISRs in some frankencode middle state. There's a lot of room for someone to create good tutorials and material for ramping up and out of Arduino. Would have helped me a lot, at least.
- marcan_42 5y agoI wish the Arduino IDE actually tried to be educational instead of just being a dumbed down wrapper around avr-gcc with automatic #includes. There are so many things that could be done... stuff like showing side by side disassembly of functions, with register names displayed symbolically. Or even just basic IDE stuff, like built in contextual function documentation. And then it should have an "export self-contained project" feature that gives you a directory containing a Makefile and all the dependencies to build your .ino, as source code form (and none of the ones you don't use), so you can easily see exactly what is getting built under the hood and can use that as a bridge to working outside the ecosystem. At the scale of 8-bit micros there's no reason not to work with copies of your dependencies, and it's very educational seeing everything in one place (and being able to hack on it) instead of having it scattered in a bunch of global package paths. The frameworks and libraries are useful (if opaque and quirky and underdocumented), but the IDE is just so underwhelming... heck, it doesn't even manage to be a decent text editor. Getting started with Arduino makes total sense, nobody's expecting newcomers to start off with the IC datasheet... but they should be able to eventually read the parts they need and work with it directly, instead of relying only on software abstractions. That's the beauty of these systems.
- dasv 5y agoI have found PlatformIO to be a good step up from the Arduino IDE: it generates a new environment for each project with a sane folder structure, and it is possible to dive into the implementation of any function just with the usual Ctrl+click. It helped me a lot to understand how the Arduino libraries worked under the hood and to make changes in them when I needed it.
- ms-fellag 5y ago>There's a lot of room for someone to create good tutorials and material for ramping up and out of Arduino. Would have helped me a lot, at least. Good news they do exist (at least if i understood your statement correctly), You just need to search with the keywords "AVR", "atmega328", or whichever version you intend to use instead of "Arduino". I attempted to make a simple PLC this summer and these resources were a great help : [book] Make: AVR Programming https://www.amazon.com/AVR-Programming-Learning-Software-Technology/dp/1449355781 https://www.amazon.com/AVR-Programming-Learning-Software-Tec... and this complementary (or introductory) video to the book by the author i believe https://www.youtube.com/watch?v=ERY7d7W-6nA https://www.youtube.com/watch?v=ERY7d7W-6nA [Youtube-videos] A playlist of Cornell's ECE 4760 AVR Lectures by the very chill "Bruce Land" (from 2012) : https://www.youtube.com/playlist?list=PLD7F7ED1F3505D8D5 https://www.youtube.com/playlist?list=PLD7F7ED1F3505D8D5 The course website (labs, exercises and readings) : https://people.ece.cornell.edu/land/courses/ece4760/index_2015_atmega1284.html https://people.ece.cornell.edu/land/courses/ece4760/index_20... Homeworks : https://people.ece.cornell.edu/land/courses/ece4760/homework/oldhomework.html https://people.ece.cornell.edu/land/courses/ece4760/homework... Labs : https://people.ece.cornell.edu/land/courses/ece4760/labs/oldlabs.html https://people.ece.cornell.edu/land/courses/ece4760/labs/old... Although the course was centered around an atmega16 or something? i really don't recall sorry but the knowledge i gained was invaluable and easily transferable to a 328p. Fundamentals of Microcontrollers - Arduino bare-metal breakdown (A playlist of a guy "Mitch Davis" exploring an atmega328 barebone and using tools like AVRdude etc..) https://www.youtube.com/playlist?list=PLNyfXcjhOAwOF-7S-ZoW2wuQ6Y-4hfjMR https://www.youtube.com/playlist?list=PLNyfXcjhOAwOF-7S-ZoW2... Atmel Programming Tutorial (by Chris Dahms ) https://www.youtube.com/playlist?list=PLoLaqVexEviMZu55Y4JO66b4xIv5K-D1L https://www.youtube.com/playlist?list=PLoLaqVexEviMZu55Y4JO6... Hope this helps.
- raverbashing 5y agoAah trying to bitbang with a Sleep? I agree, it's a case of "get off my lawn and go back to node.js" :) Or, more accordingly, don't think the computer is magical. Edit: it's a bit funny seeing someone trying to discover things by themselves but saying "it's difficult to measure pulse width with an oscilloscope" just triggered me - pun intended
- marcan_42 5y agoBitbanging like this is fine for synchronous protocols... but if you're trying to work with an async one like this one, there's just no safe way to do it in C unless your have a lot of leeway in your timing. Even if you get it to work today, there's no telling how a compiler update might change the instruction generation enough to change the timing and screw it up. I know it's cliché to tell people to go code in assembler but... this is where you do it, and why you do it. And it's easy. Seriously. These things are so simple it is not hard at all to learn how to code for them in asm. And it opens up a whole world of cool timing hacks you just can't do in C. I haven't done much microcontroller stuff recently, but here's a thing I wrote for an ATtiny a while ago. This kind of tight timing bit banging hack is just not possible to do reliably in C. https://github.com/marcan/sigmafix https://github.com/marcan/sigmafix
- spenczar5 5y agoI would absolutely love to write assembly for the most time-sensitive parts, while keeping C for the parts that act as a USB HID library interfacing with the host computer. It really does feel like it would be so much simpler to be able to drop down into a little assembly sequence for reading back the response from the keyboard. But - to your point about Arduino's gaps - I have just no idea whatsoever how to build something like that. Not so much how to write the assembly - I've written a bit, and I'm sure I could work that out. No, I mean literally how I link and assemble the final program and get it to work on the chip. I don't know whether that's just an Arduino problem, either. I find this stuff hard to google, but maybe I'm using the wrong terms - these are the perils of learning as you go.
- 5y ago
- PostThisTooFast 5y ago
- blutack 5y agoWhat frustrates me about arduino is how limited the peripherals are. I can see why they chose to begin with that obsolete processor - the arduino model of programming is simple & approachable precisely because the processor is so dumb (no complex interrupt controller, no DMA, no complex power gating and clocking setups). For reliable embedded software I try and get the peripherals of the microcontroller to do all the timing critical work and my code sits up at a high level handling complete messages etc. Kind of the equivalent of "not blocking the event loop" in node, or functional vs imperative programming. But the arduino libraries etc don't really fit well with this style, because they have to accommodate lowest common denominator chips like the ATMega. At least it's not PIC 8s I suppose... For getting into embedded, I would strongly recommend the Teensy series (the 4.1 is joyful overkill for virtually any project). They are far more functional processors which are supported very well by the arduino ecosystem and have their own excellent set of libraries maintained by Paul which make full use of the hardware in the MCU. Being ARM based means that on chip debugging is relatively painless if it comes to that.
- marcan_42 5y agoThe beauty of using such a simple MCU is that it is very easy to learn how to program it directly... ... and yet Arduino isn't actually teaching people how to do that :( I'm a staunch proponent of using up to date MCUs, which means ARM these days, but I'd be completely on board with still using an ATMega as the default in 2022 for this kind of educational purpose if the point were actually being educational. At least it's not a PIC16F84, I suppose. All the microcontroller books spent a decade teaching literally the lowest end, crappiest, and more expensive PIC in the series because it was the first model with Flash memory, and ignoring everything that came after it. Even those that were backwards compatible for all intents and purposes, and not any harder to incrementally learn.