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.
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.
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.
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.
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.
Surely there is nothing stopping you using sub-routines (functions) which are outside Loop? The "main" part of any C program is a loop isn't it? It calls subroutines when things happen.
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.
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.
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!
veterens will eventually not use this, and go to the standard way of writing code. So for professional products a coder will either use freeRtos or the standard baremetal programming.
The problem is he didn't dive into the code in the library. That's what I was hoping to see. Maybe he has another video explaining the internals of the library. I think that would make this much easier to understand. It's also not clear if interrupts are being used or just polling. I'm not too familiar with Arduino programming but I would think the downloaded libraries are the source code and not just a pre-built image. If so, you can pull it down and see what's going on under the hood.
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.
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()
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.
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.
The primary one is that it merges different types of events (time, pins, and complex events) into a single unified framework that is more intuitive than the standard one. The specific help that you would get compared to an interrupt is automatic debouncing.
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#.
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!
That is the correct way to use it. I use the same technique to do event functions, the advantage is I can change my mind and interrupt the event before its timing finish. Delay puts machine in freeze stage where nothing can be interpreted while event reading is mostly cloak speed and loop dependent.
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 RUclips suggests me this video...
Using header files and APIs might make YOUR coding "easier" but you lose exact control, increase bloat, slow execution time, and need to learn a one-job set of functions useless in your next project. Start learning as low-level as you can so you know the foundation of what you are building. BTW; in the "old way" we calculated RC time constants to create astable multivibrators using a battery and a few discrete components. We also learned not to make it look like a rat's nest. Neatness counts.
Sorry about the question but I’m completely new to programming, in your eventually demo, you tell it to blink but where in the code is the time which you define the blink timing?
I don't know if the thread here is still active... I've been having some issues trying to fire an event right after another event, basically the act of turning on an LED, wait for a moment, then turn it off and turn on another LED of another color. Here's the catch tho, the time where both LEDs are on is different, so it should be two different events right? well, i tried that and what I end up is with two events at the same time. You could say "well use delay()" without realizing that delay will literally freeze the program for a given time and not allow the interruption of the event during the delay. So how can I make two events where one depends on the other to end in order to trigger?
This is so much better than the way arduino uses code, man ive programmed in C and C++ VB , scripting languages and having only a main and a loop function sucks, what about making your own, what if i dont want something looped everytime. Im glad you wrote this library, keep up the grat work
Please help me ::::: I want to start my loop only when my Ultrasound sensor detects the object and if the object is not present then it should directly stop all the operations until the ultrasound sensor detect another (different or same) object
Hello, I'm new to arduino and I find your library really a life-saver, but I would really like to be able to delete a single listener instead of resetting the entire context (all the listeners), how can I do it?
May I link your video in the description of a my recent video where I show a event-driven (and RPC) framework originally designed for devices that are slightly different from Arduino? I think your example at the beginning is useful to understand how messy can become programs in sequential execution. By the way, interesting library the one you have made. It seems that your listeners are related to fixed features such as time and input changes, do I have understood correctly?
Hi, there's a bug in this library. Adding "false" to create a single, no-repeating function call doesn't work. It calls repeatedly (very quickly). Can you please provide some advice on executing a single function call that's delayed? I'm using eventually in a larger project, and have also tested and confirmed this issue by altering the simple blink example included with the library. Thank you.
Hi. I'm a beginner to arduino an still learning I'm watching videos here on RUclips. But I had a question, if you could help me out on a code/ Sketch I am trying to put together a sketch for a Crossing bell for a Model railroad.
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
Thanks a very good explanation for those asking why the not been written before this for make since as the writer explained in line what he is doing and how to think So this is mor about how to develop the code Thanks a lot
I like the idea, but the non-event code when completed (minus debounce and not very well optimized) was 3% of storage, but the just the setup for the event code was 5%... without actually doing anything.
When you are close to zero (3%) it's easy to jump to 2% more, adding antibouncing that would likely take 200 bytes once compiled will bring it to 4%. Even-driven programs have to pay for the burden of the underlying event handling system, but they benefit when the programs become more complex.
Does anyone know how to program NeoPixel? My daughter is working on a project and this is our first time. Would appreciate if someone knowledgeable could talk with us for a few minutes to help get us started. Many thanks in advance!
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?
You miss the point the BLINK program is a starter program to help get new users to get using the Arduino . I am nearly 60 years old, my early programming was in machine code. I found the blink program useful to get started with the Uno...
I actually love blink myself and teach it to my students. The problem is that there is nothing in Arduino that helps you move past blink. People see the simplicity of blink, and they are lured in to thinking that it will all be that simple. However, once they get to the point where you have to have counters and delays and debouncing, then a lot of people just give up. This isn't the "easy-as-blink" system they thought it was. The goal of the library is to make *regulare* Arduino programming more simple. Blink is used as a counterpoint just to show where the limitations of it are, not that people shouldn't use it as a starting program.
@@BPLearningTV I agree with what you are saying here. My experience is in Javascript and your video caught my attention as I (as an Arduino newby) was thinking myself that event handlers would be very helpful. I've written some jQuery event handlers myself for some projects and they can be extremely powerful. The basis of what you've done here is great, although I think there is always room for improvement. I would like to see something more object oriented in a future release. Imagine what you could do passing objects to event listeners.
This was cool Is there anyway to define some variables and then change there value using Ethernet Basically I want to assign a few preset variables and then be able to change them over the internet. Think of a lock code, or codes.... Event driven would be good as there could be locks in use while the variables not in use are changed..... Any thoughts? Thanks Rob
Hi i m nahid hasan. Recently i buy a IOT GA6-B mini Gsm module. I face a problem Ardiuno R3 program is Upload but GSM AT Command is Not Response...I don't know Why .....Please Help me to solve this problem....
Brilliant! I've been losing sleep over how to implement this! And I'm glad you took your time explaining it: some of us have brains that have a lower clock cycle than they used to!
I am using EvtPinListener with a PULL_UP button. I'm not sure if that is a cause, but the event only actives when I let up on the button. How can I keep the event going as long as I have the button pressed?
I gave up on the whole event thing. I put everything back into a loop and did my own debounce checks. The current project I'm working on does fine in the loop. Plus, your program was causing my Arduino to crash. It hasn't crashed and is working a lot more efficiently in the plain old loop.
Event generators are going to eat more clock cycles, continuously, while polling events, that will be bottle neck in performance critical application, in Arduino.
thanks for sharing this ill be looking at implementing it into my project. is there any documentation for this library floating around anywhere i would like to look more into it as i plan on using it as a transmitter/Reciever for an ESK8 Board.
I have 2 questions; 1. How do you write a program to create a double click function on the button to have the motor turn a certain amount of degrees, hold it as long as you hold down the button, than rewind back the exact way when you release it? 2. Could and how do you program/solder that circuit into smaller arduino models?
can anyone help me in controlling a motor that is for a specific time i want it to turn on and also taking some inputs from sensors to turn it on but specified time period will not affect when it is on due to a sensor output but after sensor is done the specified time period should start whee it ended . Regards
This program is not completely good, can you show me how to turn off on the LED instantly at the time you click the button? According to this sketch, sometimes I press the button, the LED stop blinking but still on the HIGH state, which is a little annoying. Thank you!
No, it's correct like he wrote it. Try it like ur saying to see why it wont work :) This guy wrote an event library with classes and casts and function pointers.. do you think he'd make that mistake? 😄
@@ParabolicLabs Bingo. "Look, my code is shorter!" and less efficient. This video is all about using as few lines as possible, not efficient (or clear) programming. I'm surprised they didn't put more into libraries to make it even shorter.
You made it a little more complex. It's actually easier then that. But nice to see it a different way. Yes event programming is great. I guess I'll eventually use your method someday. 😆
Hi, Thanks for the great tutorial. I am new to Arduino and as well to C++ code, but after spending about a month watchin tutorials I believe I can learn this fast. Now the reason I comment is not only to say thank you but also want to ask a favor if even possible. I work for a Laser (Optic) company An I have built electronic mechanical stations to check laser stability and save data to an excel file, well I have been thinking about doing the same with Arduino and some codes all I need to do is to check 3 monitors from the laser (voltages in the scale of 0.5VDC to 4VDC) in the 3 monitors I need to set alarms based on the Voltage, e. g (V1 is set a max of 2.2V) if the voltage exceed this number I want to trigger an alarm or else do nothing, when an alarm is triggered I need to be able to identify which event caused the alarm by a code or by turning an led which will be labeled accordingly and so on, same for the other two monitors. all this will also turn a pin off which turns off a relay, the relay will turn the laser of by interrupting a signal, after 1 second will turn a second relay off to turn the housekeeping supply and a third relay will be interrupted the same way to cut main power to the laser. I have pretty much everything documented, so if you or somebody in the group could give me a hand it would be greatly appreciated. Thanks
Some constructive criticism. It took 30 minutes to explain something that could have been explained in 5 minutes if you had written your examples prior to making the video and just loading them in side by side windows. Then you could explain why one is not as good as the other and dispense with all the typing in real time.
Understandable, but typing in real time slows it down for people who are trying to follow. That is, if you are new, and trying to follow a new comment, seeing it typed out can pace your mind to help you follow the concepts better.
I don't understand the logic please. After you added Wasbuttonpushed. I don't understand how the button works, if it stays high after push or goes low after your hand releases. Plz explain
If it stays high after push, setting Wasbuttonpushed to 1 should stop it from entering the else part again and because the button stays high, Wasbuttonpushed cannot be 0 gain hence no more switching
i wonder if there are a time spent on addition for if its a loop then if you add 1 a variable then something would check if it have meet for example 1000 then turing it off reset then turn it on and between the loop it have a checker if it have a input .... im sorry im new to this i haven't even have a arduino before hahaha
Hi, Sir can you help me I have created a program that controls the motion of the stepper motor in which i have used the L298 driver , but now I'm stuck at one point , I'm not able to apply anticlockwise motion via push push button (PB IS ACTUALLY LIKE ELECTRIC CONTUNITY PATH (please suggest me how i apply if command)...... Actually it is a robotic machine where if electric continuity is High then motor rotate anticlockwise & if electric continuity will LOW Motor will be remain stop // Include the Arduino Stepper Library #include int i=0; int PB=13; int RELAY=12; // Number of steps per output rotation const int stepsPerRevolution = 200; // Create Instance of Stepper library Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); void setup() { // set the speed at 60 rpm: myStepper.setSpeed(60); // initialize the serial port: Serial.begin(9600); pinMode(PB, INPUT); pinMode(RELAY,OUTPUT); } void loop() {
If you want to get even more slick, just do a digitalWrite (5, blinkstate); 'HIGH' and 'LOW' are just constants equating to 1 and 0. You can use 1 and 0 instead and it works just fine.
Interesting. Those example programs are for absolute beginners who eventually progress and see that there are better ways. I think that it is good to learn the nuts and bolts first.
You can use interrupts for part of it, but you still would have to debounce manually. The point of this is to provide a unified, hardware-independent, helpful way of working with events of all types, which does all the ugly stuff for you.
Well by using a custom library one can do alot. You still end up sending time to make the library anyway instead of direcly programming. Also using if and else is not the best way to program states IMO. But good to let people know how to avoid delay() at all cost
You know, given that this event library is intended to reduce code bloat, it sure as hell looks bloaty. Why not use closures? Why not use handles to enable and disable other events? Why tear down the entire edifice when the button is pressed, just to build it back up again afterwards? I've written a few simple event libraries myself, in various languages (C++, Ruby, .NET, Lisp), and yours needs work.
Closures *can* work, but they were brand new to C++ when I wrote this video. Additionally, closures use extra memory (it creates hidden classes), which you are quite short of in Arduino.
TatsAndGraphics He didn't use the serial monitor nor a serial hardware(as far as I can remember). Therefore he didn't have to begin serial communication.
Don't get disuaded from continuing development because of the fools. I've one question......do you have any idea how this library would work under FreeRTOS for the Arduino. Obviously, I'm doiug a project sing FreeRTOS, and I attempted to just use the code that I had written that ran in 3 separate Arduino NANO modules, all running different state machine login, and all required their own debounce functions with different variables used for each timer.....what a PITA. So, can you kind of describe what the code does related to a context? FreeRTOS also uses contect switching, but obviously differnt that your code. I had to use multiple debounce functions because using the same variables when multi-tasking just doesn't work......everybody steps on everone else. Anyway, good stuff. I may try to implement it into my FreeRTOS code, but I still need to do more testing to verify it all works after combining all those state machines in FreeRTOS. Since the events that I'm monitoring is somewhat time critical, I couldn't use any standard roud-robin or timeslice code, as 15ms between tasks(at a minumum) multiplied by 3 total tasks simple took to long. So, my code basically releases execution to the next task available at the exit of each state of each state machine. To get some timings, I set an LED for each task On/Off every cycle through each state machine(they don't do anything unless input is seem) and used a scope to measure times between LED changes, and I can do a context switch in about 175usec. Compared to 15ms, that's a game changer. Now, I need to verify if that 175usec results in reliable results. I'm doing quaratue detection of moving moddel train wheels across 2 FO strands about 0.030 apart, so the timings for when the train is moving at speed is critical. Using undividual NANOs, I can get reliable detection with no obvious limit.......the upper speed I tested at exceeded 200 scale MPH(measured with a speedometer car), which is rediculousy high. Half that is my target. Sorry for writing a book......THANKS.
I'll have to check your library. It's definitely conceptually cleaner, but talking about 'all the extra code' is maybe a bit naff if your library basically adds it all back in. I mean, we're working with microcontrollers, code size matters.
So instead of threads the timer is used. I wonder how the delay() formulas look like under different boards. Normally CPU should not execute any instructions and perform like a sleep() in multithreading apps.
Absolutely no. Even in multithreading apps the CPU is continuously running, it goes in sleep mode only when the computer/microcontroller is suspended, which means no processing. Also multithreading systems are totally different beasts when it comes in embedded microcontrollers such as the one used in Arduino.
delay() should not execute anything - it is power consuming code if loops are used. Maybe there are some low power instructions that take lot of cycles.
Hi I'm late to the party, I don't understand your first if statement "if(isblinking)" there is no comparison so how does this even compile? And since it did compile, what did the code actually do
The if statement evaluates what is in the brackets. If will do whatever if the statement evaluates to True. So if 'isblinking' is already true then the statement if(isblinking) is the same as if(True). Since True will evaluate to True, the stuff inside the If will run. It is like saying If(1). You don't have to say If (1 = 1) where that would be True because 1 and True are the same thing. So if(isblinking) is the same as If(True) for when isblinking is True. (The opposite when isblinking is False in which case if(isblinking) would then evaluate to False)
I am new to Arduino and would like a better way to program for me it looks such a lot of work to make something happen I have just put together as small circuit with a 555 all I need is to choose C PLUS R job done but in Arduino so much more to do the same thing why?
How can i make Arduino switch off the led after Bluetooth connection is lost. this is my sketch: // Hardware setup: // BT module Arduino // GND ------- GND // VCC ------- 5V // TX-O ------ pin2 // RX-I ------ pin3 // Arduino UNO already has an LED attached to pin 13 #include int bluetoothTx = 2; int bluetoothRx = 3; SoftwareSerial bluetooth(bluetoothTx, bluetoothRx); int led1 = 13; int led2 = 12; char cmd[100]; int cmdIndex; void exeCmd() { // "led" is the led id if(strcmp(cmd, "led 0")==0) digitalWrite(led1, LOW); if(strcmp(cmd, "led 1")==0) digitalWrite(led1, HIGH); if(strcmp(cmd, "led 2")==0) digitalWrite(led2, LOW); if(strcmp(cmd, "led 3")==0) digitalWrite(led2, HIGH); } void setup() { delay(500); // wait for bluetooth module to start bluetooth.begin(115200); // Bluetooth default baud is 115200 bluetooth.print("$"); bluetooth.print("$"); bluetooth.print("$"); // enter cmd mode delay(250); bluetooth.println("U,9600,N"); // change baud to 9600 bluetooth.begin(9600); pinMode(led1, OUTPUT); digitalWrite(led1, LOW); pinMode(led2, OUTPUT); digitalWrite(led2, LOW); cmdIndex = 0; } void loop() { if(bluetooth.available()) { char c = (char)bluetooth.read(); if(c==' ') { cmd[cmdIndex] = 0; exeCmd(); // execute the command cmdIndex = 0; // reset the cmdIndex } else { cmd[cmdIndex] = c; if(cmdIndex
Geee, i thought the arduino ide would of had the original just getting started theme; to more bells and whistles for advanced programming in C++? nice video and fantastic info...:)
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.
Sometimes, it's also used to test if the device is broken.
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.
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.
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.
Interrupts are not viable for everything, the button was example but he was demonstrating the principle
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.
Surely there is nothing stopping you using sub-routines (functions) which are outside Loop? The "main" part of any C program is a loop isn't it? It calls subroutines when things happen.
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.
Hey, there, any updates? How did it go?
@@taka4059yeah I’m curious too!
I wrote something very similar, integrated with MQTT ; your version has given me food for thought. Well done.
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.
You ran out of stack space. 😁
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!
veterens will eventually not use this, and go to the standard way of writing code. So for professional products a coder will either use freeRtos or the standard baremetal programming.
Oh god, this went over my head .......
Don't get discouraged. He made it way too complicated. Just look up interrupts. It's literally 1 line to solve the problem from 1st example.
The problem is he didn't dive into the code in the library. That's what I was hoping to see. Maybe he has another video explaining the internals of the library. I think that would make this much easier to understand. It's also not clear if interrupts are being used or just polling. I'm not too familiar with Arduino programming but I would think the downloaded libraries are the source code and not just a pre-built image. If so, you can pull it down and see what's going on under the hood.
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.
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()
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.
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.
Is there any advantage to using this library over say, using an interrupt? Thanks.
The primary one is that it merges different types of events (time, pins, and complex events) into a single unified framework that is more intuitive than the standard one. The specific help that you would get compared to an interrupt is automatic debouncing.
@@BPLearningTV Great, thanks! I can see how this would be very useful!
I've been abusing interrupts a lot! I love them! They do exactly what I want when I want. Screw debug. There is no code in my "loop" :)
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#.
That's a great idea! I will try to get that done soon. It would also be good to teach people how to make an Arduino library available.
Sure, looking forward for it!!!
I would love a video that goes into your library a little more and more examples of usage.
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!
That is the correct way to use it. I use the same technique to do event functions, the advantage is I can change my mind and interrupt the event before its timing finish. Delay puts machine in freeze stage where nothing can be interpreted while event reading is mostly cloak speed and loop dependent.
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 RUclips suggests me this video...
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.
Using header files and APIs might make YOUR coding "easier" but you lose exact control, increase bloat, slow execution time, and need to learn a one-job set of functions useless in your next project. Start learning as low-level as you can so you know the foundation of what you are building. BTW; in the "old way" we calculated RC time constants to create astable multivibrators using a battery and a few discrete components. We also learned not to make it look like a rat's nest. Neatness counts.
Event driven absolutely makes sense here. Thanks for filling the gap!
This library has a problem with SHT3X (temperature and humidity sensor) library. It does work sometime but mostly will display 130 C.
Sorry about the question but I’m completely new to programming, in your eventually demo, you tell it to blink but where in the code is the time which you define the blink timing?
I don't know if the thread here is still active...
I've been having some issues trying to fire an event right after another event, basically the act of turning on an LED, wait for a moment, then turn it off and turn on another LED of another color. Here's the catch tho, the time where both LEDs are on is different, so it should be two different events right? well, i tried that and what I end up is with two events at the same time. You could say "well use delay()" without realizing that delay will literally freeze the program for a given time and not allow the interruption of the event during the delay. So how can I make two events where one depends on the other to end in order to trigger?
This is so much better than the way arduino uses code, man ive programmed in C and C++ VB , scripting languages and having only a main and a loop function sucks, what about making your own, what if i dont want something looped everytime. Im glad you wrote this library, keep up the grat work
Plz tell me code if I want turn on the buzzer for only 10 seconds after reading then it should off automatically .
So the program can run about a month before it is overflowed ?
Please help me :::::
I want to start my loop only when my Ultrasound sensor detects the object
and if the object is not present then it should directly stop all the operations until the ultrasound sensor detect another (different or same) object
Hello, I'm new to arduino and I find your library really a life-saver, but I would really like to be able to delete a single listener instead of resetting the entire context (all the listeners), how can I do it?
This is a nice effort. Are you using a different platform these days ?
hello BP, you can make your libraries code colored by using a keywords text file and putting it in the libraries folder for arduino
May I link your video in the description of a my recent video where I show a event-driven (and RPC) framework originally designed for devices that are slightly different from Arduino? I think your example at the beginning is useful to understand how messy can become programs in sequential execution.
By the way, interesting library the one you have made. It seems that your listeners are related to fixed features such as time and input changes, do I have understood correctly?
Hi, there's a bug in this library. Adding "false" to create a single, no-repeating function call doesn't work. It calls repeatedly (very quickly). Can you please provide some advice on executing a single function call that's delayed? I'm using eventually in a larger project, and have also tested and confirmed this issue by altering the simple blink example included with the library. Thank you.
Hi. I'm a beginner to arduino an still learning I'm watching videos here on RUclips.
But I had a question, if you could help me out on a code/ Sketch I am trying to put together a sketch for a Crossing bell for a Model railroad.
Cool but 3 kB to blink an LED, though
How does it scale with a more complex program?
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
Thanks a very good explanation for those asking why the not been written before this for make since as the writer explained in line what he is doing and how to think
So this is mor about how to develop the code
Thanks a lot
I like the idea, but the non-event code when completed (minus debounce and not very well optimized) was 3% of storage, but the just the setup for the event code was 5%... without actually doing anything.
When you are close to zero (3%) it's easy to jump to 2% more, adding antibouncing that would likely take 200 bytes once compiled will bring it to 4%.
Even-driven programs have to pay for the burden of the underlying event handling system, but they benefit when the programs become more complex.
Looks great but why have you the expression stop Blink in the startBlink function?
Is it possible to make millis function in separate tab and use millis single line command like delay? For make code more simpler
cool,can you make with a 20x4 lcd?, make a sentence with the button and run the command to turn on the LED, and same command for another LED
So essentially, timer interrupts?
Does anyone know how to program NeoPixel? My daughter is working on a project and this is our first time. Would appreciate if someone knowledgeable could talk with us for a few minutes to help get us started. Many thanks in advance!
This could be really helpful for my midi instrument project! Thank you
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?
You miss the point the BLINK program is a starter program to help get new users to get using the Arduino . I am nearly 60 years old, my early programming was in machine code. I found the blink program useful to get started with the Uno...
I actually love blink myself and teach it to my students. The problem is that there is nothing in Arduino that helps you move past blink. People see the simplicity of blink, and they are lured in to thinking that it will all be that simple. However, once they get to the point where you have to have counters and delays and debouncing, then a lot of people just give up. This isn't the "easy-as-blink" system they thought it was. The goal of the library is to make *regulare* Arduino programming more simple. Blink is used as a counterpoint just to show where the limitations of it are, not that people shouldn't use it as a starting program.
@@BPLearningTV I agree with what you are saying here. My experience is in Javascript and your video caught my attention as I (as an Arduino newby) was thinking myself that event handlers would be very helpful. I've written some jQuery event handlers myself for some projects and they can be extremely powerful. The basis of what you've done here is great, although I think there is always room for improvement. I would like to see something more object oriented in a future release. Imagine what you could do passing objects to event listeners.
Agreed. Blink introduces you to:
1. Delays
2. Setting a pin height
3. setting a pin low
4. program flow.
5. program structure.
Is there some method for listening during the running cycle like FOR or WHILE, or it is necessary to use an interrupts on Arduino pins?
you could have set a bit high when the input is on than check if output is high/low invert the state and reset bit
This was cool
Is there anyway to define some variables and then change there value using Ethernet
Basically I want to assign a few preset variables and then be able to change them over the internet.
Think of a lock code, or codes....
Event driven would be good as there could be locks in use while the variables not in use are changed.....
Any thoughts?
Thanks Rob
Hi i m nahid hasan. Recently i buy a IOT GA6-B mini Gsm module. I face a problem Ardiuno R3 program is Upload but GSM AT Command is Not Response...I don't know Why .....Please Help me to solve this problem....
i mean, i know that you probably shouldnt use delay but, for "booting" my arduino and display things only once at the start its quite useful.
Thank you! This is exactly what I was looking for!!
Oh, and this looks awesome! I can't wait to try it out!
Brilliant! I've been losing sleep over how to implement this! And I'm glad you took your time explaining it: some of us have brains that have a lower clock cycle than they used to!
I am using EvtPinListener with a PULL_UP button. I'm not sure if that is a cause, but the event only actives when I let up on the button. How can I keep the event going as long as I have the button pressed?
I gave up on the whole event thing. I put everything back into a loop and did my own debounce checks. The current project I'm working on does fine in the loop. Plus, your program was causing my Arduino to crash. It hasn't crashed and is working a lot more efficiently in the plain old loop.
Event generators are going to eat more clock cycles, continuously, while polling events, that will be bottle neck in performance critical application, in Arduino.
Does this method work to fade an led or run a for loop/ PWM, controlled by a button press?
Thank you! Great video with great functionality!
thanks for sharing this ill be looking at implementing it into my project. is there any documentation for this library floating around anywhere i would like to look more into it as i plan on using it as a transmitter/Reciever for an ESK8 Board.
I have 2 questions;
1. How do you write a program to create a double click function on the button to have the motor turn a certain amount of degrees, hold it as long as you hold down the button, than rewind back the exact way when you release it?
2. Could and how do you program/solder that circuit into smaller arduino models?
So, how might one update a display, check for serial port activity, etc. that one normally does in 'loop()'?
can anyone help me in controlling a motor that is for a specific time i want it to turn on and also taking some inputs from sensors to turn it on but specified time period will not affect when it is on due to a sensor output but after sensor is done the specified time period should start whee it ended . Regards
This program is not completely good, can you show me how to turn off on the LED instantly at the time you click the button? According to this sketch, sometimes I press the button, the LED stop blinking but still on the HIGH state, which is a little annoying. Thank you!
Also, you declared your unsigned int variable (curtime) inside your loop. Do this at very beginning of your code instead.
No, it's correct like he wrote it. Try it like ur saying to see why it wont work :)
This guy wrote an event library with classes and casts and function pointers.. do you think he'd make that mistake? 😄
@@ParabolicLabs Bingo. "Look, my code is shorter!" and less efficient. This video is all about using as few lines as possible, not efficient (or clear) programming. I'm surprised they didn't put more into libraries to make it even shorter.
You made it a little more complex. It's actually easier then that. But nice to see it a different way. Yes event programming is great. I guess I'll eventually use your method someday. 😆
Hi, Thanks for the great tutorial.
I am new to Arduino and as well to C++ code, but after spending about a month watchin tutorials I believe I can learn this fast. Now the reason I comment is not only to say thank you but also want to ask a favor if even possible.
I work for a Laser (Optic) company An I have built electronic mechanical stations to check laser stability and save data to an excel file, well I have been thinking about doing the same with Arduino and some codes all I need to do is to check 3 monitors from the laser (voltages in the scale of 0.5VDC to 4VDC) in the 3 monitors I need to set alarms based on the Voltage, e. g (V1 is set a max of 2.2V) if the voltage exceed this number I want to trigger an alarm or else do nothing, when an alarm is triggered I need to be able to identify which event caused the alarm by a code or by turning an led which will be labeled accordingly and so on, same for the other two monitors. all this will also turn a pin off which turns off a relay, the relay will turn the laser of by interrupting a signal, after 1 second will turn a second relay off to turn the housekeeping supply and a third relay will be interrupted the same way to cut main power to the laser. I have pretty much everything documented, so if you or somebody in the group could give me a hand it would be greatly appreciated.
Thanks
Some constructive criticism. It took 30 minutes to explain something that could have been explained in 5 minutes if you had written your examples prior to making the video and just loading them in side by side windows. Then you could explain why one is not as good as the other and dispense with all the typing in real time.
Understandable, but typing in real time slows it down for people who are trying to follow. That is, if you are new, and trying to follow a new comment, seeing it typed out can pace your mind to help you follow the concepts better.
yeah, turned playback speed to x2
I agree with BP Learning. If you go slow and explain, where one can go wrong, one can understand better
Thank you! I was thinking the same thing. I see this noobie error all too often here on RUclips.
@@BPLearningTV No!
I don't understand the logic please. After you added Wasbuttonpushed. I don't understand how the button works, if it stays high after push or goes low after your hand releases. Plz explain
I have analysed it both ways and it doesn't work in my head
If it stays high after push, setting Wasbuttonpushed to 1 should stop it from entering the else part again and because the button stays high, Wasbuttonpushed cannot be 0 gain hence no more switching
So where is the library you made?
No worries, I found it and you don't give it like opensource you want to licence it... :(
Could i use this to program a heart monitor and set an event to trigger a sms when heartrate reaches a certain bpm?
yes of course, there is a breakout/module for that sensor.
@@monggos would you happen to know the name of that?
i wonder if there are a time spent on addition for if its a loop then if you add 1 a variable then something would check if it have meet for example 1000 then turing it off reset then turn it on and between the loop it have a checker if it have a input .... im sorry im new to this i haven't even have a arduino before hahaha
Really great library. I'm going to use it tomorrow! Thanks!
Hi, Sir can you help me I have created a program that controls the motion of the stepper motor in which i have used the L298 driver , but now I'm stuck at one point , I'm not able to apply anticlockwise motion via push push button (PB IS ACTUALLY LIKE ELECTRIC CONTUNITY PATH (please suggest me how i apply if command)......
Actually it is a robotic machine where if electric continuity is High then motor rotate anticlockwise & if electric continuity will LOW Motor will be remain stop
// Include the Arduino Stepper Library
#include
int i=0;
int PB=13;
int RELAY=12;
// Number of steps per output rotation
const int stepsPerRevolution = 200;
// Create Instance of Stepper library
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
void setup()
{
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
pinMode(PB, INPUT);
pinMode(RELAY,OUTPUT);
}
void loop()
{
while(i
If you want to get even more slick, just do a digitalWrite (5, blinkstate); 'HIGH' and 'LOW' are just constants equating to 1 and 0. You can use 1 and 0 instead and it works just fine.
Don't even need that, do digitalWrite(5, !digitalRead(5)); Doesn't need the if/else or the state variable then
Interesting. Those example programs are for absolute beginners who eventually progress and see that there are better ways. I think that it is good to learn the nuts and bolts first.
guys can someone confirm if I can just use interrupts instead of this ?
You can use interrupts for part of it, but you still would have to debounce manually. The point of this is to provide a unified, hardware-independent, helpful way of working with events of all types, which does all the ugly stuff for you.
Have you used eventually with LCD 's and menu system on arduino ?
if you ever do , a tutorial would be great....
Eventually reminds me of java ...
I need to do that. I haven't had a lot of time to tinker lately.
How can you adjust the blinking intervals? I mean if I want the LED to remind on for longer how would you adjust that?
Those time numbers are milliseconds ON or OFF (High or Low), so you can easily change those time variables.
Well by using a custom library one can do alot. You still end up sending time to make the library anyway instead of direcly programming. Also using if and else is not the best way to program states IMO. But good to let people know how to avoid delay() at all cost
Is the final Eventually code example 2-3x larger than the messy original code?
Yes - the library itself takes up a decent amount of space.
You know, given that this event library is intended to reduce code bloat, it sure as hell looks bloaty. Why not use closures? Why not use handles to enable and disable other events? Why tear down the entire edifice when the button is pressed, just to build it back up again afterwards?
I've written a few simple event libraries myself, in various languages (C++, Ruby, .NET, Lisp), and yours needs work.
Closures *can* work, but they were brand new to C++ when I wrote this video. Additionally, closures use extra memory (it creates hidden classes), which you are quite short of in Arduino.
In the example code I do not see where serial.begin is initialized or where the baud rate is set. Is this predetermined in the library?
TatsAndGraphics He didn't use the serial monitor nor a serial hardware(as far as I can remember). Therefore he didn't have to begin serial communication.
i am following your code simultaneously on sketch but it gives error
Most of the negative comments on this library seem to be from know-it-alls who probably have never had to do amy REAL programming.
Don't get disuaded from continuing development because of the fools. I've one question......do you have any idea how this library would work under FreeRTOS for the Arduino.
Obviously, I'm doiug a project sing FreeRTOS, and I attempted to just use the code that I had written that ran in 3 separate Arduino NANO modules, all running different state machine login, and all required their own debounce functions with different variables used for each timer.....what a PITA.
So, can you kind of describe what the code does related to a context? FreeRTOS also uses contect switching, but obviously differnt that your code. I had to use multiple debounce functions because using the same variables when multi-tasking just doesn't work......everybody steps on everone else.
Anyway, good stuff. I may try to implement it into my FreeRTOS code, but I still need to do more testing to verify it all works after combining all those state machines in FreeRTOS.
Since the events that I'm monitoring is somewhat time critical, I couldn't use any standard roud-robin or timeslice code, as 15ms between tasks(at a minumum) multiplied by 3 total tasks simple took to long. So, my code basically releases execution to the next task available at the exit of each state of each state machine. To get some timings, I set an LED for each task On/Off every cycle through each state machine(they don't do anything unless input is seem) and used a scope to measure times between LED changes, and I can do a context switch in about 175usec. Compared to 15ms, that's a game changer. Now, I need to verify if that 175usec results in reliable results. I'm doing quaratue detection of moving moddel train wheels across 2 FO strands about 0.030 apart, so the timings for when the train is moving at speed is critical. Using undividual NANOs, I can get reliable detection with no obvious limit.......the upper speed I tested at exceeded 200 scale MPH(measured with a speedometer car), which is rediculousy high. Half that is my target.
Sorry for writing a book......THANKS.
Great video. One question: can you still use a loop routine in your sketch?
void loop() {
// run Eventually loop
mgr.loopIteration();
}
I'll have to check your library. It's definitely conceptually cleaner, but talking about 'all the extra code' is maybe a bit naff if your library basically adds it all back in. I mean, we're working with microcontrollers, code size matters.
I guess this is for those who already know how to write code
So instead of threads the timer is used. I wonder how the delay() formulas look like under different boards. Normally CPU should not execute any instructions and perform like a sleep() in multithreading apps.
Absolutely no. Even in multithreading apps the CPU is continuously running, it goes in sleep mode only when the computer/microcontroller is suspended, which means no processing.
Also multithreading systems are totally different beasts when it comes in embedded microcontrollers such as the one used in Arduino.
delay() should not execute anything - it is power consuming code if loops are used. Maybe there are some low power instructions that take lot of cycles.
Nope.
Then try blinking 6 leds with different intervals at the same time lol
Hi I'm late to the party, I don't understand your first if statement "if(isblinking)" there is no comparison so how does this even compile? And since it did compile, what did the code actually do
The if statement evaluates what is in the brackets. If will do whatever if the statement evaluates to True. So if 'isblinking' is already true then the statement if(isblinking) is the same as if(True). Since True will evaluate to True, the stuff inside the If will run. It is like saying If(1). You don't have to say If (1 = 1) where that would be True because 1 and True are the same thing. So if(isblinking) is the same as If(True) for when isblinking is True. (The opposite when isblinking is False in which case if(isblinking) would then evaluate to False)
I am new to Arduino and would like a better way to program for me it looks such a lot of work to make something happen I have just put together as small circuit with a 555 all I need is to choose C PLUS R job done but in Arduino so much more to do the same thing why?
Because for simple task like blinking a 555 is far better and a microcontroller is overkilling.
A 555 is designed to work as an oscillator. Such a blink led can also be built with two transistors, 2 capacitors and a few resistors 😜
you can simply "int tmp = digitalRead(ledpin); somedelay ; digitalWrite(!tmp);
can you make with a 20x4 lcd?, make a sentence with the button and run the command to turn on the LED, and same command for another LED
I love how most people are complaining in the comments. BEFOUR LEARNING THIS, learn the basics of c/c++ and then you'll understand.
This is till to awesome even in 2020, thx BPL for sharing
Nice :) Can you provide the source code of your Eventually library?
github.com/johnnyb/Eventually/blob/master/src/Eventually.cpphere you have
@@mj2906 github.com/johnnyb/Eventually/blob/master/src/Eventually.cpp (space) here
even better github.com/johnnyb/Eventually
Hey, Phew! Are you you trying to dissuade me? Hell, that was a MINDFULLL. But thanks for the knowledge.
You don't half to set up the button in code but couldn't you just wire the button in a certain way for that to work
hello master..try creat for 433mhz to control 4 servo
How can i make Arduino switch off the led after Bluetooth connection is lost. this is my sketch:
// Hardware setup:
// BT module Arduino
// GND ------- GND
// VCC ------- 5V
// TX-O ------ pin2
// RX-I ------ pin3
// Arduino UNO already has an LED attached to pin 13
#include
int bluetoothTx = 2;
int bluetoothRx = 3;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
int led1 = 13;
int led2 = 12;
char cmd[100];
int cmdIndex;
void exeCmd() {
// "led" is the led id
if(strcmp(cmd, "led 0")==0) digitalWrite(led1, LOW);
if(strcmp(cmd, "led 1")==0) digitalWrite(led1, HIGH);
if(strcmp(cmd, "led 2")==0) digitalWrite(led2, LOW);
if(strcmp(cmd, "led 3")==0) digitalWrite(led2, HIGH);
}
void setup() {
delay(500); // wait for bluetooth module to start
bluetooth.begin(115200); // Bluetooth default baud is 115200
bluetooth.print("$");
bluetooth.print("$");
bluetooth.print("$"); // enter cmd mode
delay(250);
bluetooth.println("U,9600,N"); // change baud to 9600
bluetooth.begin(9600);
pinMode(led1, OUTPUT);
digitalWrite(led1, LOW);
pinMode(led2, OUTPUT);
digitalWrite(led2, LOW);
cmdIndex = 0;
}
void loop() {
if(bluetooth.available()) {
char c = (char)bluetooth.read();
if(c=='
') {
cmd[cmdIndex] = 0;
exeCmd(); // execute the command
cmdIndex = 0; // reset the cmdIndex
} else {
cmd[cmdIndex] = c;
if(cmdIndex
Geee, i thought the arduino ide would of had the original just getting started theme; to more bells and whistles for advanced programming in C++? nice video and fantastic info...:)