Saturday 7 November 2009

Arduino - Practise

On Thursday we had our first workshop on Arduino. We left the class with an unfinished exercise, in which we had to create a toggle button with a resistor, a LED and a switch. Our homework is firstly to finish this exercise and then create a blinking toggle button. The circuit I used for both exercises is this (click to enlarge):



Toggle Button
The way I implemented the Toggle Button is the following (click to enlarge):



Thinking of the Toggle function, we notice that the state of the LED will change only when the user changes the state of the switch (presses or releases the button) and the new state of the button is the pressed state. From what I found, LOW equals to FALSE and HIGH equals to TRUE. So LOW is the released state of the button, and HIGH is its state when pressed. Implementing the idea stated above, in each "Refresh" (each time loop function is called) we read the current state of the button and compare it to the previous state and if this is true we check if the current state is the pressed state. If so, we change the LED's light from off to on and vice versa.

Blinking Toggle Button
The way I implemented the Blinking Toggle Button is (click to enlarge):



Based on the code of the Toggle Button the only change is that we add the blinking ability, which is implemented into the added if-statement. Each time the button is pressed we don't change the state of the LED but the value of a "flag" variable which declares if the LED should be blinking or not. To implement the blinking of the LED at a constant interval, we store into a variable the times the loop function has been called and in every "Refresh" we check if this number divides exactly (modulo equals zero) the interval, which is set in ticks. If so, it is time to change the state of the LED.

Looking forward to the next workshop! B-)

No comments:

Post a Comment