Buttons Code

This guide has been updated to use the Adafruit IO Arduino Library

The previous version of this guide used the Adafruit MQTT Arduino Library, but we suggest using the Adafruit IO Arduino Library. If you'd like to use this library, we suggest following the MQTT, Adafruit IO & You! Learn Guide

The Arduino sketch for this project is fairly straight forward. Copy the following code into a new Arduino sketch. You'll need to modify your config.h file to reflect your Wireless Network and Adafruit IO configuration.

If you do not know how to do this, follow the steps on the the Arduino Setup page. 

Download: file
// Remote Control with the Huzzah + Adafruit IO
// 
// Button Board
// 
// Adafruit invests time and resources providing this open source code.
// Please support Adafruit and open source hardware by purchasing
// products from Adafruit!
//
// Written by Richard Albritton, based on original code by Tony DiCola for Adafruit Industries
// Licensed under the MIT license.
//
// All text above must be included in any redistribution.

/************************** Configuration ***********************************/

// edit the config.h tab and enter your Adafruit IO credentials
// and any additional configuration needed for WiFi, cellular,
// or ethernet clients.
#include "config.h"

/************************ Example Starts Here *******************************/
#include <ESP8266WiFi.h>

// Analog Pin on ESP8266
#define Buttons A0

// remote-buttons state
int ButtonRead = 0;
int current = 0;
int last = -1;

// set up the 'remote-buttons' feed
AdafruitIO_Feed *RemoteButtons = io.feed("remote-buttons");

void setup () {

  // start the serial connection
  Serial.begin(115200);

  // wait for serial monitor to open
  while(! Serial);

  // connect to io.adafruit.com
  Serial.print("Connecting to Adafruit IO");
  io.connect();

  // wait for a connection
  while(io.status() < AIO_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  // we are connected
  Serial.println();
  Serial.println(io.statusText());
  
}

void loop() {
  
  // io.run(); is required for all sketches.
  // it should always be present at the top of your loop
  // function. it keeps the client connected to
  // io.adafruit.com, and processes any incoming data.
  io.run();

  ButtonRead = analogRead(Buttons);
  delay(1);
  // grab the current state of the remote-buttons
  if (ButtonRead > 500 && ButtonRead < 600) {
    current = 1;
  }
  if (ButtonRead > 600 && ButtonRead < 750) {
    current = 2;
  }
  if (ButtonRead > 750 && ButtonRead < 900) {
    current = 3;
  }
  if (ButtonRead > 900) {
    current = 0;
  }

  // ret if value hasnt changed
  if(current == last)
    return;

  int32_t value = current;
  
  // let's publish stuff
  Serial.print("Sending RemoteButtons Value: ");
  Serial.print(value);
  Serial.print("...");

  RemoteButtons->save(value);
  delay(3000);
  
}

When you are finished reviewing the sketch and have finished making the necessary config changes, upload the sketch to your HUZZAH using the Arduino IDE. You should also open up your Adafruit IO dashboard so you can monitor the button gauge.

If everything goes as expected, you will see the gauge update on Adafruit IO. You should make sure to open the Arduino IDE's serial monitor if you are having issues with the sketch. It will provide valuable debugging info.

To add more buttons, just copy one of the conditional (if) statements and palce it in with the others. Each of the conditional statments reprisents a button that registers a press if the analog value is within a certain threshhold. You will need to set a minimum and maximum threshhold for all but the last button. The last button just needs to be grater than the maximum value of the button before it.

Download: file
 // grab the current state of the remote-buttons
    if (ButtonRead > 500 && ButtonRead < 600) {
      current = 1;
    }
    if (ButtonRead > 600 && ButtonRead < 750) {
      current = 2;
    }
    if (ButtonRead > 750 && ButtonRead < 900) {
      current = 3;
    }
    if (ButtonRead > 900) {
      current = 0;
    }

This sketch is set up to output the analog value of whatever button is pressed. You could figure out was the value of each button is with some math, but it is easier to just look at what the reading is using the serial monitor.  Give each button a wide birth to accommodate for any fluctuations.

Don't forget to add a new number to be set as the current value. Also be sure to set the Gauge so that it can accommodate the extra numbers.

Next, we will set up another HUZZAH to output the feed data to an RGB LED.

This guide was first published on Oct 11, 2015. It was last updated on Oct 11, 2015. This page (Buttons Code) was last updated on May 20, 2019.