Lesson 7: Making a Toggle Button


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
A toggle button is a button that changes between two states when pressed, e.g. On and off.

There are times where you'd want the same button to start/stop a task, or to switch on/off a device, e.g.:


== 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


Here's how you do it!


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 togglebutton;

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


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

void setInitialData()
  // Let's draw a toggle button
  togglebutton.setId(0); // Don't forget to assign a unique ID number
  togglebutton.setType(BUTTON_IN); // Defines object as a button
  togglebutton.setTitle("Turn On"); // 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()
  // Here's how you code the button action
  if( togglebutton.isPressed() )
    // Prevent user from accidentally pressing the button again
    // until Arduino has sent an acknowledgement 
    if(state == 0)
      togglebutton.setTitle("Turn off");
      state = 1; // Change state 
      // Add additional actions here to turn on something
      togglebutton.setTitle("Turn on");
      state = 0; // Change state 
      // Add additional actions here to turn off something
  togglebutton.update(); // Update your button to reflect the change in state

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