<<  
   

arduino programming.
________________________________________________________________________
Transformations: Konstfack, an Open Studio part of Interdisciplinary Studies. Pablo Miranda Carranza and Åsmund Gamlesæter. Week 18-22.

| introduction | workshop tasks | algorithm examples |

programs and algorithms
________________________________________________________________________
There is no better source of information about the arduino than the arduino site itself, so we will be using it for following the set-up, tutorials and using the snippets of code for different tasks. Once again the arduino page is: www.arduino.cc

The computer programming language used by Arduino is based in one of the most important languages in the last 35 years, as it is C. C, or one of its variatiosn as C++, is the standard language used for programming most applications you may have on your desktop, no matter if it is a Mac, a Windows, a Linux or any other type (specially) of Unix machine. Further more, many other popular languages such as Java, and particularly interesting to us, Processing, use a syntax, that is, a way of writting the code, using {}, (), ; for end of lines...etc originated in C.
Learning C is out of the scope of the course; Arduino lanuiage is far more simple than C, in fact you have most of the instructions you can write here.

We will introduce here some of the essential concepts of programming, and in particular what is a program or algorithm. An algorithm is a finite set of well-defined instructions for accomplishing some task (from Wikipedia). A standard example of an algorithm is a cooking recipe. For explaining someone how to cook a cake or a stew one does not show a picture of it, but explains step by step how to make it. What the recipee in this case represents are the steps and performance of making the cake; what we are going to be doing are the'scripts' for a performance, in this case the performance of an object or a situation. An algorithm can be thought as a sort of abstract machine (for conceptual background check Turing Machines). In our particular case, in which we a re going to build real machines and mechanisms, this is very interesting, because almost any component of any machine we could think of building, for example those of a mechanical clock, can be 'simulated' by the abstract machine an algorithm is and therefore be run in the arduino board. What this means, in more concrete terms, is that the arduino board can become any machine you can think of, if you are capable of writting a program or abstract machine to simulate it: a telephone, a clock, a burglers alarm, a traffic light, a door opener...arduino can become these and many more yet unthought of machines.

Examples of algorithms.

The important concepts we will look at in this introduction to programming are:

Variables: Variables are parts of your code, mostly values that may change, in contrast to constants, that wont. For example, if one writes an algorithm of the type:

1. jump 7 times
2. draw a 43 cm long line on the floor

you could make variables like:

1. myvariable=check the time
2. jump myvariale times
3. draw a 43 cm long line on the floor

you make myvariable a variable (depending on the time on your watch) and 43 is a constant. Other examples of variables that one could modify on the cookie recipe bellow, given as an example of algorithm, are the amounts of sugar, eggs, etc.

Loops: loops are parts of a program that are repeated. If one has an algorithm like:

1. through a stick 10 cm long in the air.
2. Wherever it falls on the floor:
3. draw a line 5 cm long at 20 degrees,
4. draw a line 10cm long at 50 degrees,
5. draw a line 4cm long at 90 degrees,
6. repeat from line 1. until the floor is full of lines

There is a loop from line 6 to line 1. Loops are an essential aspect of thinking in algorithms and processes of making. One can easily identify 'loops' in the recipe example too, in which things are repeated until something happens (something becomes creamy, or fluffy... A simple loop could be also:
read the above paragraph 25 times.
which could be written to as:
Keep on reading the paragraph above until you have read it 25 times.

Conditions:
1. Start walking out of the Architecture School in a straight line.
2. If you can't go straight turn left.
3. If the wind blows from the right, turn right.

small tasks for workshop:
________________________________________________________________________
Wednesday Afternoon:
In the morning, we have seen how to turn a simple LED on or off, and use time delays. For getting familiar with Arduino, we propose three tasks for you to do:

Task 1:
Make first the intervals of the delays in the blinking LED to increase from 0, by one milisecond every time. You can try to make it increase by 10 afterwards, or make the intervals become twice as big everytime.

Task2:
Connect 2 or 3 LEDs. Make them turn on and off in different patterns.

Task3:
You may have realised that when the delays are very small you cannot see the LED blinking, but instead its intensity changes. Can you find a way to control the intensity of the LED through this? This task will be an introduction to analog output, which we will be looking at tomorrow.


examples of algorithms:
________________________________________________________________________
1.LOGO: -

An example of an algorithm in LOGO, a computer language for children developed by Seymour Papert at MIT during the late sixties. If one follows the the steps you would walk in a square (forward means walk forward a number of steps, right means turn right a number of degrees) .

forward 100
right 90
forward 100
right 90
forward 100
right 90
forward 100
right 90

it could also be written as:
repeat 4
[
forward 100
right 90
]

LOGO instructions are generally executed by a robot on a paper, or by a simulated 'robot' on the screen.


F rom: www.bfoit.org
More information at the logo foundation.



-2.Recipe for chocolate chip cookies:
________________________________________________________________________

INGREDIENTS:

* 1 cup butter flavored shortening
* 3/4 cup white sugar
* 3/4 cup brown sugar
* 2 eggs
* 2 teaspoons Mexican vanilla extract
* 2 1/4 cups all-purpose flour
* 1 teaspoon baking soda
* 1 teaspoon salt
* 2 cups milk chocolate chips

DIRECTIONS:

1. Preheat oven to 350 degrees F (175 degrees C). Grease cookie sheets.

2. In a large bowl, cream together the butter flavored shortening, brown sugar and white sugar until light and fluffy. Add the eggs one at a time, beating well with each addition, then stir in the vanilla .Combine the flour, baking soda and salt; gradually stir into the creamed mixture. Finally, fold in the chocolate chips. Drop by rounded spoonfuls onto the prepared cookie sheets.

3. Bake for 8 to 10 minutes in the preheated oven, until light brown. Allow cookies to cool on baking sheet for 5 minutes before removing to a wire rack to cool completely.
From: allrecipes.com



-3.Postscript code:
________________________________________________________________________

%!PS
% program for drawing a number of circles

/doACircle
{ 0 0 100 0 360 arc stroke } def

0 1 100 {0 20 translate doACircle } for

showpage

 

If you want to see what the code above does, open a text editor (like textpad in windows) copy the code above and paste it in the text file, save it as whatevername.ps (*.ps extension) and open it in Adobe illustrator or any other program that can read postscript files.



-3.Arduino algorithm (from Arduino site)
________________________________________________________________________

/* Blinking LED
* ------------
*
* turns on and off a light emitting diode(LED) connected to a digital
* pin, in intervals of 2 seconds. Ideally we use pin 13 on the Arduino
* board because it has a resistor attached to it, needing only an LED

*
* Created 1 June 2005
* copyleft 2005 DojoDave <http://www.0j0.org>
* http://arduino.berlios.de
*
* based on an orginal by H. Barragan for the Wiring i/o board
*/

int ledPin = 13; // LED connected to digital pin 13

void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}

void loop()
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
}


-4.flowchart diagram:
________________________________________________________________________
Flowchart from Douglas Hopfstader, Gödel,Escher Bach, an Eternal Golden Braid. A flow char is anothre form of representing algorithms, speciallyinteresting for showing processes that imply decisions and repetitions.