Lesson 9: Using Buttons to Control LED or Relays


Written by Jonathan Sim

You can find this lesson and more in the Arduino IDE (File -> Examples -> Andee). If you are unable to find them, you will need to install the Andee Library for Arduino IDE.

Works on iOS and Android
In this lesson, I'll show you how to easily control LEDs and relay switches with your smartphone!

Going one step further, I'll also show you how to check whether the LED/relay is switched on or off, and how to get your smartphone to change its existing state.


== Upgrade to the latest firmware

Annikken Andee for iOS - V2.3 (16 Mar 2016)

Annikken Andee U - V1.4 (16 Mar 2016) ==

This new upgrading will improve the button widget, now you can get you Arduino projects to respond faster when individual button is pressed.

See the difference in responsiveness with this demo Arduino sketch

Download example Here


Note for Arduino Due users: Please note that the Arduino Due does not allow you to read the state of your digital pins if they are already configured for output.


Top of the Code

Always include these libraries. Annikken Andee needs them to work with the Arduino!

#include <SPI.h>
#include <Andee.h>

// We'll just create one button to toggle
AndeeHelper displayState;
AndeeHelper togglebutton;

// Connect LED/Relay to Pin 2
// Do not use Pins 8, 11, 12, 13 (Arduino Uno layout) as Andee 
// is using them. Strange things will happen if you do.
const int outputPin = 2;

int state; // This variable will store the current state 


The setup() function is meant to tell Arduino what to do only when it starts up.

void setup()
  Andee.begin();  // Setup communication between Annikken Andee and Arduino
  Andee.clear();  // Clear the screen of any previous displays
  setInitialData(); // Define object types and their appearance
  state = 0; // Initialise your state to zero
  pinMode(outputPin, OUTPUT); // Configures outputPin for output.


This is the function meant to define the types and the appearance of all the objects on your smartphone

void setInitialData()
  // We're gonna create two objects. The first object, displayState, will
  // read the current state of the pin and report it to your smartphone.
  // The second object, togglebutton, will be used to turn the LED/relay
  // on or off.
  displayState.setId(0);// Don't forget to assign a unique ID number
  displayState.setTitle("Current Output State");
  togglebutton.setId(1); // Don't forget to assign a unique ID number
  togglebutton.setType(BUTTON_IN); // Defines object as a button
  togglebutton.setTitle("Switch to HIGH"); // Sets the initial words for button
  // You can't use setData() and setUnit() for buttons.


Arduino will run instructions here repeatedly until you power it off.

void loop()
  state = digitalRead(outputPin); // Read current state of the output pin
  // Change text in the display box to indicate the current state of the pin
  if(state == 0) // LOW state
  else // HIGH state
  // Here's how you code the button action
  if( togglebutton.isPressed() )
    togglebutton.ack(); // Acknowledge button press. You must put this or your phone will be waiting
    if(state == 0) // If in a LOW state
      togglebutton.setTitle("Switch to LOW");
      digitalWrite(outputPin, HIGH);
    else // If in a HIGH state
      togglebutton.setTitle("Switch to HIGH");
      digitalWrite(outputPin, LOW);

  displayState.update(); // Update new info onto smartphone screen 

  delay(500); // Always leave a short delay for Bluetooth communication
comments powered by Disqus