Dear all
I am new working with an Arduino MKR 1010 to implement an IoT project with a display (ArduiTouch).
Now I have managed to shoot 2 Arduino MKR with this code, so that the USB interface doesn't show up anymore and I can't program the Arduino anymore.
Anybody got a hint? Double click on the white button I tried, the booloader still seems to be active...
THX for the help!
#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <XPT2046_Touchscreen.h>
#include <Fonts/FreeSans9pt7b.h>
#define _debug 1
#define TFT_CSÂ A3
#define TFT_DCÂ 0
#define TFT_MOSI 8
#define TFT_CLKÂ 9
#define TFT_RSTÂ 22
#define TFT_MISO 10
#define TFT_LEDÂ A2
#define HAVE_TOUCHPAD
#define TOUCH_CS A4
#define TOUCH_IRQ 1
#define ILI9341_ULTRA_DARKGREYÂ Â 0x632C
#define MINPRESSURE 10
#define MAXPRESSURE 2000
#define TS_MINX 230
#define TS_MINY 350
#define TS_MAXX 3700
#define TS_MAXY 3900
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
XPT2046_Touchscreen touch(TOUCH_CS, TOUCH_IRQ);
int tsx, tsy, tsxraw, tsyraw;
bool tsdown = false;
uint8_t rotation = 0;
void setup() {
 #ifdef _debug
 Serial.begin(115200);
 #endif
 pinMode(TFT_LED, OUTPUT);
 digitalWrite(TFT_LED, HIGH); Â
 tft.begin();
 touch.begin();
 #ifdef _debug
 Â
  Serial.print("tftx ="); Serial.print(tft.width()); Serial.print(" tfty ="); Serial.println(tft.height());
 #endif
Â
 tsx = 0;
 tsy = 0;
 tsxraw = 0;
 tsyraw = 0;
 tsdown = false;
 rotation = 0;
Â
 draw_screen(rotation);
}
void loop() {
 Â
  handleTouch();
  delay(100);
 }
void handleTouch() {
 TS_Point p;
 p = touch.getPoint();
 tsxraw = p.x;
 tsyraw = p.y;
 delay(1);
Â
 uint8_t rot = tft.getRotation();
Â
 switch (rot) {
  case 0: tsx = map(tsyraw, TS_MINY, TS_MAXY, 240, 0);
      tsy = map(tsxraw, TS_MINX, TS_MAXX, 0, 320);
      break;
  case 1: tsx = map(tsxraw, TS_MINX, TS_MAXX, 0, 320);
      tsy = map(tsyraw, TS_MINY, TS_MAXX, 0, 240);
      break;
  case 2: tsx = map(tsyraw, TS_MINY, TS_MAXY, 0, 240);
      tsy = map(tsxraw, TS_MINX, TS_MAXX, 320, 0);
      break;
  case 3: tsx = map(tsxraw, TS_MINX, TS_MAXX,320, 0);
      tsy = map(tsyraw, TS_MINY, TS_MAXY, 240, 0);
      break;
 }
Â
 if ((p.z > MINPRESSURE) != (tsdown)) {
  tsdown = (p.z > MINPRESSURE);
 Â
  if (tsdown && (tsx > (tft.width() / 2 - 20)) && (tsx < (tft.width() / 2 + 20))
   && (tsy > (tft.height() / 2 - 20)) && (tsy < (tft.height() / 2 + 20))) {
  Â
   rotation ++;
   if (rotation > 3) rotation = 0;
  }
 Â
  draw_screen(rotation);
 }
}
void draw_screen(uint8_t rot) {
 uint16_t w,h;
Â
 tft.setRotation(rot);
 tft.fillScreen(ILI9341_BLACK);
 tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
 tft.setFont(&FreeSans9pt7b);
 w = tft.width();
 h = tft.height();
Â
Â
 if ((rot==1) || (rot == 3)) {
  drawPosition1(tsxraw,tsyraw,tsx,tsy,tsdown);
  tft.fillRect(w/2 - 10, h/2 - 20,20,40, ILI9341_GREEN);
 } else {
  drawPosition2(tsxraw,tsyraw,tsx,tsy,tsdown);
  tft.fillRect(w/2 - 20, h/2 - 10,40,20, ILI9341_GREEN);
 }
Â
 tft.setCursor(w/2-5,h/2+6);
 tft.setTextColor(ILI9341_BLACK, ILI9341_GREEN);
 tft.print(rot);
Â
 tft.drawLine(0,0,20,0,ILI9341_WHITE);
 tft.drawLine(0,0,0,20,ILI9341_WHITE);
 tft.drawLine(0,0,40,40,ILI9341_WHITE);
 tft.drawLine(w-1,0,w-20,0,ILI9341_WHITE);
 tft.drawLine(w-1,0,w-1,20,ILI9341_WHITE);
 tft.drawLine(w-1,0,w-40,40,ILI9341_WHITE);
 tft.drawLine(w-1,h-1,w-40,h,ILI9341_WHITE);
 tft.drawLine(w-1,h-1,w,h-40,ILI9341_WHITE);
 tft.drawLine(w-1,h-1,w-40,h-40,ILI9341_WHITE);
 tft.drawLine(0,h-1,20,h-1,ILI9341_WHITE);
 tft.drawLine(0,h-1,0,h-20,ILI9341_WHITE);
 tft.drawLine(0,h-1,40,h-40,ILI9341_WHITE);
}
void drawPosition1(uint16_t xraw, uint16_t yraw, uint16_t x, uint16_t y, bool down) {
 tft.setCursor(20,60);
 tft.print("X = ");
 display_right(110,60,String(x));
 tft.setCursor(180,60);
 tft.print("Y = ");
 display_right(270,60,String(y));
 tft.setCursor(20,180);
 tft.print("Xraw = ");
 display_right(120,180,String(xraw));
 tft.setCursor(180,180);
 tft.print("Yraw = ");
 display_right(280,180,String(yraw));
 if (down) tft.fillCircle(160,160,10,ILI9341_RED); else tft.fillCircle(160,160,10,ILI9341_YELLOW);
}Â
void drawPosition2(uint16_t xraw, uint16_t yraw, uint16_t x, uint16_t y, bool down) {
 tft.setCursor(20,60);
 tft.print("X = ");
 display_right(110,60,String(x));
 tft.setCursor(20,100);
 tft.print("Y = ");
 display_right(110,100,String(y));
 tft.setCursor(20,240);
 tft.print("Xraw = ");
 display_right(120,240,String(xraw));
 tft.setCursor(20,280);
 tft.print("Yraw = ");
 display_right(120,280,String(yraw));
 if (down) tft.fillCircle(120,200,10,ILI9341_RED); else tft.fillCircle(120,200,10,ILI9341_YELLOW);
}Â
void display_right(int x, int y, String val) {
 int16_t x1, y1;
 uint16_t w, h;
 int str_len = val.length() + 1;
 char char_array[str_len];
 val.toCharArray(char_array, str_len);
 tft.getTextBounds(char_array, x, y, &x1, &y1, &w, &h);Â
 tft.setCursor(x - w, y);
 tft.print(char_array);
}