Created
August 29, 2017 16:37
-
-
Save JCash/210dbb6b02cba76dc7309f41375f8ba3 to your computer and use it in GitHub Desktop.
Arduino touch screen example using Nintendo 2DS screen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Touch screen example for arduino using a Nintendo 2ds touch screen | |
// Code is mostly inspired from http://tronixstuff.com/2010/12/29/tutorial-arduino-and-the-ds-touch-screen/ | |
// and the Adafruit touch screen library | |
#include <Wire.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
#define XM A0 | |
#define XP A2 | |
#define YM A3 | |
#define YP A1 | |
#define OLED_RESET 4 | |
Adafruit_SSD1306 display(OLED_RESET); | |
// From: http://tronixstuff.com/2010/12/29/tutorial-arduino-and-the-ds-touch-screen/ | |
// Returns [0,1023] from left to right on the touch screen | |
int readX() // returns the value of the touch screen's X-axis | |
{ | |
int xr=0; | |
pinMode(XM, INPUT); | |
pinMode(XP, INPUT); | |
pinMode(YP, OUTPUT); | |
pinMode(YM, OUTPUT); | |
digitalWrite(YP, LOW); // set to GND | |
digitalWrite(YM, HIGH); // set to 5V | |
delay(5); // short delay is required to give the analog pins time to adjust to their new roles | |
xr=analogRead(0); | |
return 1023-xr; | |
} | |
// Returns [0,1023] from top to bottom of the touch screen | |
int readY() | |
{ | |
int yr=0; | |
pinMode(XM, OUTPUT); | |
pinMode(XP, OUTPUT); | |
pinMode(YP, INPUT); | |
pinMode(YM, INPUT); | |
digitalWrite(XM, LOW); // set to GND | |
digitalWrite(XP, HIGH); // set to 5V | |
delay(5); // short delay is required to give the analog pins time to adjust to their new roles | |
yr=analogRead(1); | |
return yr; | |
} | |
// From: https://github.com/adafruit/Touch-Screen-Library/blob/master/TouchScreen.cpp | |
uint16_t readPressure() { | |
// Set X+ to ground | |
pinMode(XP, OUTPUT); | |
digitalWrite(XP, LOW); | |
// Set Y- to VCC | |
pinMode(YM, OUTPUT); | |
digitalWrite(YM, HIGH); | |
// Hi-Z X- and Y+ | |
digitalWrite(XM, LOW); | |
pinMode(XM, INPUT); | |
digitalWrite(YP, LOW); | |
pinMode(YP, INPUT); | |
int z1 = analogRead(0); | |
int z2 = analogRead(1); | |
int _rxplate = 300; // threshold | |
if (_rxplate != 0) { | |
// now read the x | |
float rtouch; | |
rtouch = z2; | |
rtouch /= z1; | |
rtouch -= 1; | |
rtouch *= readX(); | |
rtouch *= _rxplate; | |
rtouch /= 1024; | |
return rtouch; | |
} else { | |
return (1023-(z2-z1)); | |
} | |
} | |
void setup() { | |
Serial.begin(9600); | |
Wire.setClock(400000); | |
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); | |
display.clearDisplay(); | |
display.display(); | |
display.setTextSize(2); | |
display.setTextColor(WHITE); | |
} | |
int prevPressure = 0; | |
int writeString(int x, int y, const char* text) | |
{ | |
display.setCursor(x, y); | |
for(int i = 0; i < strlen(text); ++i) | |
display.write(text[i]); | |
} | |
void loop() { | |
int x = readX(); | |
int y = readY(); | |
uint16_t p = readPressure(); // can only detect touch or not (not sure if I've made any mistakes) | |
int touched = prevPressure == 0 && p > 10; | |
int released = prevPressure > 0 && p < 10; | |
prevPressure = p; | |
if(p < 10) | |
{ | |
x = -1; | |
y = -1; | |
} | |
char buffer[32]; | |
snprintf(buffer, sizeof(buffer), "X: %d", x); | |
writeString(0,0, buffer); | |
snprintf(buffer, sizeof(buffer), "Y: %d", y); | |
writeString(0,16, buffer); | |
float fx = x / 1023.0f; | |
float fy = y / 1023.0f; | |
int px = 64 + fx * 64; | |
int py = 0 + fy * 32; | |
display.drawPixel(px, py, WHITE); | |
display.drawPixel(px-1, py, WHITE); | |
display.drawPixel(px+1, py, WHITE); | |
display.drawPixel(px, py-1, WHITE); | |
display.drawPixel(px, py+1, WHITE); | |
display.display(); | |
display.clearDisplay(); | |
if( p > 10 ) | |
delay(33); | |
else | |
delay(200); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment