Learning   Examples | Foundations | Hacking | Links

Examples > TFT

Esplora TFT Color Picker

This example for the Esplora with an Arduino screen reads the input of the joystick's two axes and the slider, using the values to change the screen's color.

Color on the TFT screen screen is handled as 8-bit numbers (0-255). However, the library scales these values to 5-bits (32 levels) for red and blue, 6-bits (64 levels) for green.

Hardware Required

  • Arduino Esplora
  • Arduino TFT screen

Circuit

Attach the screen to the socket on your Esplora, with the label "SD Card" facing up.

Code

To use the screen you must first include the SPI and TFT libraries. Don't forget to include the Esplora library as well.

#include <Esplora.h>
#include <GLCD.h>
#include <SPI.h>

In setup(), start serial communication so you can read the values used for the background color. Also, start the screen and make the background white.

void setup() {
  Serial.begin(9600);
  EsploraTFT.begin();
  EsploraTFT.background(255, 255, 255);
}

In loop(), read the values fron the joystick axes and slider, mapping them to values between 0-255. with background(), set the mapped background colors and print the values to the serial monitor.

void loop() {
  int xValue = map(Esplora.readJoystickX(), -512, 512, 0, 255);
  int yValue = map(Esplora.readJoystickY(), -512, 512, 0, 255);
  int slider = map(Esplora.readSlider(), 0, 1023, 0, 255);

  EsploraTFT.background(xValue, yValue, slider);

  Serial.print("background(");
  Serial.print(xValue);
  Serial.print(" , ");
  Serial.print(yValue);
  Serial.print(" , ");
  Serial.print(slider);
  Serial.println(")");

  delay(33);

}

The complete sketch is below :

/*

 Esplora TFT Color Picker
 
 This example for the Esplora with an Arduino TFT reads
 the input of the joystick and slider, using the values
 to change the screen's color.
 
 This example code is in the public domain.
 
 Created 15 April 2013 by Scott Fitzgerald
 
 http://arduino.cc/en/Tutorial/TFTColorPicker
 
 */


#include <Esplora.h>
#include <TFT.h>  // Arduino LCD library
#include <SPI.h>

void setup() {
  Serial.begin(9600);

  // initialize the LCD
  EsploraTFT.begin();

  // start out with a white screen
  EsploraTFT.background(255, 255, 255);

}

void loop() {

  // map the values from sensors
  int xValue = map(Esplora.readJoystickX(), -512, 512, 0, 255);  // read the joystick's X position
  int yValue = map(Esplora.readJoystickY(), -512, 512, 0, 255);  // read the joystick's Y position
  int slider = map(Esplora.readSlider(), 0, 1023, 0, 255);   // read the slider's position
 
  // change the background color based on the mapped values
  EsploraTFT.background(xValue, yValue, slider);
 
  // print the mapped values to the Serial monitor
  Serial.print("background(");
  Serial.print(xValue);
  Serial.print(" , ");
  Serial.print(yValue);
  Serial.print(" , ");
  Serial.print(slider);
  Serial.println(")");
 
  delay(33);

}

Share