Arduino Event-Based Programming

preview_player
Показать описание
In this inaugural episode of the Tinkerer's Toolbox, I am showing a better way to program Arduinos using the Eventually library. This tutorial shows what is wrong with the traditional method of Arduino programming, and how Eventually can save you from the awful code-mess that plagues all but the simplest Arduino programs.
Рекомендации по теме
Комментарии
Автор

The classic blink code is for those who have no idea in programming or microcontrollers. It is simple enough to be easily understood and have them interested.

inspiredtiny
Автор

Just use interrupts. It's way simpler.
Your event loop thingy makes it way more complicated than it needs to be. The event loop is still polling over all possible events, so you have delays and inneficiencis and possible other timing issues. With interrupts it's 1 line to attach a function to a pin, so the function triggers when button is pressed. Bonus that it works on rising or falling edge, so much less issues with debouncing.

MGL
Автор

Being a newbie to programming, it's great to see different methods used, to arrive at a similar basic outcome. Thank you for your time and effort. Nice video mate.

fjonesjones
Автор

That's fantastic. Can't wait to use this library. I want to use a shift registers to scan through the states of various (numerous) inputs from a very few pins on the arduino. I hope eventually will help simplify this process and give my code the elbow room of a c++ environment.

dwdw
Автор

This may be the answer to my prayers. The one thing I hated about programming for the Arduino vs computer software was everything was stuck in a loop, so button events couldn't be accessed unless the button was pressed at the right time.

TraceguyRune
Автор

Awesome, I just completed a project that's an environment controller for plants, it measures temperature and humidity and switches on a fan, a heater, and lights according to different conditions.
I wanted to keep the user interface simple using four buttons and a 1602 display to display time and date aternating with temperature and humidity while no button is pressed, and then on a button press, to go through 13 different menus to set parameters.
It's been a real challenge from the point of view of updating the displays and reading and debouncing button presses all in the same loop.
Now I have downloaded your library I am planning a rewrite of my code using Eventually, I should now be able to incorporate some new features based on Eventually that will make the project super awesome, so a big thanks.

SusanAmberBruce
Автор

Well, that was a special treat! I have to admit that I only actually watched the first 90 seconds before opening a new tab and continuing my work. I continued to listen, however, and every so often I would check back in on your video out of curiosity thus inserting delay after delay in my normal workflow, and with each loop came an increase in variables I struggled to fill much less understand. On and on, over and over and over until finally, eyes glazed over, one eyelid slowly blinking, I exited abnormally.

RoySATX
Автор

I absolutely love this code. I'm building a wave maker for an aquarium using this setup and it's working fine. I wanted to add in another pump to run at different times so I created another time listener. The problem is the second time listener I created will not operate at a slower speed than the first operator for some reason. I'm going to post my code eventually but I'm having problems figuring out how to add more time listeners.

drumminjeff
Автор

I was a wizard with Basic and qBasic back when I was in college. That said, it was a long time ago.
Switching from Basic to C++ is actually not too difficult. The core control structures and alot of the strings/variables are very similar.
It's really awesome to see guys like this show better and more intuitive ways of coding for Arduino specifically. Clearly the OP knows C++ inside and out. This is making my transition to modern coding super fun and exciting for me.
Thank you for sharing your knowledge and experience sir. Much appreciated!!! Great video!

tbwpf
Автор

I think you could take your blink example one step further. replace blinklight() with a blinkOn() and blinkOff() listener, have them call each other, and set false so it only runs once. Then you don't have to keep that blinkState or have the "if" logic that is currently in blinklight()

leeaustex
Автор

I wrote something very similar, integrated with MQTT ; your version has given me food for thought. Well done.

favesongslist
Автор

I think would be great thing if you could make tutorial about creating your eventually library. This would be perfect introduction of concepts for event based programing in others languages like c#.

mj
Автор

What I am worrying about is that we basically ignore the very first buttonpress and the whole bunch of the bouncing transitions. We consider the button has been actually pressed only _after_ the bounces are over plus some extra delay to be sure. (So in the hypotetic case when the button contact is poor we could have a significant delay or even miss the button press completely!)
However at the very first transition (despite the bouncing that follows) it is _already_ pretty clear the button _has indeed_ been pressed. So we may want to react as soon as possible and only get rid of the _following_ jitter!
What I would do is to react immediately on the _first_ transition and then just ignore the following transitions for a certain period of time. Never thought of doing this with a state machine though. Just check for millis() inside the loop.

michroz
Автор

Good stuff.

Any 'bloat' from using libraries should be cleared out by the compiler, so criticisms about that are irrelevant - if people want the developer to watch every byte, then they should be using assembly language anyway!

I see there are a number of event libraries out there, but yours is the first I have looked at and your video shows you are clearly thinking about the right issues and explaining them, so I shall be giving it a whirl.

Thanks!

pperrinuk
Автор

Thank you for taking the time to show us the difference between this way and the "old way". For those of us just starting out it really helps.

TatsAndGraphics
Автор

I can see some problems with this scheme and that is the execution time of the listeners themselves and the eventual call of the function as a result of an event.
If the events are occurring at a rate that's discernable by eye (blinking led) then the execution time of the library routines will be negligible because the events are occurring relatively slow but if events occur very fast, say in the microseconds or milliseconds then the listeners may have trouble reacting in real time....So they're useful if one keeps that in mind.

btomas
Автор

if( millis() - previous_time >= interval ){ // declare previous time to 0 and interval as you wish in setup
previous_time = millis();
//your code here below
digitalWrite( 13, digitalRead( 13 ) ^ 1 ); // To toggle led.

}

//Use this instead

kishpawar
Автор

Can I like this multiple times?
God, clean code is beautiful.
I'm starting with Arduino for a personal project, and I was dreading the delay function. For someone used to OOP, this was painful.
And then YouTube suggests me this video...

nuno.picado
Автор

Also, you declared your unsigned int variable (curtime) inside your loop. Do this at very beginning of your code instead.

errrzarrr
Автор

Thanks, this lack of event programming is why I've never used that Arduino programming system before. Without events it remains a toy.
Do you have an example of how to create event type libraries that you would be willing to share?

etmax