Lesson 5: Varying Colours with Analog Sensor Changes


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 change the colour of your displays automatically when certain variables, e.g. sensor readings, change. This is very useful to help you interpret sensor readings.

For more information about available colour options, please refer to Lesson 4: Changing Colours.


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 create a display to show you how you can
// change the colours of your display boxes according to the 
// sensor readings
AndeeHelper colourDisplay;

// We'll use Analog Input Pin A0 to read in the analog input
const int analogInputPin = A0;


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


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

void setInitialData()
  // Let's draw colourDisplay first!
  colourDisplay.setId(0);  // Each object must have a unique ID number
  colourDisplay.setType(DATA_OUT);  // This defines your object as a display box
  colourDisplay.setLocation(0, 0, FULL); // Sets the location and size of your object
  colourDisplay.setTitle("Colour Change"); // Sets title
  colourDisplay.setData(""); // We'll update it with new analog data later.


The code below will change the colour (and title colour) of the display box to either green, yellow or red, depending on the sensor input.


void loop()
  // Read values from analog pins and store it in int variables
  int reading = analogRead(analogInputPin); 
  if(reading < 300) // Feel free to change the value
    colourDisplay.setColor(GREEN); // Sets background colour
    Or you can use the ARGB colour code to set your own colour, in the form:
    discreteDisplay.setColor("FF00FF00"); */
    colourDisplay.setTextColor(BLACK); // Sets font colour. 
    colourDisplay.setTitleColor(GREEN); // Sets title background colour
    colourDisplay.setTitleTextColor(BLACK); // Sets title font colour
  else if(reading < 350) // Feel free to change the value
  colourDisplay.setData(reading); // Set the display box with new data value
  colourDisplay.update(); // Update the display to show the new value
  // A short delay is necessary to give Andee time to communicate with the smartphone
comments powered by Disqus