IFD:Interaktive Elektronik 2012/Arduino programming beginners: Difference between revisions

From Medien Wiki
mNo edit summary
Line 35: Line 35:


=== using variables ===
=== using variables ===
variables are a place to store numbers in the memory. They make life easier in many cases:
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 pin is much less tedious and cumbersome.
replace pin number by a variable - changing the pin is much less tedious and cumbersome.
replace delay by a variable - doesn't seem useful first. but:
replacing the delay by a variable - only change it once.


Changing variables during runtime (while the program is running) is also possible:
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'''
'''Having some random fun'''
Line 62: Line 71:
<source lang="c"> delaytime = 200 </source>
<source lang="c"> delaytime = 200 </source>
Now the value of delaytime is 200.
Now the value of delaytime is 200.


=== a new kind of output: ''analogWrite'' ===
=== a new kind of output: ''analogWrite'' ===

Revision as of 10:37, 10 May 2012

Using an LED on pin 13
Use an LED on pin 3


Use a Potentiometer and LED


We covered in the first lesson:

  • 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.


Today:

  • introduce the concept of variables.
  • introduce the "if" condition.
  • introduce functions.

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. valid names for variables: otto, lemonade24 and pudding not allowed names for variables: ööö äää 1234Otto (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);