2 Arduino MKR with no USB connection

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. :confused:

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);
}

Upload something simple from EXAMPLE such as BLINk or BARE MINIMUM after putting it into bootloader mode to clear out the errant sketch.
Don't forget to choose the BOOTLOADER COM POST in the IDE before uploading.

Bob.

Great, thx Bob

Both boards are alive again.

Two questions remain

  • Why did I shoot the two MKRs with this code?
  • What do you mean with "Don't forget to choose the BOOTLOADER COM POST in the IDE before uploading."? I work with Arduino Create...

THX
Martin

You can use either CREATE or the regular IDE in bootloader mode.
You simply double click the reset to put it into bootloader mode then select whatever COM port it will give you.

Not really a coding person so I could not really tell you why they lock up your board.
However if it were me I would start with lhe libraries used first in case one is meant for a different MCU.

Bob.

Many THX for your support, Bob