Arduino Leonardo

Arduino Multi-function Shield Part 1

Basic Input / Output

This is Part 1 of the Applied Hackatronics Series for the Arduino Multi-function shield, which shows how to use the shield library to access the multi-function shield buttons, buzzer and display. If you haven’t already done so, you’ll need to download the source code and install the libraries using the links in the introduction.

By following the Hackatronics series, you agree to do so at your own risk, and agree to take full responsibility for any loss or damages you may incur upon yourself or others.

Using the shield’s beeper

The multi-function shield library provides a flexible way to sound different types of alarms using the beeper. The actual timing and sounding of the beeper is controlled in the background using interrupts, which means your application can continue to focus on performing its main task.

#include <TimerOne.h>
#include <Wire.h>
#include <MultiFuncShield.h>

void setup() {
// put your setup code here, to run once:

Timer1.initialize();
MFS.initialize(&Timer1); // initialize multi-function shield library

// NOTE beep control is performed in the background, i.e. beep() is non blocking.

// short beep for 200 milliseconds
MFS.beep();

delay(1000);

// 4 short beeps, repeated 3 times.
MFS.beep(5, // beep for 50 milliseconds
5, // silent for 50 milliseconds
4, // repeat above cycle 4 times
3, // loop 3 times
50 // wait 500 milliseconds between loop
);
}

void loop() {
// put your main code here, to run repeatedly:
}

Detecting button presses on the shield

With the multi-function shield library, different types of button presses can be detected: short press, long press, button release after short press, button release after long press. The sketch below displays the type of button press in the serial monitor window. Check what happens you press and or hold multiple buttons together, and for different durations.

#include <TimerOne.h>
#include <Wire.h>
#include <MultiFuncShield.h>

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Timer1.initialize();
  MFS.initialize(&Timer1);    // initialize multi-function shield library
}

void loop() {
  // put your main code here, to run repeatedly:

  byte btn = MFS.getButton(); // Normally it is sufficient to compare the return
                              // value to predefined macros, e.g. BUTTON_1_PRESSED,
                              // BUTTON_1_LONG_PRESSED etc.
  
  if (btn)
  {
    byte buttonNumber = btn & B00111111;
    byte buttonAction = btn & B11000000;
    
    Serial.print("BUTTON_");
    Serial.write(buttonNumber + '0');
    Serial.print("_");
    
    if (buttonAction == BUTTON_PRESSED_IND)
    {
      Serial.println("PRESSED");
    }
    else if (buttonAction == BUTTON_SHORT_RELEASE_IND)
    {
      Serial.println("SHORT_RELEASE");
    }
    else if (buttonAction == BUTTON_LONG_PRESSED_IND)
    {
      Serial.println("LONG_PRESSED");
    }
    else if (buttonAction == BUTTON_LONG_RELEASE_IND)
    {
      Serial.println("LONG_RELEASE");
    }
  }
}

Writing to the shield’s digit display

The management of the multi-function shield’s digit display is performed in the background using interrupts, which means your application can continue to focus on performing its main task. String, integer and float values are written to the display as demonstrated in the sketch below:

#include <TimerOne.h>
#include <Wire.h>
#include <MultiFuncShield.h>

void setup() {
  // put your setup code here, to run once:
  
  Timer1.initialize();
  MFS.initialize(&Timer1);    // initialize multi-function shield library
  
  MFS.write("Hi");
  delay(2000);
  MFS.write(-273);
  delay(2000);
  MFS.write(3.141, 2);  // display to 2 decimal places.
  delay(2000);
}

int counter=0;
byte ended = false;

void loop() {
  // put your main code here, to run repeatedly:

  if (counter < 200)
  {
    MFS.write((int)counter);
    counter++;
  }
  else if (!ended)
  {
    ended = true;
    MFS.write("End");
    MFS.blinkDisplay(DIGIT_ALL, ON);
  }
  delay(50);
}

Controlling the shield’s LED lights

Although it isn’t strictly necessary to use the multi-function shield library to control the LED lights of the shield, support is provided in cases where your application needs the LEDs to perform basic blink operations. Blinking is managed in the background using interrupts.

#include <TimerOne.h>
#include <Wire.h>
#include <MultiFuncShield.h>

void setup() {
  // put your setup code here, to run once:
  
  Timer1.initialize();
  MFS.initialize(&Timer1);    // initialize multi-function shield library
  
  MFS.writeLeds(LED_ALL, ON);
  delay(2000);
  MFS.blinkLeds(LED_1 | LED_2, ON);
  delay(2000);
  MFS.blinkLeds(LED_1 | LED_2, OFF);
  
  MFS.blinkLeds(LED_3 | LED_4, ON);
  delay(2000);
  MFS.blinkLeds(LED_ALL, ON);
  delay(2000);
  MFS.blinkLeds(LED_ALL, OFF);
  MFS.writeLeds(LED_ALL, OFF);
}

void loop() {
  // put your main code here, to run repeatedly:

}

Reading the value of the shield’s potentiometer

This sketch demonstrates how the value of the preset pot is read and displayed on the multi-function shield. After uploading this sketch, turn the screw of the potentiometer to see the reading change on the digit display.

#include <TimerOne.h>
#include <Wire.h>
#include <MultiFuncShield.h>

void setup() {
  // put your setup code here, to run once:

  Timer1.initialize();
  MFS.initialize(&Timer1);    // initialize multi-function shield library
}

void loop() {
  // put your main code here, to run repeatedly:

  MFS.write(analogRead(POT_PIN));
  
  delay(100);
}

All the code samples and applications have been tested and work. If you experience any difficulties, please leave a comment, and we’ll get back to you as soon as we can.

Share on LinkedInShare on Google+Tweet about this on TwitterShare on Facebook

6 thoughts on “Arduino Multi-function Shield Part 1

  1. Jukka

    Reading the value of the shield’s potentiometer

    Any idea why value drifts so much in this sketch? On serial monitor drift is almost nonexistent.

    Reply
    1. Kashif Baig Post author

      Not sure why you’re experiencing this. What happens if you do multiple reads of potentiometer before outputting value?

      Reply
      1. Jukka

        I think you library is too much strain on my Duemillanove. I tried without your library just using shiftout function and it works perfectly.

        Reply
        1. Yuri

          I stored the analog readout first in a float, then output to MFS. Very stable. it does however matter if you use the MFS.write or not, by 1-2%.

          Reply
  2. Yuri

    for some reason my display holds the upper right part constantly lit, so that 5 reads as 8, 6 as 9 etc. is that a malfunction of the board?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *