Monday, January 13, 2014

Arduino Esplora example: read slider and display on TFT

The example run on Esplora, read from slider, and display on TFT.

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

const int MAX_W = 160;
const int MAX_H = 128;
unsigned long ul_MAX_H = (unsigned long)MAX_H;
const int MAX_SLIDER = 1023;

int xPos = 0; 
int yPos = 0;
int xPrev = 0;
int yPrev = MAX_H;
char printout[5];

void setup(){
  EsploraTFT.begin();  
  EsploraTFT.background(0,0,0);

  //preset dummy reading to print
  String dummy = "0";
  dummy.toCharArray(printout,5);
}

void loop(){
  int slider = Esplora.readSlider();

  //clear previous print of reading
  EsploraTFT.stroke(0,0,0);
  EsploraTFT.text(printout,0,10);
  //update reading
  String(slider).toCharArray(printout,5);
  EsploraTFT.stroke(255,255,255);
  EsploraTFT.text(printout,0,10);

  // update the location of the dot
  xPos++;
  //align y-position
  yPos = MAX_H - slider * ul_MAX_H/MAX_SLIDER;
  EsploraTFT.line(xPrev, yPrev, xPos, yPos);

  // if the x position reach screen edges, 
  // clear screen
  if(xPos >= MAX_W){
    EsploraTFT.background(0,0,0);
    xPos = 0;
  }

  // update the point's previous location
  xPrev=xPos;
  yPrev=yPos;

  delay(50);
}

No comments:

Post a Comment