Uno won't start sketch on external powersupply

I'm not sure if this is a (specific) Uno problem or a project related problem.

My sketch will only work right after uploading. Resetting the arduino while still being powered by the pc(mac) will show a working sketch. Only when i disconnect and use a external powersupply or the same usb powersupply, the sketch won't start.

For debugging i've included a Serial.println() in setup() and connected the arduino's TX to a serial port (via TTL to serial). This also won't show the Serial.prinln() when the arduino is powercycled after uploading the sketch.

#include "velbus.h"
#include <SPI.h>
#include <stdio.h>
#define INT8U unsigned char
#define BufferLength 128

int errorled = 7;
int scanbutton = 6;
unsigned long buttonDebounce = millis() + 75;

int clearfilter = 0;
int wtwled_1 = 3;
int wtwled_2 = 4;
int wtwled_3 = 5;
boolean wtwsend = false;
byte wtwsend_address = 0x00;
byte wtwsend_clear = 0x00;

VelbusPacket vbpbuffer[BufferLength];
VelbusPacket receivedpacket;
VelbusPacket sendpacket;
int BufferStart = 0;
int BufferEnd = 0;

void setup()
{
  pinMode(scanbutton, INPUT);
  pinMode(errorled,OUTPUT);

  pinMode(wtwled_1, OUTPUT);  
  pinMode(wtwled_2, OUTPUT);  
  pinMode(wtwled_3, OUTPUT);  

  digitalWrite(errorled, HIGH);

  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  
  delay(2000);
 
  VELBUS.begin();                   // init velbus : default baudrate = 16k666, default SPI chip select pin = 10
  attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt when new packet has been received
  Serial.begin(9600);
  Serial.println("debug: Arduino start");
}

void MCP2515_ISR()
{ 
  while (VELBUS.checkReceive() == CAN_MSGAVAIL)
  {
    if (VELBUS.receivepacket(receivedpacket))
    {
      int BufferEndNow = BufferEnd;
      BufferEndNow++;                                  // fill receivebuffer with new packet so we can process it in the loop
      if (BufferEndNow >= BufferLength) BufferEndNow = 0;
      if (BufferEndNow != BufferStart)
      {
        vbpbuffer[BufferEndNow] = receivedpacket;
        BufferEnd = BufferEndNow;
      }
    }
  }
}

void loop()
{
    if(BufferStart != BufferEnd)                   // check if buffer is not empty
    {
      BufferStart ++;
      if (BufferStart >= BufferLength) BufferStart = 0;
      
      if(vbpbuffer[BufferStart].Address == 0xA3)
      {
        if(vbpbuffer[BufferStart].dataLen == 4)
        {
          if((vbpbuffer[BufferStart].Data[0] == 0x00) && (vbpbuffer[BufferStart].Data[1] == 0x01) && (vbpbuffer[BufferStart].Data[2] == 0x00))
          {
            Serial.println("debug: WTW Stand 1");  
            digitalWrite(wtwled_1, HIGH);
            digitalWrite(wtwled_2, LOW);
            digitalWrite(wtwled_3, LOW);
            wtwsend = true;
            wtwsend_address = 0x01;
          }
          else if ((vbpbuffer[BufferStart].Data[0] == 0x00) && (vbpbuffer[BufferStart].Data[1] == 0x02) && (vbpbuffer[BufferStart].Data[2] == 0x00))
          {
            Serial.println("debug: WTW Stand 2");  
            digitalWrite(wtwled_1, LOW);
            digitalWrite(wtwled_2, HIGH);
            digitalWrite(wtwled_3, LOW);
            wtwsend = true;
            wtwsend_address = 0x02;
          }
          else if ((vbpbuffer[BufferStart].Data[0] == 0x00) && (vbpbuffer[BufferStart].Data[1] == 0x08) && (vbpbuffer[BufferStart].Data[2] == 0x00))
          {
            Serial.println("debug: WTW Stand 3");  
            digitalWrite(wtwled_1, LOW);
            digitalWrite(wtwled_2, LOW);
            digitalWrite(wtwled_3, HIGH);
            wtwsend = true;
            wtwsend_address = 0x08;
          }

          if (wtwsend == true)
          {
            sendpacket.RTR = false;
            sendpacket.Priority = Priority::low;
            sendpacket.Address = 0xA3;
            sendpacket.Data[0] = 0xF6;
            sendpacket.Data[1] = wtwsend_address;
            sendpacket.dataLen = 2;
  
            if (VELBUS.sendpacket(sendpacket)) digitalWrite(errorled,HIGH); else digitalWrite(errorled, LOW); 

            delay(90); // delay 90ms 

            wtwsend_clear = 0x0B - wtwsend_address;  // ClearLed 0x0B (led 1,2,8 ) minus new setled
            
            sendpacket.RTR = false;
            sendpacket.Priority = Priority::low;
            sendpacket.Address = 0xA3;
            sendpacket.Data[0] = 0xF5;
            sendpacket.Data[1] = wtwsend_clear;
            sendpacket.dataLen = 2;
  
            if (VELBUS.sendpacket(sendpacket)) digitalWrite(errorled,HIGH); else digitalWrite(errorled, LOW); 
  
            wtwsend = false;
          }
        }

        // feedback for homeseer
        Serial.print("canbus: ");
        Serial.print(vbpbuffer[BufferStart].Address,16);
        Serial.print(" ");
        for(int i = 0; i<vbpbuffer[BufferStart].dataLen; i++)    // print the data
        {
          Serial.print(vbpbuffer[BufferStart].Data[i], 16); 
          Serial.print(" ");
        }
        Serial.println("");
      }
    }
    
    
    // EXAMPLE OF SENDING VELBUS PACKET
    
    // below we check the button on pin 6. When the pin is LOW for 75ms (debounce time of 75ms) we send a scan packet to the bus
    
    if (digitalRead(scanbutton) == HIGH)
    {
      if (millis() > buttonDebounce && buttonDebounce > 0)
      {
        
        if (clearfilter == 1)
        {
          sendpacket.RTR = false;
          sendpacket.Priority = Priority::low;
          sendpacket.Address = 0xA3;
          sendpacket.Data[0] = 0xF5;
          sendpacket.Data[1] = 0x04;
          sendpacket.dataLen = 2;
  
          
          if (VELBUS.sendpacket(sendpacket)) digitalWrite(errorled,HIGH); else digitalWrite(errorled, LOW); // if you send a next packet within 90ms after the previous packet, VELBUS.sendpacket() will return false and the packet will not be sent (to avoid flooding the velbus). To avoid this, you can check VELBUS.cansend() before sending the packet

          buttonDebounce = 0;
          clearfilter = 0;
        }
        else
        {
          sendpacket.RTR = false;
          sendpacket.Priority = Priority::low;
          sendpacket.Address = 0xA3;
          sendpacket.Data[0] = 0xF7;
          sendpacket.Data[1] = 0x04;
          sendpacket.dataLen = 2;
        
          if (VELBUS.sendpacket(sendpacket)) digitalWrite(errorled,HIGH); else digitalWrite(errorled, LOW); // if you send a next packet within 90ms after the previous packet, VELBUS.sendpacket() will return false and the packet will not be sent (to avoid flooding the velbus). To avoid this, you can check VELBUS.cansend() before sending the packet
   
          buttonDebounce = 0;
          clearfilter = 1;
        }
      }
    }
    else
    {
      buttonDebounce = millis() + 75;
    }
}

FabianNL:
For debugging i've included a Serial.println() in setup() and connected the arduino's TX to a serial port (via TTL to serial). This also won't show the Serial.prinln() when the arduino is powercycled after uploading the sketch.

Did you forgot to connect the grounds?

All grounds are connected. I'm assuming all leds would stay off when ground isn't connected

FabianNL:
I'm assuming all leds would stay off when ground isn't connected

The serial converter very likely gets power from the USB port. You stated your Uno has its own power supply. Which means your assumption is wrong.

All grounds are connected.

The ground on your serial converter is connected to your Uno's ground?

All grounds are connected. External powersupply, power supplied bij the usb/serial converter or powered via the usb port all won't work unelss i reprogram the arduino and keep it powered.

I've replaced the UNO r3 with an older UNO, both original arduinos. Same code, same wiring and it all works now as it should.

Could it be a bootloader issue on this r3 ?

Write a short sketch that blinks LEDs in a pattern that demonstrates it is working even if nothing appears in the Serial monitor and see if that works on the external power supply.

...R

Uno powers both the 328p and the serial converter chips from the same internal board's 5V bus independent of how the board is powered. If the 328p has power so will the USB converter chip even if not connected to a PC.

FabianNL:
I'm not sure if this is a (specific) Uno problem or a project related problem.

My sketch will only work right after uploading. Resetting the arduino while still being powered by the pc(mac) will show a working sketch. Only when i disconnect and use a external powersupply or the same usb powersupply, the sketch won't start.

For debugging i've included a Serial.println() in setup() and connected the arduino's TX to a serial port (via TTL to serial). This also won't show the Serial.prinln() when the arduino is powercycled after uploading the sketch.

I too have a similar problem... sketch runs fine with USB powered.
Common to my sketch is SPI, which I use for RF24. So SPI and Serial may be related.
Try commenting out all Serial.print and see if that helps?