Introduction to programming using Arduino part 2

From XinCheJian
Jump to: navigation, search

Background

You should be totally familiar with the Introduction to programming using Arduino. Please check that out if you aren't yet.


Your First For Loop

// Dim an LED using a PWM pin
int PWMpin = 10; // LED in series with 470 ohm resistor on pin 10

void setup()
{
  // no setup needed
}

void loop()
{
   for (int i=0; i <= 255; i++){
      analogWrite(PWMpin, i);
      delay(10);
   } 
}


Your First Switch Case

  switch (var) {
    case 1:
      //do something when var equals 1
      break;
    case 2:
      //do something when var equals 2
      break;
    default: 
      // if nothing else matches, do the default
      // default is optional
  }


Your First Array

int i;
for (i = 0; i < 5; i = i + 1) {
  Serial.println(myPins[i]);
}


Your First While Loop

var = 0;
while(var < 200){
  // do something repetitive 200 times
  var++;
}


Arduino Basic Input / Ouput Functions

pinMode()

Digital:

digitalWrite()

Digital: Writing

digitalRead()

Digital: Reading


analogReference()

Analsog

analogRead()

Analog: Reading

analogWrite() - PWM

Analog: Writing


Arduino Serial

Used for communication between the Arduino board and a computer or other devices. All Arduino boards have at least one serial port (also known as a UART or USART): Serial. It communicates on digital pins 0 (RX) and 1 (TX) as well as with the computer via USB. Thus, if you use these functions, you cannot also use pins 0 and 1 for digital input or output.


Arduino Stream

Stream is the base class for character and binary based streams. It is not called directly, but invoked whenever you use a function that relies on it.

Stream defines the reading functions in Arduino. When using any core functionality that uses a read() or similar method, you can safely assume it calls on the Stream class. For functions like print(), Stream inherits from the Print class.


Arduino Interrupts

Re-enables interrupts (after they've been disabled by noInterrupts()). Interrupts allow certain important tasks to happen in the background and are enabled by default. Some functions will not work while interrupts are disabled, and incoming communication may be ignored. Interrupts can slightly disrupt the timing of code, however, and may be disabled for particularly critical sections of code.


See also