Type Here to Get Search Results !

Arduino Sketching For Led Test And Wave Animation -2

Looking on the improved sketch, i call it "step-1b". That now we are handling the situation that i would like the user to click only once. On the test button that will toggle on and off, and for that we need to handle switch bouncing and this is what we are going to do in this program under the variables declaration, i added three more variables


The first one is like the flag of the status of the leds and it will present the state. That we will know if the leds  are right now on or off, and two additional boolean variables, first one is the "lastbutton" and the second one is the "currentbutton" status, and we initial them to low value when starting the program. If i will scroll down below then under the setup function i didn't changed anything, it is the same function. And the same goes for the loop function, i didn't changed anything

I changed the content of the "checktestbutton" custom function. Now let's review the new code that i added under the "checktestbutton". Maybe if i am looking on that for the first time, it will look a litte bit complicated. But at the end it is a simple logic. The first line i am updating and reading the status of the test button using some function that i will discuss in a minute, assuming that i have this status right now in this variable

I would like to verify some specific condition and if you remember the diagram. So that's the condition i would like to test if the "current" is high and the "last" is low the current button is high and the last button is low, meaning that the first time the button was pressed, in that case i will toggle the leds state. That's the line that will switch the value of the "ledson", meaning if the "ledson" flag is low, it will switch to high, if it is high it will switch to low, and of course i am updating the value of the "lastbutton" from the "curretbutton" so it will be always aligned, and writing that using the digital write function and updating the leds using this flag

Now let's how exactly i am reading the right value of the button using this function that is called "debounce". Ok so the debounce function is actually the function that is providing the delay. That we will read the stabalized state of the button. This function, "debounce" is getting two parameters. One parameter is the pin number we would like to test, and the last state of the specific button

If i will scroll a little bit up then you will be able to see this new function, "debounce". It is returning a boolean value, and it is getting two parameters, as i said the button pin as an integer and the last value of that, button as a boolean. Now, the first thing that i am doing, i am reading into some local variable that is called "current",, the current value of this button using digitalread function. If "current" is not equal to "last", meaning there is some change

It will wait for 5 milliseconds using the delay function and only then it will read again using the digitalread reading again the value and put that in the "current" local variable and return that. The all idea in this function is that it will return the stabalized value/state of a specific button

I hope it is not too confusing and you got the idea. Now let's see the result, ok we changed the software and we would like to upload that and see the result in the actual project, so i am clicking on "verify", of course every time i am changing something and i will upload that to the arduino and let's see the result. Please watch this video below to understand it better.




Ok, so after uploading this new sketch with the new code into the Arduino. now we know that we can go to the test button and click on that only once every time. without holding this button, clicking.... each time it, will switch the LEDs ON and OFF. that's a behavior that users will expect. usually while pushing a push button. so the software for handling this switch debouncing is going to be useful for any project that you are going to use today or in the future

That's all for this step, step # 1. we learned few things that are important. how to send digital output to LED device,. how to connect those LEDs using resistors. how to read digital input from an external component like this push button, and how to handle some mechanical limitation of a pushbutton, like the switch bouncing and handle that in very nice way using software. that's all for this step and let's move to the next step, adding more analog capability into our project.



Post a Comment

0 Comments