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

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


* introduce the concept of variables.
* introduce the concept of variables.
* introduce the "if" condition.
* introduce 2 function: random() and analogWrite
* introduce functions.
* if we are fast: analogRead() and  Serial communication basics


Introducing variables.
Introducing variables.
int (means an integer number: 0 1 2 3 4 5 6 ... and also negative numbers. limits: -32,768 to 32,767
* 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.
* there are other kinds of variables. we only use int for simplicity in this lesson.  
valid names for variables: otto, lemonade24 and pudding
** In your homework: see other types of variables.
not allowed names for variables: ööö äää 1234Otto (must start with an alphanumeric character)
 
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
better names for variables: delayTimeOff, pinNumber, delayTimeOn



Revision as of 10:45, 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 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);