IFD:Interaktive Elektronik 2012/Arduino programming beginners: Difference between revisions
mNo edit summary |
mNo edit summary |
||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[File:LED_Pin_13_Steckplatine.png]] | [[File:4_Buttons_Internal_Pullup_schem.png | thumb | Buttons without extra pull up resistors - be careful!]] | ||
[[File:LED_Pin_3_Steckplatine.png]]<br> | |||
[[File:LED_Pin_3_Potentiometer_A0_Steckplatine.png]]<br> | [[File:LED_Pin_13_Steckplatine.png | thumb | Using an LED on pin 13]] | ||
[[File:LED_Pin_3_Steckplatine.png | thumb | Use an LED on pin 3]]<br> | |||
[[File:LED_Pin_3_Potentiometer_A0_Steckplatine.png | thumb |Use a Potentiometer and LED]]<br> | |||
First Lesson, 2nd of May, 2012: | |||
* Arduino file format (pde and ino) | |||
* Functions of the buttons of the Arduino IDE | |||
* Opening the Serial monitor. | |||
* Going through the blink example. | |||
** setup: | |||
*** set pin 13 to output | |||
** loop: | |||
*** set pin 13 to be turned on | |||
*** wait one second | |||
*** set pin 13 to be turned off | |||
*** wait one second | |||
*** repeat the loop. | |||
<br clear="all"> | |||
2nd Lesson, 9th of May, 2012: | |||
[[/Variables_Random_analogWrite | Code + comments: Variables, Random and analogWrite ]]: (The code examples I made during class.) | |||
* introduce the concept of variables. | |||
* introduce 2 function: random() and analogWrite | |||
* if we are fast: analogRead() and Serial communication basics | |||
Introducing variables. | |||
* int (means an integer number: 0 1 2 3 4 5 6 ... and also negative numbers. limits: -32,768 to 32,767 | |||
* there are other kinds of variables. we only use int for simplicity in this lesson. | |||
** In your homework: see other types of variables. | |||
Names of Variables: | |||
valid names for variables: examples: otto, lemonade24, pudding, pinNumber, pin_Number | |||
not allowed names for variables: ööö äää 1234Otto (It must start with an alphanumeric character) | |||
better names for variables: delayTimeOff, pinNumber, delayTimeOn | |||
=== using variables === | |||
variables are a place to store numbers in the memory of a computer. They make life for us now: | |||
replace pin number by a variable - changing the pin is much less tedious and cumbersome. | |||
replacing the delay by a variable - only change it once. | |||
Changing variables during runtime (while the program is running) is also possible: | |||
'''Most simple example of changing a variable:''' | |||
<source lang="c"> | |||
myVariable = 100; | |||
delay(myVariable); | |||
myVariable = 1000; | |||
delay(myVariable); | |||
</source> | |||
'''Having some random fun''' | |||
<source lang="c">delayTime = random(1000);</source> | |||
will assign a random number from 1 to 1000. | |||
Some other useful ways to assign a new value to a variable: | |||
today we use: + - * | |||
increasing the delay time: | |||
<source lang="c"> | |||
delaytime = 100; | |||
delaytime = delaytime + 100; | |||
</source> | |||
in the first line delaytime gets the new value 100 | |||
in the second line something apparently strange happens - how is this possible? | |||
This line is not meaning a mathematical equality. The right side of the equals sign (=) is calculated first and then assigned to the left side of the equals sign. The following 3 steps happen - invisible for you inside the program when it's running: | |||
'''Step 1:''' The old delaytime is replaced by 100 | |||
<source lang="c"> delaytime = 100 + 100 </source> | |||
'''Step 2:''' The values on the right side of the equals sign are added and assigned to the variable: | |||
<source lang="c"> delaytime = 200 </source> | |||
Now the value of delaytime is 200. | |||
=== a new kind of output: ''analogWrite'' === | |||
Since we are working with LEDs a desirable feature would be to set the brightness. ''analogWrite'' lets us do this: <source lang="c"> analogWrite(pinNumber,Brightness); </source> | |||
or more generally since it is not only meant for brightness: | |||
<source lang="c"> analogWrite(pinNumber,analogValue); </source> |
Latest revision as of 11:18, 21 June 2012
First Lesson, 2nd of May, 2012:
- Arduino file format (pde and ino)
- Functions of the buttons of the Arduino IDE
- Opening the Serial monitor.
- Going through the blink example.
- setup:
- set pin 13 to output
- loop:
- set pin 13 to be turned on
- wait one second
- set pin 13 to be turned off
- wait one second
- repeat the loop.
- setup:
2nd Lesson, 9th of May, 2012:
Code + comments: Variables, Random and analogWrite : (The code examples I made during class.)
- introduce the concept of variables.
- introduce 2 function: random() and analogWrite
- if we are fast: analogRead() and Serial communication basics
Introducing variables.
- int (means an integer number: 0 1 2 3 4 5 6 ... and also negative numbers. limits: -32,768 to 32,767
- there are other kinds of variables. we only use int for simplicity in this lesson.
- In your homework: see other types of variables.
Names of Variables: valid names for variables: examples: otto, lemonade24, pudding, pinNumber, pin_Number not allowed names for variables: ööö äää 1234Otto (It must start with an alphanumeric character) better names for variables: delayTimeOff, pinNumber, delayTimeOn
using variables
variables are a place to store numbers in the memory of a computer. They make life for us now: replace pin number by a variable - changing the pin is much less tedious and cumbersome. replacing the delay by a variable - only change it once.
Changing variables during runtime (while the program is running) is also possible:
Most simple example of changing a variable:
myVariable = 100;
delay(myVariable);
myVariable = 1000;
delay(myVariable);
Having some random fun
delayTime = random(1000);
will assign a random number from 1 to 1000.
Some other useful ways to assign a new value to a variable: today we use: + - * increasing the delay time:
delaytime = 100;
delaytime = delaytime + 100;
in the first line delaytime gets the new value 100 in the second line something apparently strange happens - how is this possible? This line is not meaning a mathematical equality. The right side of the equals sign (=) is calculated first and then assigned to the left side of the equals sign. The following 3 steps happen - invisible for you inside the program when it's running:
Step 1: The old delaytime is replaced by 100
delaytime = 100 + 100
Step 2: The values on the right side of the equals sign are added and assigned to the variable:
delaytime = 200
Now the value of delaytime is 200.
a new kind of output: analogWrite
Since we are working with LEDs a desirable feature would be to set the brightness. analogWrite lets us do this:
analogWrite(pinNumber,Brightness);
or more generally since it is not only meant for brightness:
analogWrite(pinNumber,analogValue);