Type Here to Get Search Results !

Start Sketching Arduino Code With Us

Now, let's see the sketch i used to run this project, as we saw before, we are starting the sketch with some comments about the goal or the function of the sketch.

In that case we are talking about "step-1a" and the idea is to use two buttons, one is for testing the leds and the other one is to run some animation wave. We are utilizing the digital input and also digital output, now in each program, you will see some section related to global variables declaration in our case we would like to map the physical sensors connections to the arduino digital pins.


According to our project, so it is going to be a constant integer variable, that i am calling the first one "testbutton", and it is equal to 2, meaning pin#2, and the same for the "wavebutton", just different pin, going next, we would like to map the six leds physical connections to the arduino digital pins.

Now, i have six pins so one option is to create six different variables. I can call it "led1, led2..." and so on, in that case to make it more easy i am using the capability to create an array ok, that's the name of that variable "ledspin", it is an array of integer in that case it is an array of 6 integers values, now the values you can see over here, that's the pin numbers that are being used to connect the leds, meaning if i am looking in this location this is the pin number 3 is used for led # 1.

If i am looking over here, so this is pin number 9 being used for led # 4. I am sure that you remeber that in every sketch, we have two basic functions, the first one is called "setup()" function and it is being run only once, and the other one is the "loop()" function that is running all the time in a loop. Ok so the main code will be under the "loop()". And some initialization will be placed under the "setup()" function. So in our case we are using a function that is called "pinmode".

To setup each digital pin as input or output, so the "testbutton" will be setup as input, we would like check if a button was pressed the same true for the "wavebutton", we are setting that as an input now for the six leds that are connected to six pins in our arduino. We would like to setup as in output. So the easy thing to do is to use some "for" loop, start from zero until five and each time change the index in the array and setup that as an output. The next main function is called "loop()" i mean most of our code will be placed over here, now what i have done to reduce the complexity of the "loop()", is to create two customized functions that i am running inside the loop.

The first one is checking the test button and doing something if the test button is being pressed, and a second one is checking the "wavebutton" and also doing something if the wavbutton was pressed, and because it is under the "loop()"  running those two functions again and again.

Now let's zoon on each customized function, ok, let's review the first function i created that is called "checktestbutton". So the first thing that we can see is that this function is not returning any value, and that's the reason i can see the saved word "void", and it is not getting any parameters as input.

Now moving to the first line, what i would like to do is to read the status of the testbutton right now, so i am utilziing a function that is called "digitalread", and sending as parameter the pin number that i would like to read and of course we are sending the testbutton as parameter and i will get the result, it can be low or high.

Now using this operator, i am comparing the result if it is equal to high, meaning the button is pressed, in that case i would like to turn on all the six leds, and for that i am utilizing the "for" loop, starting from zero until five and here i am utilizing the "digitalwrite" function instead of "digitalread" and as an input the same, i am providing which pin number i would like to write and the value that i would like to write inside and each time this index will be changed according to this "for" loop, it will start from zero and go to 1,2,3,4 and 5, and each time different index will be provided.

Also, i will get different value from the "ledspin" array as you remeber we initialized this "ledspin" over here so according to that i will get the relevant pin numer and set it to high, after that i would like to wait for 1 second so i am utilizing a function that is called "delay()", that paramter are in milliseconds so that will be 1000 milliseconds to get 1 second, and after finishing that i would do the same, just turning off all the six the leds, that's what the "checktestbutton" is actually doing.


Moving on to the "checkwavebutton", the "checkwavebutton" is not returning anying so, i can see the "void" over here, it is not getting any parameters, but here using the "digitalread" function i am reading here the "wavebutton" as an input, and the same logic, i am comparing that to "high" to see if the button is pressed, in that case what i would like to do is to run some wave of leds starting from the first one, until led number 6 and then start from led#6 and going back to  led#1.

So i am using again the "for" loop, using the "i" as an index, so the logic is simple, setting using the "digitalwrite" the relevant led to "high", waiting 400 milliseconds and then turn that to off, the same led, and doing that again and again running from the first led until the last led and we finish that we are doing the same just different order.

So here i am initlizing the index to 5 and then reducing the index each time. So i will get that as a wave just the other side. Now there is something i would like to improve in this code, and this is related to the "checktestbutton". What we have over here is that you need to hold the test button, and then the all leds will be turned on, as soon as you release the button the all leds will be turned off, in may cases you would like the user to only press that button on and off, without holding this button, and this is what we are going to do in the next sketch as an improvement.

We will call it "step-1b", and we will learn how to handle this situation, it is a little bit more complicated but it is important to close the issue on how to read information from buttons. Please watch this video for practical demonstration and clear and better understanding.



Post a Comment

0 Comments