Stap 13: Arduino en touchscreen
Als u geïnteresseerd in aanraking interfaces bent, er is een relatief goedkoop ($8 + $4 connector) resistive touchscreen of (de duurdere) trackpad van Adafruit dat x en y positie en druk zodat kunt u projecten als deze:
Het scherm van de Aanraking verbinden met de Arduino met vier pinnen geëtiketteerd X +, X-, Y + en Y-; de pin aansluitingen zijn aangegeven in de onderstaande code. Ik gewend van Adafruit touch scherm bibliotheek interface met het scherm (hier is hoe te het toevoegen van een bibliotheek voor de Arduino). Hier is een eenvoudige schets aan de slag:
#include <stdint.h> #include "TouchScreen.h" int YPlus = A0; // must be an analog pin int XMinus = A1; // must be an analog pin int YMinus = 8; // can be a digital pin int XPlus = 9; // can be a digital pin // For better pressure precision, we need to know the resistance // between X+ and X-, Use any multimeter to read it // For the one I'm using, it's 654 ohms TouchScreen ts = TouchScreen(XPlus, YPlus, XMinus, YMinus, 654); void setup(void) { Serial.begin(9600); } void loop(void) { // a point object holds x y and z coordinates Point p = ts.getPoint();//get data from screen if (p.z > ts.pressureThreshhold) {//if pressure is above a certain threshold Serial.print("X = "); Serial.print(p.x); Serial.print(" Y = "); Serial.print(p.y); Serial.print(" Pressure = "); Serial.println(p.z); } delay(100); }
Hier is een echt eenvoudige MIDI-implementatie van de Kaossilator, mapping x positie te merken en y-positie aan snelheid:
#include <stdint.h> #include "TouchScreen.h" byte noteON = 144;//note on command int YPlus = A0; // must be an analog pin, use "An" notation! int XMinus = A1; // must be an analog pin, use "An" notation! int YMinus = 8; // can be a digital pin int XPlus = 9; // can be a digital pin // For better pressure precision, we need to know the resistance // between X+ and X- Use any multimeter to read it // For the one we're using, its 654 ohms across the X plate TouchScreen ts = TouchScreen(XPlus, YPlus, XMinus, YMinus, 654); void setup(void) { Serial.begin(9600); } void loop(void) { // a point object holds x y and z coordinates Point p = ts.getPoint(); // we have some minimum pressure we consider 'valid' // pressure of 0 means no pressing! if (p.z > ts.pressureThreshhold) { byte note = map(p.x, 0, 1023, 0, 127); byte velocity = map(p.y, 0, 1023, 0, 127); MIDImessage(noteON, note, velocity); delay(100); MIDImessage(noteON, note, 0);//turn note off } } //send MIDI message void MIDImessage(byte command, byte data1, byte data2) { Serial.write(command); Serial.write(data1); Serial.write(data2); }
En hier is een video: