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 <MultiFuncShield.h>

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

MFS.initialize(); // 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 <MultiFuncShield.h>

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  MFS.initialize();    // 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 <MultiFuncShield.h>

void setup() {
  // put your setup code here, to run once:
  
  MFS.initialize();    // 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 <MultiFuncShield.h>

void setup() {
  // put your setup code here, to run once:
  
  MFS.initialize();    // 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 <MultiFuncShield.h>

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

  MFS.initialize();    // 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.

25 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
  3. Beth Robertson

    Is this capable of doing outputs to external LED’s? For example, if the speedometer is above a certain point, I want to light up an LED light strip.

    Reply
    1. Kashif Baig Post author

      Hi Beth, this functionality is not part of the source code, but would be easy to implement by someone with some programming experience.

      Reply
      1. Beth Robertson

        Thanks – I did a simple fix by placing it on a Mega so that I could utilize the extra pin space. Now are we are trying to figure out where to put a conversion to mph from Kph in the code. I saw a previous comment about developing the source code for us non-metric types. Is that available yet?

        Reply
  4. Dave

    I’ve tried several of the code sketches above and all I get is:
    exit status 1
    Error compiling for board Arduino Mega or Mega 2560.
    when I compile the code.

    Dave.
    (former professional IBM 370 programmer).

    Reply
    1. Kashif Baig Post author

      Hi Dave. Does any other sketch compile, and do you have other boards to try with?

      Reply
    2. Phil

      The sketches also require that the TimerOne library to be installed.
      This is not clear in the documentation but once installed, it solves the problem.

      Reply
      1. Kashif Baig Post author

        Hello Phil

        The most recent coding examples don’t require the TimerOne library. This has been mentioned in the Introduction, which also has the correct download links. I hope that helps.

        Reply
        1. Dave

          Yes they do. Contrary to the introduction the code doesn’t work until the TimerOne library is included.
          Problem solved, thanks Phil.

          Dave.

          Reply
          1. Kashif Baig Post author

            Lo and behold. There was indeed a left over reference to TimerOne in the library. As of this instance, it has been removed from the library. I hope that helps.

  5. Phil

    Is there a page showing all of the commands supported by the library and the correct syntax to use?

    Phil

    Reply
    1. Kashif Baig Post author

      All library methods can be found in MultiFuncShield.h. The different coding examples cover usage of most of the methods.

      Reply
  6. Kevin

    Hi – I’m new to Arduino and trying to land the beep example except I keep getting the following message
    exit status 1
    no matching function for call to ‘MultiFuncShield::initialize(TimerOne*)

    I have TimerOne, Wire and MultiFuncShield-Library an my Arduino library, any help most appreciated

    Reply
    1. Kevin

      Hi Sorry, I must have a had a brain freeze but the code I’m getting the message is for DslrIntervalometer!
      Error occurs in void setup line MFS.initialize(&Timer1); and displays error:
      exit status 1
      no matching function for call to ‘MultiFuncShield::initialize(TimerOne*)

      Kevin

      Reply
      1. Kashif Baig Post author

        Thanks for pointing this out Kevin. You can remove references to TimerOne as it is no longer required. The downloadable source code has now been modified with this change. Let us know if it works now. However, do make sure you have the latest Multifunction Shield Library too.

        Reply
        1. kevin

          Hey thats fantastic all working fine, what a service!
          I’ll let you know once I’ve built and tested the interface to my dslr.
          Loving all your other examples for the Multi Function shield.
          thanks
          Kevin

          Reply
          1. Kashif Baig Post author

            That’s good to know. At some point in the near future a version with a responsive web interface that allows control using WiFi will be available.

  7. nscruz

    theres 4 jumper pins on next tto switches ,how do i connect them? short them together.? my shields digits are not working. not lighting up.

    Reply
    1. nscruz

      never mind , i found a schematic diagram, they provide power for the push buttons and the external jumpers

      Reply

Leave a Reply

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