|
|
arduino
programming.
________________________________________________________________________
Sensing Matter
is a workshop exploring rule-based design and assembly of wooden structures. Pablo Miranda Carranza and Åsmund Gamlesæter with Letizia Jaccheri, Knut Einar Larsen and Jose Cabral Filho. 25-29 Sept. 2006. The workshop is part of the course 1-2-TRE:6 at NTNU.
| introduction |
algorithm examples | NTNU code
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 C, one
of the most used language of the last 35 years. C and its derivation 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. Many other popular languages
such as Java, use a syntax (a way of writing the code by using {}, (),
; and the 'grammar' of the language) originated in C.
Learning C is out of the scope of our workshop; if you want to learn Arduino,
which is far more simple than C, you have most of the instructions you
can write here.
If you are interested, here are 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 recipe in this case
represents are the steps and performance of making the cake. An algorithm
can be thought as a sort of abstract machine (for conceptual
background check Turing
Machines). 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 writing
a program or abstract machine to simulate it: a telephone, a clock, a
burglars alarm, a traffic light, a door opener ...arduino can become these
and many more yet un thought-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.
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 another form of representing algorithms,
specially interesting for showing processes that imply decisions and repetitions.
ntnu
code:
________________________________________________________________________
Here is the basic code we will be using for reading sensors and using
theLED displays. We will also add schematics of the electronics and more
detailed code.
Sensor reading.
This code reads sensor values and outputs the result through the serial
port. You can open the serial to 'listen' to Arduino pressing the
Serial Monitor button. The baud rate, which is set in
Tools> Serial Monitor Baud Rate >
Should be set to the same that is in the code, in this case 19200.
/*
Analog input
reads an analog input on analog in 0, prints the value out.
*/
int analogValue = 0; // variable to hold the analog value
void setup() {
// open the serial port at 9600 bps:
Serial.begin(19200);
}
void loop() {
// read the analog input on pin 0:
analogValue = analogRead(0);
// print it out in many formats:
Serial.print("analog val: "); // print as an ASCII-encoded decimal
Serial.println(analogValue);
// delay 10 milliseconds before the next reading:
delay(20);
}
LED 7 segement digit display.
The following code is an example of how to display numbers using the LED
display. In the code below there are only some of the numbers implemented.
As an exercise you should develop a system to display numbers from 0 to
9.
/*
*TEST PROGRAM FOR LED DISPLAY
*Code initialy developed by Anna Tascha Larsson, Malin Henningsson and
Neal Fernandez, *as part of the Transformations course at Konsfack, 2006.
More code and explanations at:
*http://annamalinneal.blogspot.com/
*/
int number[][8] = { {1, 1, 1, 1, 1, 1, 0, 1}, {0, 1, 0, 1, 0, 0, 0, 0},
{1, 0, 1, 1, 1, 0, 1, 0}, {1, 1, 0, 1, 1, 0, 1, 0}, {0, 1, 0, 1, 0, 1,
1, 0}, {1, 1, 0, 0, 1, 1, 1, 1} } ;
int ledpins[] = {8, 10, 2, 3, 4, 5, 6, 7} ;
void setup()
{
for(int i = 0; i < 8; i++)
{
pinMode(ledpins[i], OUTPUT) ;
}
}
void loop()
{
for(int i = 0; i < 9; i++)
{
show(i) ;
delay(500) ; //wait a bit...
}
}
void show(int n) {
for(int i = 0; i < 8; i++) {
if (number[n][i] == 1) {
digitalWrite(ledpins[i], HIGH) ;
}
else {
digitalWrite(ledpins[i], LOW) ;
}
}
}
A version with a sensor input:
/*
*Code developed by Anna Tascha Larsson, Malin Henningsson and Neal Fernandez,
as part
*of the Transformations course at Konsfack, 2006. More code and explanations
at:
*http://annamalinneal.blogspot.com/
*/
int analogPin = 5; // potentiometer connected to analog pin 3
int val = 0; // variable to store the read value
int number[][8] = { {1, 1, 1, 1, 1, 1, 0, 1}, {0, 1, 0, 1, 0, 0, 0, 0},
{1, 0, 1, 1, 1, 0, 1, 0}, {1, 1, 0, 1, 1, 0, 1, 0}, {0, 1, 0, 1, 0, 1,
1, 0}, {1, 1, 0, 0, 1, 1, 1, 1} } ;
int ledpins[] = {8, 10, 2, 3, 4, 5, 6, 7} ;
void setup()
{
for(int i = 0; i < 8; i++) {
pinMode(ledpins[i], OUTPUT) ;
}
Serial.begin(9600);
}
void loop()
{
val = analogRead(analogPin); // read the input pin
Serial.print("the value of the sensor is: ");
Serial.print(val);
Serial.print("\n");
if (val>10)
{
val=10;
}
show(val);
delay(200) ; //wait a bit...
}
void show(int n) {
for(int i = 0; i < 8; i++) {
if (number[n][i] == 1) {
digitalWrite(ledpins[i], HIGH) ;
}
else {
digitalWrite(ledpins[i], LOW) ;
}
}
}
Sensor output through LED 7 segement digit
display.
The challenge here is how to translate the input from your particular
sensor in to the digital LED display. Think of the maximum number you
get as input from the sensor, if it is 1024 or 500, and how to scale it
to a number from 0 to 9.
|
|