A Quick Introduction to Microcontrollers and the Arduino

Here’s my 40 minute introduction to microcontrollers and the Arduino.

Here’s the code I used for the first example of lighting an LED.

/*
 Light a LED.

 This example shows how to light an LED.

 This example code is in the public domain.
 */

int redled = 9; // the pin that the LED is attached to
int greenled = 10;
int blueled = 11;
int led = redled;

int brightness = 255; // how bright the LED is

// the setup routine runs once when you press reset
void setup() {
 pinMode(led, OUTPUT);
 // set the brightness of the LED
 analogWrite(led, 255 - brightness);
}

void loop() {}

Here’s the code for the second example of brightening and dimming a LED.

/*
 Fade

 This example shows how to fade an LED.  Based on Arduino code.

 This example code is in the public domain.
 */

int redled = 9;         // the pin that the LED is attached to
int greenled = 10;
int blueled = 11;

int led = greenled;
int brightness = 0;      // how bright the LED is, initially
int fadeAmount = 5;      // how many points to fade the LED by

// the setup routine runs once when you press reset
void setup()  {
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever
void loop()  { 
  // set the brightness of the LED
  analogWrite(led, 255 - brightness);

  // reverse the direction of the fading at the ends of the fade
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount; 
  }

  // change the brightness for next time through the loop:
  brightness -= fadeAmount;

  // wait for 30 milliseconds to see the dimming effect    
  delay(30);
}

Here’s the code for brightening and dimming all LEDs in a tricolour LED package.

/*
 Fade

 This example shows how to fade a tricolour LED device.  Based on Arduino code.

 This example code is in the public domain.
 */

int redled = 9;         // the pin that the LED is attached to
int greenled = 10;
int blueled = 11;

int brightness = 0;      // how bright the LED is, initially
int fadeAmount = 5;      // how many points to fade the LED by

// the setup routine runs once when you press reset
void setup()  {
  pinMode(redled, OUTPUT);
  pinMode(greenled, OUTPUT);
  pinMode(blueled, OUTPUT);
}

// the loop routine runs over and over again forever
void loop()  { 
  // set the brightness of the LEDs
  analogWrite(redled, 255 - brightness);
  analogWrite(greenled, brightness);
  analogWrite(blueled, 255 - brightness);

  // reverse the direction of the fading at the ends of the fade
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount; 
  }

  // change the brightness for next time through the loop:
  brightness -= fadeAmount;

  // wait for 30 milliseconds to see the dimming effect    
  delay(30);
}

Here’s the code for colour-cycling on a tri-colour LED:

/*
 Fade

 This example shows how to colour-cycle a tricolour LED device.

 This example code is in the public domain.
 */

int redled = 9;         // the pin that the LED is attached to
int greenled = 10;
int blueled = 11;

int hue = 0;            // colour
int saturation = 255;   // purity
int value = 255;        // intensity

int fadeAmount = 1;      // how many points to fade the LED by

// Convert HueSaturationValue to RGB
void hsvToRGB(int h, int s, int v, int *r, int *g, int *b) {
    unsigned int region, remainder, p, q, t;

    if (s == 0) {
        *r = v; *g = v; *b = v;
        return;
    }
    region = h / 43;
    remainder = (h - (region * 43)) * 6; 
    p = (v * (255 - s)) >> 8;
    q = (v * (255 - ((s * remainder) >> 8))) >> 8;
    t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8;

    switch (region) {
        case 0: *r = v; *g = t; *b = p; break;
        case 1: *r = q; *g = v; *b = p; break;
        case 2: *r = p; *g = v; *b = t; break;
        case 3: *r = p; *g = q; *b = v; break;
        case 4: *r = t; *g = p; *b = v; break;
        default: *r = v; *g = p; *b = q; break;
    }
}

// the setup routine runs once when you press reset:
void setup()  {
  pinMode(redled, OUTPUT);
  pinMode(greenled, OUTPUT);
  pinMode(blueled, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop()  {   
  int r, g, b;

  hsvToRGB(hue, saturation, value, &r, &g, &b);

  // set the brightness of the LEDs
  analogWrite(redled, 255 - r);
  analogWrite(greenled, 255 - g);
  analogWrite(blueled, 255 - b);

  // change the hue for next time through the loop:
  hue += fadeAmount;
  if (hue > 255)
    hue = 0;

  // wait for 30 milliseconds to see the dimming effect    
  delay(30);
}

Finally, here’s the code for driving 7-segment LED displays:

/*
 * 7-segment display demo.
 *
 * Copyright (c) 2014 Dave Voorhis <d.voorhis@derby.ac.uk>
 * Free for any use.
 *
 */

// 7-segment display (with decimal point).
class SevenSegment
{
  private:
    // True if common anode; false if common cathode
    boolean commonAnode;

    // high/low states dependent on common anode or common cathode
    int state1;
    int state2;  

    // What pins activate the segments?
    int *segmentPins;

    // What pins activate the 7segment displays?  One array entry per digit.
    int *digitPins;

    // Display values.  One array entry per digit.
    unsigned int *values;

    // How many digits?
    int numberOfDigits;

    // Last lit digit (by pin number)
    int lastLit;

    // Next digit to display (by digit number)
    int nextToDisplay;

    // Activate or deactivate a 7 segment display
    void binaryTo7Segment(int digit, unsigned int value);

  public:
    // 1          __
    //           / /
    //           -
    //         /_/ *
    //
    // 2          __
    //           * /
    //           -
    //         /_/ .
    //
    // 4           *
    //           / /
    //           - 
    //         /_/ .
    //
    // 8          __
    //           / *
    //           - 
    //         /_/ .
    //
    // 16         __
    //           / /
    //           -
    //         /*/ .
    //
    // 32         __
    //           / /
    //           -
    //         *_/ .
    //
    // 64         __
    //           / /
    //           - 
    //         /_* .
    //    
    // 128        __
    //           / /
    //           * 
    //         /_/ .
    //    

    // ctor
    SevenSegment(int _segmentPins[8], int _digitPins[], boolean _commonAnode, unsigned int _values[], int _size);

    // refresh display
    void refresh();
};

SevenSegment::SevenSegment(int _segmentPins[8], int _digitPins[], boolean _commonAnode, unsigned int _values[], int _size) 
{
    segmentPins = _segmentPins;
    digitPins = _digitPins;
    commonAnode = _commonAnode;
    values = _values;
    numberOfDigits = _size;
    state1 = (commonAnode) ? HIGH : LOW;
    state2 = (commonAnode) ? LOW : HIGH;  
    for (int i=0; i<=numberOfDigits; i++) {
      int digitPin = digitPins[i];
      pinMode(digitPin, OUTPUT);
      digitalWrite(digitPin, state2);
    }
    for (int i=0; i<8; i++) {
      int segmentPin = segmentPins[i];
      pinMode(segmentPin, OUTPUT);
      digitalWrite(segmentPin, state1);
    }
    lastLit = -1;
    nextToDisplay = 0;
}

// Activate or deactivate a 7 segment display
void SevenSegment::binaryTo7Segment(int digit, unsigned int value) {
  // turn out the most recently lit display
  if (lastLit >= 0)
    digitalWrite(lastLit, state2);
  // get the display we might light
  int thisDigitPin = digitPins[digit];
  // if value > 0, we light the display.  Otherwise, turn it off.
  if (value) {
    // use the value as a bit pattern to determine which segments to light
    for (int i=0; i<8; i++) {
      int bitOn = value & (((unsigned int)1) << i);
      // turn on segments corresponding to set bits
      digitalWrite(segmentPins[i], (bitOn) ? state2 : state1);
    }
    digitalWrite(thisDigitPin, state1);
    lastLit = thisDigitPin;
  } else {
    // value is zero, so turn off the display
    digitalWrite(thisDigitPin, state2);
    // last lit display is now none of them
    lastLit = -1;
  }
}

// Refresh display
void SevenSegment::refresh() {
  binaryTo7Segment(nextToDisplay, values[nextToDisplay]);
  if (++nextToDisplay >= numberOfDigits)
    nextToDisplay = 0;
}

// Array to convert hexadecimal value to 7 segment value
int decimal_to_7segment[] = {
  2 + 4 + 8 + 16 + 32 + 64,         // 0
  8 + 64, 
  4 + 8 + 16 + 32 + 128, 
  4 + 8 + 16 + 64 + 128, 
  2 + 8 + 64 + 128, 
  2 + 4 + 16 + 64 + 128, 
  2 + 4 + 16 + 32 + 64 + 128, 
  4 + 8 + 64, 
  2 + 4 + 8 + 16 + 32 + 64 + 128,
  2 + 4 + 8 + 16 + 64 + 128,        // 9
  2 + 4 + 8 + 32 + 64 + 128,        // A
  2 + 16 + 32 + 64 + 128,           // b
  2 + 4 + 16 + 32,                  // C
  8 + 16 + 32 + 64 + 128,           // d
  2 + 4 + 16 + 32 + 128,            // E
  2 + 4 + 32 + 128                  // F
};

// What pins connect to the segments?  There must be 8, including the decimal point.
int displaySegmentPins[8] = {8, 3, 4, 5, 6, 7, 9, 2};

// What pins connect to the common anode or cathode?  One array entry per digit.
int displayAnodePins[] = {11, 12};

// An array of 8-bit values that define what is displayed.  Each bit corresponds to a segment.  One array entry per digit.
unsigned int displayValues[] = {0, 0};

// Create a 2-digit 7-segment display.
SevenSegment display(displaySegmentPins, displayAnodePins, true, displayValues, 2);

void setup() {
}

int counter = 0;

int index1 = 0;
int index2 = 0;

void loop() {
  display.refresh();
  if (++counter == 20) {
    displayValues[1] = decimal_to_7segment[index1];
    displayValues[0] = decimal_to_7segment[index2];
    if (++index1 > 15) {
      index1 = 0;
      if (++index2 > 15)
        index2 = 0;
    }
    counter = 0;
  }
  delay(200);
}
This entry was posted in Uncategorized. Bookmark the permalink.