I'm working on a touch screen step sequencer that will eventually be sending out MIDI data. I'm still working on drawing the graphics and creating a unique on/ off state for each square on my grid.
In terms of drawing and redrawing my grid I've been successful however, I'm using a bunch of nested if statements and I wonder if this could be done more efficiently somehow. Perhaps there is a way I could iterate through instead of having to write a section of code for each button... ? The section I'm talking about is in the bpress() function.
#include <stdint.h>
#include <TFTv2.h>
#include <SeeedTouchScreen.h>
#define ShowSerial Serial
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) // mega
#define YP A2 // must be an analog pin, use "An" notation!
#define XM A1 // must be an analog pin, use "An" notation!
#define YM 54 // can be a digital pin, this is A0
#define XP 57 // can be a digital pin, this is A3
#elif defined(__AVR_ATmega32U4__) // leonardo
#define YP A2 // must be an analog pin, use "An" notation!
#define XM A1 // must be an analog pin, use "An" notation!
#define YM 18 // can be a digital pin, this is A0
#define XP 21 // can be a digital pin, this is A3
#elif defined(ARDUINO_SAMD_VARIANT_COMPLIANCE) // samd21
#define YP A2 // must be an analog pin, use "An" notation!
#define XM A1 // must be an analog pin, use "An" notation!
#define YM A4 // can be a digital pin, this is A0
#define XP A3 // can be a digital pin, this is A3
#undef ShowSerial
#define ShowSerial SerialUSB
#else //168, 328, something else
#define YP A2 // must be an analog pin, use "An" notation!
#define XM A1 // must be an analog pin, use "An" notation!
#define YM 14 // can be a digital pin, this is A0
#define XP 17 // can be a digital pin, this is A3
#endif
//Measured ADC values for (0,0) and (210-1,320-1)
//TS_MINX corresponds to ADC value when X = 0
//TS_MINY corresponds to ADC value when Y = 0
//TS_MAXX corresponds to ADC value when X = 240 -1
//TS_MAXY corresponds to ADC value when Y = 320 -1
#define TS_MINX 116*2
#define TS_MAXX 890*2
#define TS_MINY 83*2
#define TS_MAXY 913*2
int counter1 = 0;
int row = 0;
int col = 0;
int grid[3][3];
// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// The 2.8" TFT Touch shield has 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM);
void setup(void) {
int gridLine=0;
int x = 0;
int y = 0;
ShowSerial.begin(9600);
TFT_BL_ON; // turn on the background light
Tft.TFTinit(); //init TFT library
/*
Tft.drawLine(0, 0, 239, 319, RED); //start: (0, 0) end: (239, 319), color : RED
Tft.drawVerticalLine(240/2, 1, 319, BLUE); // Draw a vertical line
// start: (60, 100) length: 100 color: blue
while(gridLine < 13){
Tft.drawHorizontalLine(1, 320/12*gridLine+4, 239, GREEN); //Draw a horizontal line
//start: (30, 60), high: 150, color: green
gridLine++;
} */
//draw grid
for(x = 0; x < 200; x = x+50){
for(y = 0; y < 150; y = y+50){
delay(10);
Tft.drawRectangle(x, y, 50, 50, BLUE);
}
delay(10);
Tft.drawRectangle(x, y, 50, 50, BLUE);
}
delay(10);
}
/*
void loop(void) {
for(row = 0; row < 200; row = row+50){
for(col = 0; col < 150; col = col+50){
delay(1000);
Tft.fillRectangle(row, col, 50, 50, BLUE);
}
delay(1000);
Tft.fillRectangle(row, col, 50, 50, BLUE);
}
delay(1000);
}
*/
void loop(void){
bpress();
}
//the func that registers pressing squares on the grid
void bpress(void){
// a point object holds x y and z coordinates
Point p = ts.getPoint();
//map raw values to pixel values
p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240);
p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320);
// we have some minimum pressure we consider 'valid'
// pressure of 0 means no pressing!
//display mapped pixel values instead of raw values
if (p.z > __PRESSURE) {
ShowSerial.print("X = "); ShowSerial.print(p.x);
ShowSerial.print("\tY = "); ShowSerial.print(p.y);
ShowSerial.print("\tPressure = "); ShowSerial.println(p.z);
//step 1
if(grid[0][0] == 0){
if(p.x <= 50 && p.x > 0 && p.y <=50 && p.y > 0){
Tft.fillRectangle(0, 0, 50, 50, BLUE);
grid[0][0] = 1;
}
} else if(grid[0][0] == 1){
if(p.x <= 50 && p.x > 0 && p.y <=50 && p.y > 0){
Tft.fillRectangle(0, 0, 50, 50, BLACK);
Tft.drawRectangle(0, 0, 50, 50, BLUE);
grid[0][0] = 0;
}
}
//step 2
if(grid[0][1] == 0){
if(p.x <= 100 && p.x > 50 && p.y <=50 && p.y > 0){
Tft.fillRectangle(50, 0, 50, 50, BLUE);
grid[0][1] = 1;
}
} else if(grid[0][1] == 1){
if(p.x <= 100 && p.x > 50 && p.y <=50 && p.y > 0){
Tft.fillRectangle(50, 0, 50, 50, BLACK);
Tft.drawRectangle(50, 0, 50, 50, BLUE);
grid[0][1] = 0;
}
}
//step 3
if(grid[0][2] == 0){
if(p.x <= 150 && p.x > 100 && p.y <=50 && p.y > 0){
Tft.fillRectangle(100, 0, 50, 50, BLUE);
grid[0][2] = 1;
}
} else if(grid[0][2] == 1){
if(p.x <= 150 && p.x > 100 && p.y <=50 && p.y > 0){
Tft.fillRectangle(100, 0, 50, 50, BLACK);
Tft.drawRectangle(100, 0, 50, 50, BLUE);
grid[0][2] = 0;
}
}
//step 4
if(grid[0][3] == 0){
if(p.x <= 200 && p.x > 150 && p.y <=50 && p.y > 0){
Tft.fillRectangle(150, 0, 50, 50, BLUE);
grid[0][3] = 1;
}
} else if(grid[0][3] == 1){
if(p.x <= 200 && p.x > 150 && p.y <=50 && p.y > 0){
Tft.fillRectangle(150, 0, 50, 50, BLACK);
Tft.drawRectangle(150, 0, 50, 50, BLUE);
grid[0][3] = 0;
}
}
//step 5
if(grid[1][0] == 0){
if(p.x <= 50 && p.x > 0 && p.y <=100 && p.y > 50){
Tft.fillRectangle(0, 50, 50, 50, BLUE);
grid[1][0] = 1;
}
} else if(grid[1][0] == 1){
if(p.x <= 50 && p.x > 0 && p.y <=100 && p.y > 50){
Tft.fillRectangle(0, 50, 50, 50, BLACK);
Tft.drawRectangle(0, 50, 50, 50, BLUE);
grid[1][0] = 0;
}
}
//step 6
if(grid[1][1] == 0){
if(p.x <= 100 && p.x > 50 && p.y <=100 && p.y > 50){
Tft.fillRectangle(50, 50, 50, 50, BLUE);
grid[1][1] = 1;
}
} else if(grid[1][1] == 1){
if(p.x <= 100 && p.x > 50 && p.y <=100 && p.y > 50){
Tft.fillRectangle(50, 50, 50, 50, BLACK);
Tft.drawRectangle(50, 50, 50, 50, BLUE);
grid[1][1] = 0;
}
}
//step 7
if(grid[0][2] == 0){
if(p.x <= 150 && p.x > 100 && p.y <=100 && p.y > 50){
Tft.fillRectangle(100, 50, 50, 50, BLUE);
grid[0][2] = 1;
}
} else if(grid[0][2] == 1){
if(p.x <= 150 && p.x > 100 && p.y <=100 && p.y > 50){
Tft.fillRectangle(100, 50, 50, 50, BLACK);
Tft.drawRectangle(100, 50, 50, 50, BLUE);
grid[0][2] = 0;
}
}
//step 8
if(grid[0][3] == 0){
if(p.x <= 200 && p.x > 150 && p.y <=100 && p.y > 50){
Tft.fillRectangle(150, 50, 50, 50, BLUE);
grid[0][3] = 1;
}
} else if(grid[0][3] == 1){
if(p.x <= 200 && p.x > 150 && p.y <=100 && p.y > 50){
Tft.fillRectangle(150, 50, 50, 50, BLACK);
Tft.drawRectangle(150, 50, 50, 50, BLUE);
grid[0][3] = 0;
}
}
delay(250); //debounce
}
}