The Arduino doesn't have enough horsepower to drive the screen but you can use the touch panel pretty easily. My idea was to stick paper behind the touch panel and draw an interface on it, and eventually have several "screens" on a single paper that moves behind the touch panel. Well at least I thought it was a clever idea...
My sketches are based on info from this Arduino + DS touch page.
Pretty simple right? Ok, getting the values is a little complicated. Nonetheless, it runs pretty quick and leaves plenty of time to do whatever you want with the results.
I took the touch input and used it to send a packet using the Arduino's ethernet shield. For this I had to move analog pins 0 and 1 because the ethernet shield used them for the SD card (even if you're not using the SD card it will change the values you read from the touch panel). I moved them to analog pins 4 and 5, respectively.
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>
/*
** WARNING: Ethernet Sheild uses analog pins 0 and 1 for the SD card
** Even if you're not using the SD card, there are 10k pull-ups
** So readings will be inaccurate
*/
/*******************************/
// Touch
const int xLow = 17;
const int xHigh = 19; // 15
const int yLow = 16;
const int yHigh = 18; // 14
int touchX;
int touchY;
// Ethernet
int eth_mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
int eth_ip[] = { 192,168,1,3 };
int eth_theserver[] = { 192,168,1,100 };
int eth_port = 12345;
EthernetClient client;
/*******************************/
void getTouchValues() {
//This function is the same, see top of this blog post
}
boolean process_touch() {
//Do whatever you want in here
}
/*******************************/
void setup(){
Serial.begin(9600);
Ethernet.begin(eth_mac, eth_ip);
Serial.println("connecting...");
if (client.connect(eth_theserver, eth_port)) {
Serial.println("connected");
} else {
Serial.println("connecting failed");
}
}
/*******************************/
void loop(){
getTouchValues();
Serial.print(touchX,DEC);
Serial.print(",");
Serial.println(touchY,DEC);
process_touch();
}Not too crazy. Calling getTouchValues() gets the touch position from the panel, then process_touch() handles it. You'll also notice I don't like to be consistent with the naming of my functions.
Here's the final script. You'll see I ignore the first few inputs. Probably more than I need to. It's because as the person is putting their full pressure on the touch panel it can read values that are lower than expected, registering a touch lower on the panel than expected. I also ignore incomplete inputs (X but no Y, or vice versa). I also want to ignore all touch until the user lifts their finger (so we don't get 999x inputs for one "button press"). For this, I ignore all touch inputs until the Arduino reads (0,0) from the touch panel again (which means nobody is touching it).
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>
/*
** WARNING: Ethernet Sheild uses analog pins 0 and 1 for the SD card
** Even if you're not using the SD card, there are 10k pull-ups
** So readings will be inaccurate
*/
/*
**
** X values range from 80-750
** Lower value as low as 50 at extreme edge
** Upper value slightly dependant on Y coord, but much more affected by pressure
**
** Y values range from 150-720
** Lower value 180 with finger
** Upper value slightly dependant on X coord (range from 680-720)
*/
/*******************************/
// Touch
const int xLow = 17;
const int xHigh = 19; // 15
const int yLow = 16;
const int yHigh = 18; // 14
int touchX;
int touchY;
boolean ignore_touch = false;
const int TOUCHES_TO_IGNORE = 2; // ignore first 2 touches (filters bad inputs)
int ignored_touches = 0;
// Ethernet
uint8_t eth_mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
uint8_t eth_ip[] = { 192,168,1,3 };
uint8_t eth_theserver[] = { 192,168,1,100 };
int eth_port = 12345;
boolean eth_connected = false;
EthernetClient client;
/*******************************/
void getTouchValues(int delay_before_read = 10) {
pinMode(xLow,OUTPUT);
pinMode(xHigh,OUTPUT);
digitalWrite(xLow,LOW);
digitalWrite(xHigh,HIGH);
digitalWrite(yLow,LOW);
digitalWrite(yHigh,LOW);
pinMode(yLow,INPUT);
pinMode(yHigh,INPUT);
delay(delay_before_read);
//analog pins are 0-5
touchX = analogRead(yLow -14);
pinMode(yLow,OUTPUT);
pinMode(yHigh,OUTPUT);
digitalWrite(yLow,LOW);
digitalWrite(yHigh,HIGH);
digitalWrite(xLow,LOW);
digitalWrite(xHigh,LOW);
pinMode(xLow,INPUT);
pinMode(xHigh,INPUT);
delay(delay_before_read);
//analog pins are 0-5
touchY = analogRead(xLow - 14);
}
void music_play_pause() {
Serial.println("Play/Pause");
client.print('1');
}
void music_stop() {
Serial.println("Stop");
client.print('4');
}
void music_next() {
Serial.println("Next");
client.print('2');
}
void music_prev() {
Serial.println("Previous");
client.print('3');
}
void music_playlist_local() {
Serial.println("Switching to Local Libray");
client.print('a');
}
void music_playlist_dm() {
Serial.println("Switching to DJ Mixes");
client.print('c');
}
void music_playlist_eh() {
Serial.println("Switching to Electro House");
client.print('b');
}
boolean process_touch() {
if ((touchX == 0) && (touchY == 0)) {
ignore_touch = false;
ignored_touches = 0;
return false;
} else if ((touchX == 0) || (touchY == 0)) {
// Ignore partial touch inputs
return false;
} else if (ignore_touch) {
return false;
} else {
if (ignored_touches < TOUCHES_TO_IGNORE) {
ignored_touches++;
return false;
}
}
ignore_touch = true;
// Process the touch
if (touchX <= 200) { // Next or Prev
if (touchY <= 400)
music_prev();
else
music_next();
}
else if ((touchX >= 250) && (touchX <= 400)) {
if ((touchY >= 280) && (touchY <= 520)) {
music_stop();
}
}
else if ((touchX >= 440) && (touchX <= 670)) {
if ((touchY >= 200) && (touchY <= 660)) {
music_play_pause();
}
}
else if ((touchX >= 700)) { // Playlists
if ((touchY < 250)) {
music_playlist_eh();
}
else if ((touchY >= 350) && (touchY <= 510)) {
music_playlist_dm();
}
else {
music_playlist_local();
}
}
return true;
}
/*******************************/
void setup(){
Serial.begin(9600);
Ethernet.begin(eth_mac, eth_ip);
pinMode(relay_pin, OUTPUT);
Serial.println("connecting...");
if (client.connect(eth_theserver, eth_port)) {
Serial.println("connected");
} else {
Serial.println("connection failed");
}
}
/*******************************/
void loop(){
getTouchValues();
// Serial.print(touchX,DEC);
// Serial.print(",");
// Serial.println(touchY,DEC);
process_touch();
}
No comments:
Post a Comment